-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
77 lines (70 loc) · 2.54 KB
/
Copy pathevaluation.py
File metadata and controls
77 lines (70 loc) · 2.54 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
"""
evaluation.py
Métriques et plots pour classification et régression.
"""
import streamlit as st
from sklearn.metrics import (accuracy_score, precision_score, recall_score, f1_score, roc_auc_score,
confusion_matrix, mean_absolute_error, mean_squared_error, r2_score)
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
def evaluate_classification(model, X_test, y_test, show_plots=True):
preds = model.predict(X_test)
probs = None
try:
probs = model.predict_proba(X_test)[:,1]
except Exception:
try:
probs = model.decision_function(X_test)
except Exception:
probs = None
acc = accuracy_score(y_test, preds)
prec = precision_score(y_test, preds, average='weighted', zero_division=0)
rec = recall_score(y_test, preds, average='weighted', zero_division=0)
f1 = f1_score(y_test, preds, average='weighted', zero_division=0)
auc = None
if probs is not None and len(np.unique(y_test))==2:
try:
auc = roc_auc_score(y_test, probs)
except Exception:
auc = None
metrics = {"accuracy": acc, "precision": prec, "recall": rec, "f1": f1, "auc": auc}
st.write(metrics)
if show_plots:
# confusion matrix
cm = confusion_matrix(y_test, preds)
fig, ax = plt.subplots()
sns.heatmap(cm, annot=True, fmt='d', ax=ax, cmap='Blues')
ax.set_title("Confusion Matrix")
st.pyplot(fig)
return metrics
def evaluate_regression(model, X_test, y_test, show_plots=True):
preds = model.predict(X_test)
mae = mean_absolute_error(y_test, preds)
mse = mean_squared_error(y_test, preds)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, preds)
metrics = {"mae":mae, "mse":mse, "rmse":rmse, "r2":r2}
st.write(metrics)
if show_plots:
fig, ax = plt.subplots()
ax.scatter(y_test, preds, alpha=0.6)
ax.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red')
ax.set_xlabel("Vérité terrain")
ax.set_ylabel("Prédictions")
ax.set_title("Vérité terrain vs Prédictions")
st.pyplot(fig)
return metrics
def plot_learning_curve(history):
import matplotlib.pyplot as plt
st.subheader("Courbes d'entraînement")
if isinstance(history, dict):
keys = history.keys()
fig, ax = plt.subplots()
for k in keys:
ax.plot(history[k], label=k)
ax.legend()
st.pyplot(fig)
else:
st.write("Historique non disponible.")