-
Notifications
You must be signed in to change notification settings - Fork 0
/
laliga.py
289 lines (219 loc) · 13.5 KB
/
laliga.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
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import Dense , LSTM, Dropout
from scikeras.wrappers import KerasClassifier
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from itertools import product
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
############################################################### Nacitanie a uprava dat######################################################################
sezona1 = pd.read_csv("LaLiga_dataset/LaLiga_2014-2015.csv")
sezona2 = pd.read_csv("LaLiga_dataset/LaLiga_2015-2016.csv")
sezona3 = pd.read_csv("LaLiga_dataset/LaLiga_2016-2017.csv")
sezona4 = pd.read_csv("LaLiga_dataset/LaLiga_2017-2018.csv")
sezona5 = pd.read_csv("LaLiga_dataset/LaLiga_2018-2019.csv")
sezona6 = pd.read_csv("LaLiga_dataset/LaLiga_2019-2020.csv")
sezona7 = pd.read_csv("LaLiga_dataset/LaLiga_2020-2021.csv")
sezona8 = pd.read_csv("LaLiga_dataset/LaLiga_2021-2022.csv")
# Spojenie vsetkych sezón do jednej tabulky
vsetky_sezony = pd.concat([sezona1, sezona2, sezona3, sezona4, sezona5, sezona6, sezona7, sezona8], ignore_index=True)
selected_columns = ['HomeTeam', 'AwayTeam', 'FTR', 'HS', 'AS', 'HST', 'AST', 'HF', 'AF', 'HC', 'AC', 'HY', 'AY']
vsetky_sezony = vsetky_sezony[selected_columns]
X = vsetky_sezony.drop('FTR', axis=1)
y = vsetky_sezony['FTR']
print(f"V celej datovej sade LaLiga sa nachadza: Vyhry: {sum(y == 'H')}, Prehry: {sum(y == 'A')}, Remizy: {sum(y == 'D')}")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Rozdelenie dat na trenovaciu a testovaciu mnozinu
print(f"V testovacej sade LaLiga sa nachadza: Vyhry: {sum(y_test == 'H')}, Prehry: {sum(y_test == 'A')}, Remizy: {sum(y_test == 'D')}")
# Pouzivam LabelEncoder na zakodovanie vysledkov do ciselnej podoby
label_encoder = LabelEncoder()
y_train = label_encoder.fit_transform(y_train)
y_test = label_encoder.transform(y_test)
# Pouzivam ColumnTransformer na zakodovanie kategorickych atributov a normalizaciu ciselnych atributov
ct = ColumnTransformer(
transformers=[
('onehot', OneHotEncoder(), ['HomeTeam', 'AwayTeam']),
('scaler', StandardScaler(), ['HS', 'AS', 'HST', 'AST', 'HF', 'AF', 'HC', 'AC', 'HY', 'AY'])
],
remainder='passthrough'
)
X_train_encoded = ct.fit_transform(X_train)
X_test_encoded = ct.transform(X_test)
############################################################### Jednovrstvovy perceptron KERAS ######################################################################
def single_layer_perceptron_keras(X_train_encoded, y_train, X_test_encoded, y_test):
def create_model(optimizer='adam', activation='relu'):
model = Sequential()
model.add(Dense(3, input_dim=X_train_encoded.shape[1], activation=activation))
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
param_grid = {
'model__optimizer': ['adam', 'Adadelta', 'SGD', 'rmsprop'],
'model__activation': ['softmax', 'sigmoid', 'relu', 'tanh'],
'epochs': [15],
'batch_size': [32]
}
model = KerasClassifier(build_fn=create_model, verbose=0)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train_encoded, y_train)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_encoded)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(best_model, X_train_encoded, y_train, cv=5, scoring='accuracy')
print()
print('---------------------------------------------[ Jednovrstvovy perceptron KERAS ]---------------------------------------------')
print("Pocet kombinacii: ", len(list(product(*param_grid.values()))))
print('Najlepsie parametre modelu: ', grid_search.best_params_)
print("Cross-validacia skore: ", cv_scores)
print(f"Priemerna presnost modelu pri krizovej validacii: {cv_scores.mean()*100:.3f}%")
print(f"Rozpyl presnosti modelu pri krizovej validacii: ± {cv_scores.std()*100:.3f}%")
print(f"Presnost modelu na testovacej sade: {accuracy*100:.3f}%")
print('--------')
print(f"Predikovane vysledky: Vyhry: {sum(y_pred == 2)}, Prehry: {sum(y_pred == 0)}, Remizy: {sum(y_pred == 1)}")
print(f"Predikovane spravne vysledky: Vyhry: {sum((y_pred == y_test) & (y_test == 2))}, Prehry: {sum((y_pred == y_test) & (y_test == 0))}, Remizy: {sum((y_pred == y_test) & (y_test == 1))}")
############################################################### Jednovrstvovy perceptron MLPC Scikit ######################################################################
def single_layer_perceptron_mlpc(X_train_encoded, y_train, X_test_encoded, y_test):
param_grid = {
'activation': ['logistic', 'tanh', 'relu'],
'solver': ['lbfgs', 'sgd', 'adam'],
'hidden_layer_sizes': [10, 20, 30 , 50, 100],
'max_iter': [10, 50, 100],
'batch_size': [32],
}
model = MLPClassifier()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train_encoded, y_train)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_encoded)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(best_model, X_train_encoded, y_train, cv=5, scoring='accuracy')
print()
print('---------------------------------------------[ Jednovrstvovy perceptron MLPC Scikit ]---------------------------------------------')
print("Pocet kombinacii: ", len(list(product(*param_grid.values()))))
print('Najlepsie parametre modelu: ', grid_search.best_params_)
print("Cross-validacia skore: ", cv_scores)
print(f"Priemerna presnost modelu pri krizovej validacii: {cv_scores.mean()*100:.3f}%")
print(f"Rozpyl presnosti modelu pri krizovej validacii: ± {cv_scores.std()*100:.3f}%")
print(f"Presnost modelu na testovacej sade: {accuracy*100:.3f}%")
print('--------')
print(f"Predikovane vysledky: Vyhry: {sum(y_pred == 2)}, Prehry: {sum(y_pred == 0)}, Remizy: {sum(y_pred == 1)}")
print(f"Predikovane spravne vysledky: Vyhry: {sum((y_pred == y_test) & (y_test == 2))}, Prehry: {sum((y_pred == y_test) & (y_test == 0))}, Remizy: {sum((y_pred == y_test) & (y_test == 1))}")
############################################################### Viacvrstvovy perceptron KERAS ######################################################################
def mlp(X_train_encoded, y_train, X_test_encoded, y_test):
def create_model(optimizer='adam', activation1='relu', activation2='relu', activation3='relu'):
model = Sequential()
model.add(Dense(74, input_dim= X_train_encoded.shape[1], activation=activation1))
model.add(Dense(32, activation=activation2))
model.add(Dense(3, activation=activation3))
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
param_grid = {
'model__activation1': ['relu', 'tanh', 'sigmoid', 'softmax'],
'model__activation2': ['relu', 'tanh','sigmoid', 'softmax'],
'model__activation3': ['relu', 'tanh', 'sigmoid', 'softmax'],
'model__optimizer': ['adam', 'sgd', 'rmsprop', 'Adadelta'],
'epochs': [10],
'batch_size': [32],
}
model = KerasClassifier(build_fn=create_model, verbose=0)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train_encoded, y_train)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_encoded)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(best_model, X_train_encoded, y_train, cv=5, scoring='accuracy')
print()
print('---------------------------------------------[ Viacvrstvovy perceptron KERAS ]---------------------------------------------')
print("Pocet kombinacii: ", len(list(product(*param_grid.values()))))
print('Najlepsie parametre modelu: ', grid_search.best_params_)
print("Cross-validacia skore: ", cv_scores)
print(f"Priemerna presnost modelu pri krizovej validacii: {cv_scores.mean()*100:.3f}%")
print(f"Rozpyl presnosti modelu pri krizovej validacii: ± {cv_scores.std()*100:.3f}%")
print(f"Presnost modelu na testovacej sade: {accuracy*100:.3f}%")
print('--------')
print(f"Predikovane vysledky: Vyhry: {sum(y_pred == 2)}, Prehry: {sum(y_pred == 0)}, Remizy: {sum(y_pred == 1)}")
print(f"Predikovane spravne vysledky: Vyhry: {sum((y_pred == y_test) & (y_test == 2))}, Prehry: {sum((y_pred == y_test) & (y_test == 0))}, Remizy: {sum((y_pred == y_test) & (y_test == 1))}")
############################################################### Viacvrstvovy perceptron MLPC Scikit ######################################################################
def mlpc(X_train_encoded, y_train, X_test_encoded, y_test):
param_grid = {
'activation': ['relu', 'tanh', 'logistic'],
'solver': ['adam', 'sgd', 'lbfgs'],
'hidden_layer_sizes': [
(100, 50, 3),
(80, 40, 3),
(120, 60, 3),
(150, 75, 3),
(90, 45, 3),
(110, 55, 3),
(130, 65, 3),
(140, 70, 3),
],
'max_iter': [50, 100],
'batch_size': [32],
}
model = MLPClassifier()
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train_encoded, y_train)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_encoded)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(best_model, X_train_encoded, y_train, cv=5, scoring='accuracy')
print()
print('---------------------------------------------[ Viacvrstvovy perceptron MLPC Scikit ]---------------------------------------------')
print("Pocet kombinacii: ", len(list(product(*param_grid.values()))))
print('Najlepsie parametre modelu: ', grid_search.best_params_)
print("Cross-validacia skore: ", cv_scores)
print(f"Priemerna presnost modelu pri krizovej validacii: {cv_scores.mean()*100:.3f}%")
print(f"Rozpyl presnosti modelu pri krizovej validacii: ± {cv_scores.std()*100:.3f}%")
print(f"Presnost modelu na testovacej sade: {accuracy*100:.3f}%")
print('--------')
print(f"Predikovane vysledky: Vyhry: {sum(y_pred == 2)}, Prehry: {sum(y_pred == 0)}, Remizy: {sum(y_pred == 1)}")
print(f"Predikovane spravne vysledky: Vyhry: {sum((y_pred == y_test) & (y_test == 2))}, Prehry: {sum((y_pred == y_test) & (y_test == 0))}, Remizy: {sum((y_pred == y_test) & (y_test == 1))}")
############################################################### LSTM KERAS ######################################################################
def lstm_in_keras(X_train_encoded, y_train, X_test_encoded, y_test):
def create_model(optimizer='adam', dropout_rate=0.2, activation='relu'):
model = Sequential()
model.add(LSTM(74, input_shape=(X_train_encoded.shape[1], 1), return_sequences=False))
model.add(Dropout(dropout_rate))
model.add(Dense(3, activation=activation))
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
X_train_dense = X_train_encoded.toarray()
X_test_dense = X_test_encoded.toarray()
param_grid = {
'model__activation': ['sigmoid', 'softmax', 'tanh', 'relu'],
'model__optimizer': ['adam', 'sgd', 'rmsprop'],
'model__dropout_rate': [0.2],
'epochs': [8,10,15],
'batch_size': [32],
}
model = KerasClassifier(build_fn=create_model, verbose=0)
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)
grid_search.fit(X_train_dense, y_train)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test_dense)
accuracy = accuracy_score(y_test, y_pred)
cv_scores = cross_val_score(best_model, X_train_dense, y_train, cv=5, scoring='accuracy')
print()
print('---------------------------------------------[ LSTM KERAS ]---------------------------------------------')
print("Pocet kombinacii: ", len(list(product(*param_grid.values()))))
print('Najlepsie parametre modelu: ', grid_search.best_params_)
print("Cross-validacia skore: ", cv_scores)
print(f"Priemerna presnost modelu pri krizovej validacii: {cv_scores.mean()*100:.3f}%")
print(f"Rozpyl presnosti modelu pri krizovej validacii: ± {cv_scores.std()*100:.3f}%")
print(f"Presnost modelu na testovacej sade: {accuracy*100:.3f}%")
print('--------')
print(f"Predikovane vysledky: Vyhry: {sum(y_pred == 2)}, Prehry: {sum(y_pred == 0)}, Remizy: {sum(y_pred == 1)}")
print(f"Predikovane spravne vysledky: Vyhry: {sum((y_pred == y_test) & (y_test == 2))}, Prehry: {sum((y_pred == y_test) & (y_test == 0))}, Remizy: {sum((y_pred == y_test) & (y_test == 1))}")
single_layer_perceptron_keras(X_train_encoded, y_train, X_test_encoded, y_test)
single_layer_perceptron_mlpc(X_train_encoded, y_train, X_test_encoded, y_test)
mlp(X_train_encoded, y_train, X_test_encoded, y_test)
mlpc(X_train_encoded, y_train, X_test_encoded, y_test)
lstm_in_keras(X_train_encoded, y_train, X_test_encoded, y_test)