-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig_model_stats.py
141 lines (119 loc) · 3.28 KB
/
fig_model_stats.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import colormaps
import sklearn
import pandas as pd
import sklearn.metrics
import shap
import xgboost as xgb
import joblib
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OrdinalEncoder
from xgboost import core
from staty_base import stratified_train_test_split
def add_confusion_matrix_plot(axs, real, pred, name: str, col, accuracy: float):
ax = axs[col-1]
cmd = sklearn.metrics.ConfusionMatrixDisplay.from_predictions(
y_true=real,
y_pred=pred,
display_labels=y['Tag'].drop_duplicates(),
ax=ax,
colorbar=False,
)
cmd.ax_.tick_params(axis='x', labelrotation=75)
cmd.ax_.set(
xlabel=None,
ylabel=None,
title=name + f" ({accuracy * 100:.2f} %)".replace(".", ",")
)
FILE_NAME = "./collected-data/flat-replay-data-5rep.csv"
MODEL_STORARE_DIR = "./models/"
SEED = 3142
dataset = pd.read_csv(FILE_NAME) # For Pandas
dataset = dataset.astype({"Tag": str})
mappingNumToCat = {
"0": "Normal",
# "Stunt": 1,
# "Maze": 2,
"3": "Offroad",
# "Laps": 4,
"5": "Fullspeed",
"6": "LOL",
"7": "Tech",
"8": "SpeedTech",
# "RPG": 9,
"10": "PressForward",
# "Trial": 11,
"12": "Grass",
}
dataset['Tag'] = dataset['Tag'].replace(mappingNumToCat) # Map to categorical
# split data into X and y
X, y = dataset.drop("Tag", axis=1), dataset[['Tag']] # For Pandas
# Encode y to numeric
y_encoded = OrdinalEncoder().fit_transform(y)
# Split the data
X_train, y_train, X_test, y_test = stratified_train_test_split(y_encoded, X)
fig, axs = plt.subplots(figsize=(2*5, 1*5), ncols=3, nrows=1, sharex=True, sharey=True, layout="constrained")
if True:
model = joblib.load(MODEL_STORARE_DIR + "dummy_model.pkl")
y_pred = model.predict(X_test)
add_confusion_matrix_plot(
axs=axs,
real=y_test,
pred=y_pred,
name="Simulacre",
col=1,
accuracy=model.score(X, y_encoded)
)
if True:
model = joblib.load(MODEL_STORARE_DIR + "logistic_regression_model.pkl")
y_pred = model.predict(X_test)
count_correct = 0
for i in range(len(y_test)):
if (y_pred[i] == y_test[i]):
count_correct += 1
percent_correct = count_correct / len(y_test)
add_confusion_matrix_plot(
axs=axs,
real=y_test,
pred=y_pred,
name="Régression logistique",
col=2,
accuracy=percent_correct
)
if True:
dtest_clf = xgb.DMatrix(X_test, y_test, enable_categorical=True)
model = joblib.load(MODEL_STORARE_DIR + "xgboost_model.pkl")
y_pred = model.predict(dtest_clf)
count_correct = 0
for i in range(len(y_test)):
if (y_pred[i] == y_test[i]):
count_correct += 1
percent_correct = count_correct / len(y_test)
add_confusion_matrix_plot(
axs=axs,
real=y_test,
pred=y_pred,
name="XGBoost",
col=3,
accuracy=percent_correct
)
plt.suptitle("Matrices de confusion")
count_max = y_test[y_test[:] == y_test[0]].shape[0]
cbar_ax = fig.add_axes([0.05, 0.01, 0.02, 0.22])
colorbar = fig.colorbar(
mpl.cm.ScalarMappable(
norm=mpl.colors.Normalize(
vmin=0,
vmax=count_max
),
cmap=colormaps.get_cmap("viridis")
),
cax=cbar_ax,
orientation='vertical'
)
colorbar.set_ticks([0, count_max])
fig.supylabel("Réel")
fig.supxlabel("Prédite")
plt.savefig("rendered-figs/fig-model-stats.pdf")
# plt.show()