Zaktualizuj 'evaluate.py'
This commit is contained in:
parent
c621e2ce45
commit
f93259b711
122
evaluate.py
122
evaluate.py
@ -1,39 +1,13 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
import torch.nn.functional as F
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.metrics import accuracy_score, f1_score, precision_score
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
# Wczytanie danych
|
||||
print(os.getcwd())
|
||||
data = pd.read_csv("./Sales.csv")
|
||||
|
||||
# Przygotowanie danych
|
||||
data["Profit_Category"] = pd.cut(data["Profit"], bins=[-np.inf, 500, 1000, np.inf], labels=[0, 1, 2])
|
||||
bike = data.loc[:, ['Customer_Age', 'Customer_Gender', 'Country','State', 'Product_Category', 'Sub_Category', 'Profit_Category']]
|
||||
bikes = pd.get_dummies(bike, columns=['Country', 'State', 'Product_Category', 'Sub_Category', 'Customer_Gender'])
|
||||
X = bikes.drop('Profit_Category', axis=1).values
|
||||
y = bikes['Profit_Category'].values
|
||||
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=0)
|
||||
scaler = StandardScaler()
|
||||
X = scaler.fit_transform(X)
|
||||
#### Tworzenie tensorów
|
||||
X_train = X_train.astype(np.float32)
|
||||
X_test = X_test.astype(np.float32)
|
||||
y_train = y_train.astype(np.float32)
|
||||
y_test = y_test.astype(np.float32)
|
||||
X_train=torch.FloatTensor(X_train)
|
||||
X_test=torch.FloatTensor(X_test)
|
||||
y_train=torch.LongTensor(y_train)
|
||||
y_test=torch.LongTensor(y_test)
|
||||
|
||||
#### Model
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
import torch.nn.functional as F
|
||||
|
||||
# Definicja modelu
|
||||
class ANN_Model(nn.Module):
|
||||
def __init__(self,input_features=82,hidden1=20,hidden2=20,out_features=3):
|
||||
super().__init__()
|
||||
@ -46,60 +20,50 @@ class ANN_Model(nn.Module):
|
||||
x=self.out(x)
|
||||
return x
|
||||
|
||||
torch.manual_seed(20)
|
||||
model=ANN_Model()
|
||||
model.parameters
|
||||
# Wczytanie danych
|
||||
data = pd.read_csv("./Sales.csv")
|
||||
|
||||
def calculate_accuracy(model, X, y):
|
||||
# Przygotowanie danych
|
||||
data["Profit_Category"] = pd.cut(data["Profit"], bins=[-np.inf, 500, 1000, np.inf], labels=[0, 1, 2])
|
||||
bike = data.loc[:, ['Customer_Age', 'Customer_Gender', 'Country','State', 'Product_Category', 'Sub_Category', 'Profit_Category']]
|
||||
bikes = pd.get_dummies(bike, columns=['Country', 'State', 'Product_Category', 'Sub_Category', 'Customer_Gender'])
|
||||
X = bikes.drop('Profit_Category', axis=1).values
|
||||
y = bikes['Profit_Category'].values
|
||||
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2,random_state=0)
|
||||
scaler = StandardScaler()
|
||||
X = scaler.fit_transform(X)
|
||||
X_test = X_test.astype(np.float32)
|
||||
y_test = y_test.astype(np.float32)
|
||||
X_test=torch.FloatTensor(X_test)
|
||||
y_test=torch.LongTensor(y_test)
|
||||
|
||||
# Wczytanie modelu
|
||||
model = torch.load("classificationn_model.pt")
|
||||
|
||||
# Funkcja do obliczania predykcji
|
||||
def calculate_predictions(model, X):
|
||||
with torch.no_grad():
|
||||
outputs = model(X)
|
||||
_, predicted = torch.max(outputs.data, 1)
|
||||
total = y.size(0)
|
||||
correct = (predicted == y).sum().item()
|
||||
accuracy = correct / total * 100
|
||||
return accuracy
|
||||
return predicted
|
||||
|
||||
def calculate_f1(model, X, y):
|
||||
with torch.no_grad():
|
||||
outputs = model(X)
|
||||
_, predicted = torch.max(outputs.data, 1)
|
||||
f1 = f1_score(y, predicted, average='weighted')
|
||||
return f1
|
||||
# Obliczenie predykcji
|
||||
y_pred = calculate_predictions(model, X_test)
|
||||
y_pred_np = y_pred.numpy()
|
||||
|
||||
def calculate_precision(model, X, y):
|
||||
with torch.no_grad():
|
||||
outputs = model(X)
|
||||
_, predicted = torch.max(outputs.data, 1)
|
||||
precision = precision_score(y, predicted, average='weighted')
|
||||
return precision
|
||||
# Zapisanie predykcji do pliku
|
||||
np.savetxt("predictions.txt", y_pred_np, fmt='%d')
|
||||
|
||||
loss_function = nn.CrossEntropyLoss()
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
|
||||
# Obliczenie metryk
|
||||
accuracy = accuracy_score(y_test.numpy(), y_pred_np)
|
||||
f1 = f1_score(y_test.numpy(), y_pred_np, average='micro')
|
||||
precision = precision_score(y_test.numpy(), y_pred_np, average='micro')
|
||||
recall = recall_score(y_test.numpy(), y_pred_np, average='micro')
|
||||
|
||||
epochs = 100
|
||||
final_losses = []
|
||||
|
||||
for i in range(epochs):
|
||||
i = i + 1
|
||||
y_pred = model.forward(X_train)
|
||||
loss = loss_function(y_pred, y_train)
|
||||
final_losses.append(loss)
|
||||
|
||||
train_accuracy = calculate_accuracy(model, X_train, y_train)
|
||||
test_accuracy = calculate_accuracy(model, X_test, y_test)
|
||||
train_f1 = calculate_f1(model, X_train, y_train)
|
||||
test_f1 = calculate_f1(model, X_test, y_test)
|
||||
train_precision = calculate_precision(model, X_train, y_train)
|
||||
test_precision = calculate_precision(model, X_test, y_test)
|
||||
|
||||
print(f"Epoch: {i}, Loss: {loss.item()}, "
|
||||
f"Train Accuracy: {train_accuracy}%, Test Accuracy: {test_accuracy}%, "
|
||||
f"Train F1: {train_f1}, Test F1: {test_f1}, "
|
||||
f"Train Precision: {train_precision}, Test Precision: {test_precision}")
|
||||
|
||||
optimizer.zero_grad()
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
torch.save(model,"classificationn_model.pt")
|
||||
# Zapisanie metryk do pliku
|
||||
with open("metrics.txt", "w") as f:
|
||||
f.write(f"Accuracy: {accuracy}\n")
|
||||
f.write(f"F1 Score: {f1}\n")
|
||||
f.write(f"Precision: {precision}\n")
|
||||
f.write(f"Recall: {recall}\n")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user