Zaktualizuj 'evaluate.py'

This commit is contained in:
Witold Woch 2023-05-12 23:52:52 +02:00
parent f93259b711
commit 9f88ab7365

View File

@ -7,7 +7,6 @@ from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_sc
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__()
@ -20,10 +19,8 @@ class ANN_Model(nn.Module):
x=self.out(x)
return x
# Wczytanie danych
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'])
@ -37,30 +34,24 @@ 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)
return predicted
# Obliczenie predykcji
y_pred = calculate_predictions(model, X_test)
y_pred_np = y_pred.numpy()
# Zapisanie predykcji do pliku
np.savetxt("predictions.txt", y_pred_np, fmt='%d')
# 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')
# Zapisanie metryk do pliku
with open("metrics.txt", "w") as f:
f.write(f"Accuracy: {accuracy}\n")
f.write(f"F1 Score: {f1}\n")