-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml.py
129 lines (93 loc) · 5.67 KB
/
ml.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
import service
import numpy as np
import matplotlib.pyplot as plt
from math import *
from sklearn.neural_network import MLPClassifier
def predictWinnerWithFixtureInfos(chosenChampionshipRound, teamName):
#filePath = service.FOLDER_PATH + str(chosenChampionshipRound) + ".json"
#championshipRoundsJSON = service.readJSONFile(filePath)
#inputs = extractInputsValuesFromMatches(championshipRoundsJSON["matches"])
#desired = extractDesiredValuesFromMatches(championshipRoundsJSON["matches"])
fixtureInfosToCompare = service.getTeamFixtureWithRoundAndTeamName(chosenChampionshipRound, teamName)
inputs, desired = service.combinedAllRoundsFound()
TEST_PERCENTAGE = 30
training_size = int(len(inputs) * (1.0 - TEST_PERCENTAGE / 100.0))
train_inputs, train_outputs = inputs[:training_size], desired[:training_size] #On coupe le jeu de données en 2
test_inputs, test_outputs = inputs[training_size:], desired[training_size:]
#mlp = MLPClassifier(solver='adam', max_iter=10000, learning_rate_init=0.048214, random_state=3, hidden_layer_sizes=(14), verbose=True, tol=0.0000001)
#mlp = MLPClassifier(solver='adam', max_iter=10000, learning_rate_init=0.06, hidden_layer_sizes=(12,6,3),
# random_state=3,verbose=True, tol=0.00001, activation='logistic')
#mlp = MLPClassifier(solver='adam', max_iter=10000, learning_rate_init=0.024183776,
# hidden_layer_sizes=(12,6,3), random_state=3, tol=0.00004, activation='logistic')
mlp = MLPClassifier(solver='adam', max_iter=10000, learning_rate_init=0.024183776,
hidden_layer_sizes=(8), random_state=3, tol=0.00004, activation='logistic')
trainedModel = mlp.fit(train_inputs, train_outputs)
## Evaluer sur l'ensemble d'apprentissage la qualité de mon modèle
#learning_score = mlp.score(train_inputs, train_outputs)
#print(f"#Score d'apprentissage : {round(learning_score * 100)}%")
## Evaluer sur l'ensemble de test la qualité de mon modèle
#learning_score = mlp.score(test_inputs, test_outputs)
#print(f"#Score de test : {round(learning_score * 100)}%")
# fig, ax = plt.subplots()
# ax.plot(mlp.loss_curve_)
# plt.yscale('log')
# plt.show()
inputsToCompare = extractInputsFromFixtureStats(fixtureInfosToCompare)
prediction = mlp.predict(np.array(inputsToCompare).reshape(1, -1))
prediction_odds = mlp.predict_proba(np.array(inputsToCompare).reshape(1, -1))
service.displayPrediction(prediction)
service.displayOdds(prediction_odds)
def extractDesiredValuesFromMatches(championshipRoundsMatches):
desired = []
for championshipRound in championshipRoundsMatches:
desired.append(championshipRound["winner"])
return desired
def extractDesiredValuesFromFixtureStats(fixtureStats):
return fixtureStats["winner"]
def extractInputsFromFixtureStats(fixtureStats):
# On ajoute les valeurs de "home" de notre JSON dans inputs
# On continue avec les valeurs de "away"
homeGoalsAvg = fixtureStats["home"]["goals_avg"]
homeGoalsAgainstAvg = fixtureStats["home"]["goals_against_avg"]
# On convertit les pourcentages en float
homeForme = convertPercentToFloat(fixtureStats["home"]["forme"])
homeAtt = convertPercentToFloat(fixtureStats["home"]["att"])
homeDef = convertPercentToFloat(fixtureStats["home"]["def"])
homeH2h = convertPercentToFloat(fixtureStats["home"]["h2h"])
awayGoalsAvg = fixtureStats["away"]["goals_avg"]
awayGoalsAgainstAvg = fixtureStats["away"]["goals_against_avg"]
# On convertit les pourcentages en float
awayForme = convertPercentToFloat(fixtureStats["home"]["forme"])
awayAtt = convertPercentToFloat(fixtureStats["home"]["att"])
awayDef = convertPercentToFloat(fixtureStats["home"]["def"])
awayH2h = convertPercentToFloat(fixtureStats["home"]["h2h"])
inputfixtureStats = [homeGoalsAvg, homeGoalsAgainstAvg, homeForme, homeAtt, homeDef, homeH2h, awayGoalsAvg, awayGoalsAgainstAvg, awayForme, awayAtt, awayDef, awayH2h]
return inputfixtureStats
def extractInputsValuesFromMatches(championshipRoundsMatches):
inputs = []
for championshipRound in championshipRoundsMatches:
# On ajoute les valeurs de "home" de notre JSON dans inputs
# On continue avec les valeurs de "away"
homeGoalsAvg = championshipRound["home"]["goals_avg"]
homeGoalsAgainstAvg = championshipRound["home"]["goals_against_avg"]
# On convertit les pourcentages en float
homeForme = convertPercentToFloat(championshipRound["home"]["forme"])
homeAtt = convertPercentToFloat(championshipRound["home"]["att"])
homeDef = convertPercentToFloat(championshipRound["home"]["def"])
homeH2h = convertPercentToFloat(championshipRound["home"]["h2h"])
awayGoalsAvg = championshipRound["away"]["goals_avg"]
awayGoalsAgainstAvg = championshipRound["away"]["goals_against_avg"]
# On convertit les pourcentages en float
awayForme = convertPercentToFloat(championshipRound["home"]["forme"])
awayAtt = convertPercentToFloat(championshipRound["home"]["att"])
awayDef = convertPercentToFloat(championshipRound["home"]["def"])
awayH2h = convertPercentToFloat(championshipRound["home"]["h2h"])
groupedInfo = [homeGoalsAvg, homeGoalsAgainstAvg, homeForme, homeAtt, homeDef, homeH2h, awayGoalsAvg, awayGoalsAgainstAvg, awayForme, awayAtt, awayDef, awayH2h]
inputs.append(groupedInfo)
return inputs
def convertPercentToFloat(valueToConvert):
valueToFloat = float(valueToConvert.strip('%'))/10
return round(valueToFloat, 2)
if __name__ == "__main__":
predictWinnerWithFixtureInfos(21, "Marseille")
# il faut commencer la prédiction