-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification_and_regression.py
399 lines (344 loc) · 13.4 KB
/
classification_and_regression.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm, model_selection
import pandas as pd
from sklearn.metrics import accuracy_score, classification_report, precision_recall_fscore_support
from sklearn.pipeline import Pipeline
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, r2_score, mean_squared_error
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import SelectKBest, f_regression, f_classif
import xgboost as xgb
import warnings
warnings.filterwarnings("ignore")
def float_encoding(string):
'''
Convertion function
'''
if string == "positive":
return 2
elif string == "neutral":
return 1
else:
return 0
def calc_delta(a, b):
'''
Calculates Delta between two numbers a and b keeping the sign of the variation
'''
if a > b:
delta = b - a
else:
delta = a - b
if delta < 0:
delta = - delta
return delta
def level_polarity(num):
'''
Identifies the polarity for given number (positive, negative, null)
'''
if float(num) == 0:
return "No Variation"
elif float(num) > 0:
return "Positive Variation"
else:
return "Negative Variation"
def model_name(text):
'''
Extracts the name of the model from a standard text
'''
try:
return str(text).split()[4].replace("(", "").replace(")", "").replace("]", "").replace(",", "")
except:
return "(Name Not Available)"
def model_name_reg(text):
'''
Extracts the name of the model from a standard text. Customized for regression model
'''
try:
return str(text).split()[7].replace("(", "").replace(")", "").replace("]", "").replace(",", "")
except:
return "(Name Not Available)"
def extract_model_informations(fav_mod, y, data):
'''
Extracts the informations of the given model
:param fav_mod: model
:param y: target variable
:return: prints informations
'''
print("\n\033[36mRISULTATI\033[0m")
name_best = model_name(fav_mod)
if name_best == "LinearDiscriminantAnalysis":
best_mod = fav_mod.best_estimator_
selected_features = data.columns[best_mod.named_steps['selector'].get_support()]
print(
f"\n\033[01mL'accuracy migliore con y = {y} è del modello {name_best} con un punteggio di {best_acc} di accuracy\033[0m\n",
f"\033[01m\nVariabili utilizzate:\033[0m\n {tuple(selected_features)}\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
elif name_best == "SVC":
best_mod = fav_mod.best_estimator_
selected_features = data.columns[best_mod.named_steps['selector'].get_support(indices=True)].tolist()
print(
f"\n\033[01mL'accuracy migliore con y = {y} è del modello {name_best} con un punteggio di {best_acc} di accuracy\033[0m\n",
f"\033[01m\nVariabili utilizzate:\033[0m\n {selected_features}\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
else:
best_mod = fav_mod.best_estimator_
print(
f"\n\033[01mL'accuracy migliore con y = {y} è del modello {name_best} con un punteggio di {best_acc} di accuracy\033[0m\n",
f"\033[31m\nNon sono disponibili informazioni sulle variabili selezionate\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
def extract_model_informations_reg(fav_mod,y):
'''
Extracts the informations of the given regression model
:param fav_mod: model
:param y: target variable
:return: prints informations
'''
print("\n\033[36mRISULTATI\033[0m")
name_best = model_name_reg(fav_mod)
if name_best == "LinearRegression":
best_mod = fav_mod.best_estimator_
selected_features = x_data_reg.columns[best_mod.named_steps['selector'].get_support()]
print(
f"\n\033[01mL'MSE migliore con y = {y} è del modello {name_best} con un punteggio di {best_score} di MSE"
f" e un R2 di {best_R2}\033[0m\n",
f"\033[01m\nVariabili utilizzate:\033[0m\n {tuple(selected_features)}\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
elif name_best == "xgb.XGBRegressor":
best_mod = fav_mod.best_estimator_
selected_features = x_data_reg.columns[best_mod.named_steps['selector'].get_support(indices=True)].tolist()
print(
f"\n\033[01mL'MSE migliore con y = {y} è del modello {name_best} con un punteggio di {float(best_score)} di MSE"
f"e un R2 di {best_R2}\033[0m\n",
f"\033[01m\nVariabili utilizzate:\033[0m\n {selected_features}\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
else:
best_mod = fav_mod.best_estimator_
print(
f"\n\033[01mL'MSE migliore con y = {y} è del modello {name_best} con un punteggio di {float(best_score)} di MSE"
f"e un R2 di {best_R2}\033[0m\n",
f"\033[31m\nNon sono disponibili informazioni sulle variabili selezionate\033[0m\n",
f"\033[01m\nDettagli modello:\033[0m\n",
best_mod, '\n')
if __name__ == '__main__':
df = pd.read_csv("premier_with_sentiment.csv")
print(df.keys())
df = df.dropna()
print(len(df))
# PREPARING DATA
df["float_emotion_vader"] = df["vader_emotion_before"].apply(float_encoding)
df["float_emotion_tb"] = df["tb_emotion_before"].apply(float_encoding)
x_data = df[["tb_polarity_before", "float_emotion_tb",
"float_emotion_vader", "vader_polarity_before", "goal", "assist",
"minutes"]]
y_data = df[["char_rating"]]
x_train, x_test, y_train, y_test = model_selection.train_test_split(
x_data, y_data, test_size=0.33, random_state=42)
# MODELS FOR Y = RATING CHAR (SENTIMENT BEFORE TO GAME RATING)
# SVC Pipeline
pipeline_svc = Pipeline(
[
('selector', SelectKBest(f_classif)),
('model', svm.SVC())
]
)
parameters_svc = {
'selector__k': [1, 2, 3, 4],
'model__kernel': ['linear', 'rbf'],
'model__C': [1, 10],
'model__probability': [True]}
svc = model_selection.GridSearchCV(
estimator=pipeline_svc,
param_grid=parameters_svc,
n_jobs=-1,
scoring="f1_micro",
cv=5,
verbose=2
)
# LDA Pipeline
pipeline_lda = Pipeline(
[
('selector',SelectKBest(f_classif)),
('model',LinearDiscriminantAnalysis())
]
)
parameters_lda = {
'selector__k':[1,2,3,4,5],
"model__solver":['svd', 'lsqr', 'eigen']}
lda = model_selection.GridSearchCV(
estimator = pipeline_lda,
param_grid = parameters_lda,
n_jobs=-1,
scoring="f1_micro",
cv=5,
verbose=2
)
# Running models
models = (svc, lda)
parameters = (parameters_svc, parameters_lda)
acc = []
best_acc = 0
c =0
for p in models:
print("Performing grid search...")
p.fit(x_train, np.ravel(y_train, order="C"))
print("Best score: %0.3f" % p.best_score_)
print("Best parameters set:")
best_parameters = p.best_estimator_.get_params()
if p == svc:
for param_name in sorted(parameters_svc.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
else:
for param_name in sorted(parameters_lda.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
predicted = p.predict(x_test)
acc_mod = accuracy_score(y_test, predicted)
acc.append((p,acc_mod))
print("Accuracy:",accuracy_score(y_test, predicted))
print(precision_recall_fscore_support(y_test, predicted))
print(classification_report(y_test, predicted))
conf = confusion_matrix(y_test, predicted, labels=p.classes_)
disp2 = ConfusionMatrixDisplay(conf, display_labels=p.classes_)
disp2.plot()
if c == 0:
plt.title("Confusion Matrix della SVC y = char_rating")
c+=1
else:
plt.title("Confusion Matrix della LDA con y = char_rating")
plt.tight_layout()
plt.show()
for el in acc:
if el[1] > best_acc:
best_acc = el[1]
fav_mod = el[0]
else:
continue
# Printing the results
extract_model_informations(fav_mod, "char_rating", x_data)
# MODELS FOR Y = VADER POLARITY CHAR (GAME TO SENTIMENT AFTER)
df["delta_pol_vad"] = df.apply(lambda df: calc_delta(df['vader_polarity_before'], df['vader_polarity_after']),
axis=1)
df["char_delta_pol_vad"] = df["delta_pol_vad"].apply(level_polarity)
x_data2 = df[["vader_polarity_before","rating", "goal", "minutes", "assist"]]
y_data2 = df[["char_delta_pol_vad"]]
x_train2, x_test2, y_train2, y_test2 = model_selection.train_test_split(
x_data2, y_data2, test_size=0.33, random_state=45)
# Running models
acc = []
best_acc = 0
c = 0
for p in models:
print("Performing grid search...")
p.fit(x_train2, np.ravel(y_train2, order="C"))
print("Best score: %0.3f" % p.best_score_)
print("Best parameters set:")
best_parameters = p.best_estimator_.get_params()
if p == svc:
for param_name in sorted(parameters_svc.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
else:
for param_name in sorted(parameters_lda.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
predicted = p.predict(x_test2)
acc_mod = accuracy_score(y_test2, predicted)
acc.append((p,acc_mod))
print("Accuracy:",accuracy_score(y_test2, predicted))
print(precision_recall_fscore_support(y_test2, predicted))
print(classification_report(y_test2, predicted))
conf = confusion_matrix(y_test2, predicted, labels=p.classes_)
disp2 = ConfusionMatrixDisplay(conf, display_labels=p.classes_)
disp2.plot()
if c == 0:
plt.title("Confusion Matrix della SVC y = char_delta_pol_vad")
c+=1
else:
plt.title("Confusion Matrix della LDA con y = char_delta_pol_vad")
plt.tight_layout()
plt.show()
for el in acc:
if el[1] > best_acc:
best_acc = el[1]
fav_mod = el[0]
else:
continue
# Printing results
extract_model_informations(fav_mod, "char_delta_pol_vad", x_data2)
# REGRESSION MODELS
# Defining pipelines
pipeline_linreg = Pipeline(
[
('selector',SelectKBest(f_regression)),
('model',LinearRegression())
]
)
pipeline_XGBoost_reg = Pipeline(
[
('selector',SelectKBest(f_regression)),
('model',xgb.XGBRegressor())
]
)
parameters_lin = {
'selector__k':[1,2,3,4],
"model__positive":[True, False]
}
parameters_xgreg = {
'selector__k':[1,2,3,4]
}
print("\nregression")
lin = model_selection.GridSearchCV(
pipeline_linreg,
parameters_lin,
scoring="neg_mean_squared_error",
n_jobs=-1,
cv=5,
verbose=2
)
xgreg = model_selection.GridSearchCV(
pipeline_XGBoost_reg,
parameters_xgreg,
scoring="neg_mean_squared_error",
n_jobs=-1,
cv=5,
verbose=2
)
models_reg = [lin, xgreg]
parameters = (parameters_svc, parameters_lda)
y_data_reg = df[["delta_pol_vad"]]
x_data_reg = df[["vader_polarity_before","rating", "goal",
"assist", "minutes"]]
x_train_reg, x_test_reg, y_train_reg, y_test_reg = model_selection.train_test_split(
x_data_reg, y_data_reg, test_size=0.33, random_state=45)
# Running models
score = []
best_score = 1000
for p in models_reg:
print("Performing grid search...")
p.fit(x_train_reg,np.ravel(y_train_reg, order= "C"))
print("Best score: %0.3f" % p.best_score_)
print("Best parameters set:")
best_parameters = p.best_estimator_.get_params()
if p == lin:
for param_name in sorted(parameters_lin.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
else:
for param_name in sorted(parameters_xgreg.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))
predicted = p.predict(x_test_reg)
MSE = mean_squared_error(y_test_reg, predicted)
R2= r2_score(y_test_reg, predicted)
score.append((p,MSE, R2))
for el in score:
if el[1] < best_score:
best_score = el[1]
best_R2 = el[2]
fav_mod = el[0]
else:
continue
# Printing results
extract_model_informations_reg(fav_mod, "delta_pol_vad")
# END