-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
97 lines (79 loc) · 2.57 KB
/
model.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
#%%
from database import main
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
import time
import pickle
"""
Bonne configuration après GridSearch
{'subsample': 1.0,
'n_estimators': 250,
'min_child_weight': 4,
'max_depth': 4,
'gamma': 0.5,
'eta': 0.3,
'colsample_bytree': 0.6}
"""
# %% XGBOOST
df = main()
y = df["rating"]
X = df.drop(['rating'], axis=1)
# Break off test set from the training data
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, test_size=0.2)
# For the GridSearchCV/RandomizedSearchCV
params = {
'n_estimators':[250],
'min_child_weight':[4,5],
'gamma':[i/10.0 for i in range(4,6)],
'subsample':[i/10.0 for i in range(7,11)],
'colsample_bytree':[i/10.0 for i in range(6,11)],
'max_depth': [4,6,7],
'eta': [i/10.0 for i in range(3,6)],
}
# With fine-tuned parameters
clf = XGBClassifier(subsample=1.0,
n_estimators=250,
min_child_weight=4,
max_depth=4,
gamma=0.5,
eta=0.3,
colsample_bytree=0.6,
objective="multi:softprob")
# In comments: parameters for RandomizedSearchCV
#n_iter_search = 80
#random_search = RandomizedSearchCV(clf,
# param_distributions=params,
# n_iter=n_iter_search,
# cv=5,
# verbose=2,
# scoring="accuracy",
# n_jobs=-1,
# random_state=0
# )
start = time.time()
clf.fit(X_train, y_train)
print("RandomizedSearchCV took %.2f seconds"
" parameter settings." % ((time.time() - start)))
# %%
#def predict_class(self, X):
# out = self.predict(X)
# return np.clip(np.round(out), 0, 3)
#model.predict_class = predict_class.__get__(model)
#model.predict_class(X_test)
# %%
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt
predictions = clf.predict(X_test)
cm = confusion_matrix(y_test, predictions)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()
# %%
print(classification_report(y_test, clf.predict(X_test)))
# %%
f_i = clf.feature_importances_
features = X.columns
f_i, features = zip(*sorted(zip(f_i, features), reverse=True))