-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel Training
More file actions
77 lines (63 loc) · 2.29 KB
/
Copy pathModel Training
File metadata and controls
77 lines (63 loc) · 2.29 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
# Define classifiers
classifiers = [
LogisticRegression(max_iter=1000),
GradientBoostingClassifier(),
SVC(kernel='linear', probability=True),
SVC(kernel='rbf', probability=True),
KNeighborsClassifier(),
DecisionTreeClassifier(),
RandomForestClassifier(),
MLPClassifier(max_iter=1000)
]
# Classifier names
classifier_names = [
"Logistic Regression", "Gradient Boosting", "SVM (Linear)", "SVM (RBF)",
"K-Nearest Neighbours (k-NN)", "Decision Tree", "Random Forest", "Neural Network"
]
# Initialize storage for metrics
results = []
# Fit, evaluate, and visualize classifiers
for clf, name in zip(classifiers, classifier_names):
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
y_prob = clf.predict_proba(X_test)[:, 1] if hasattr(clf, "predict_proba") else None
# Collect metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_prob) if y_prob is not None else None
results.append({
"Model": name,
"Accuracy": round(accuracy,4),
"Precision_Score": round(precision,4),
"Recall_Score": round(recall,4),
"F1_Score": round(f1,4),
"ROC_AUC": round(roc_auc,4)
})
# Create DataFrame
comparison_table = pd.DataFrame(results)
# Print the comparison table
print(comparison_table)
# Plot the table
fig, ax = plt.subplots(figsize=(14, 8)) # Set the figure size
ax.axis('tight')
ax.axis('off')
# Table
table = ax.table(cellText=comparison_table.values, colLabels=comparison_table.columns, cellLoc='center', loc='center')
# Styling the table
table.auto_set_font_size(False)
table.set_fontsize(14)
table.scale(1.5, 1.5)
# Set colors for header
for j in range(len(comparison_table.columns)):
table[0, j].set_fontsize(12)
table[0, j].set_text_props(weight='bold')
table[0, j].set_facecolor('#40466e')
table[0, j].set_text_props(color='white')
# Define the indices of the rows you want to highlight
highlight_rows = [1, 3, 5, 7] # Change these to the indices of the rows you want to highlight
for highlight_row in highlight_rows:
for i in range(len(comparison_table.columns)):
table[highlight_row, i].set_facecolor('#c5c5e5')
plt.show()