-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
80 lines (68 loc) · 2.64 KB
/
Copy pathtesting.py
File metadata and controls
80 lines (68 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
This file contains functions for evaluating the trained models.
"""
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, roc_curve, auc
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import label_binarize
def evaluate_model(model, X_test_path='data/X_test.csv', y_test_path='data/y_test.csv'):
"""
Evaluates a trained model and returns a dictionary of metrics.
"""
X_test = pd.read_csv(X_test_path)
y_test = pd.read_csv(y_test_path).values.ravel()
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)
metrics = {
'accuracy': accuracy_score(y_test, y_pred),
'precision': precision_score(y_test, y_pred, average='macro'),
'recall': recall_score(y_test, y_pred, average='macro'),
'f1_score': f1_score(y_test, y_pred, average='macro'),
'confusion_matrix': confusion_matrix(y_test, y_pred),
'y_test': y_test,
'y_prob': y_prob
}
return metrics
def plot_confusion_matrix(cm, labels, model_name):
"""
Plots a confusion matrix.
"""
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=labels, yticklabels=labels)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title(f'Confusion Matrix for {model_name}')
plt.show()
def plot_roc_curves(results):
"""
Plots ROC curves for all models.
"""
plt.figure(figsize=(10, 8))
for model_name, metrics in results.items():
y_test = metrics['y_test']
y_prob = metrics['y_prob']
# Binarize the output
y_test_bin = label_binarize(y_test, classes=np.unique(y_test))
n_classes = y_test_bin.shape[1]
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test_bin[:, i], y_prob[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test_bin.ravel(), y_prob.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
plt.plot(fpr["micro"], tpr["micro"],
label=f'{model_name} (AUC = {roc_auc["micro"]:.2f})')
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curves')
plt.legend(loc="lower right")
plt.show()