Compare commits

...

10 Commits

6 changed files with 1619 additions and 148 deletions

View File

@ -1,4 +1,4 @@
link_match : page link of match in Premier League Official website link_match : page link of match in Premier League Official websitemarkdow
season : match season season : match season
date : match date date : match date
home_team : home team home_team : home team

View File

@ -1,5 +1,17 @@
import pandas as pd import pandas as pd
from simpful import * from fuzzy import *
def save_to_csv(filename, dataframe):
dataframe.to_csv(filename, mode='a', index=False, header=not pd.DataFrame().append(dataframe).empty)
def split_to_parts(dataframe, part_size):
for i in range(0, len(dataframe), part_size):
yield dataframe.iloc[i:i + part_size]
def przetwarzaj_co_50_rekordow(plik_wejsciowy, plik_wyjsciowy):
dataframe_wejsciowe = pd.read_csv(plik_wejsciowy)
def generateTrainingData(dataframe): def generateTrainingData(dataframe):
columns = ['season','date','home_team','away_team','result_full','home_passes','away_passes', columns = ['season','date','home_team','away_team','result_full','home_passes','away_passes',
@ -10,25 +22,45 @@ def generateTrainingData(dataframe):
def generateFuzzyLogicData(dataframe): def generateFuzzyLogicData(dataframe):
columns = ['season','date','home_team','away_team','result_full','c_home_form_5m','c_away_form_5m',#,'c_home_passes','c_away_passes', columns = ['season','date','home_team','away_team','result_full','c_home_form_5m','c_away_form_5m',#,'c_home_passes','c_away_passes',
# 'c_home_possession','c_away_possession','c_home_shots','c_away_shots', # 'c_home_possession','c_away_possession','c_home_shots','c_away_shots',
'c_home_diff_5m', 'c_away_diff_5m','c_home_form_season','c_away_form_season', 'c_home_diff_5m', 'c_away_diff_5m',"c_home_form_5s",
'c_home_diff_season', 'c_away_diff_season'] 'c_away_form_5s','c_home_diff_5s','c_away_diff_5s'
, 'c_home_aggression_5m',
'c_away_aggression_5m', 'c_away_shots_5m','c_away_shots_5m',
'c_away_shots_5btw', 'c_away_shots_5btw', 'c_away_defence_5m',
'c_away_defence_5m', 'c_away_defence_5btw', 'c_away_defence_5btw',
'c_home_passing_5m', 'c_away_passing_5m', 'c_home_passing_5btw',
'c_away_passing_5btw', 'c_away_aggression_5btw', 'c_away_aggression_5btw'
#'c_home_aggression_season', 'c_away_aggression_season',
# 'c_home_form_season','c_away_form_season',
# 'c_home_diff_season', 'c_away_diff_season'
]
return dataframe[columns] return dataframe[columns]
def last5Matches(season, teamA, data, df): def last5Matches(season, teamA, data, df):
# Wybierz rekordy dla danej pary drużyn i sezonu
subset = df[((df['season'] == season) & ((df['home_team'] == teamA) | (df['away_team'] == teamA)))] subset = df[((df['season'] == season) & ((df['home_team'] == teamA) | (df['away_team'] == teamA)))]
# Filtruj dane, aby zawierały te przed daną datą
before_given_date = subset[pd.to_datetime(subset['date']) < pd.to_datetime(data)] before_given_date = subset[pd.to_datetime(subset['date']) < pd.to_datetime(data)]
# Posortuj wg daty w odwrotnej kolejności
before_given_date = before_given_date.sort_values(by='date', ascending=False) before_given_date = before_given_date.sort_values(by='date', ascending=False)
# Wybierz 5 ostatnich przed daną datą
last_before_date = before_given_date.head(5) last_before_date = before_given_date.head(5)
return last_before_date return last_before_date, "_5m"
def last5MatchesBtwTeams(teamA, teamB, data, df):
subset = df[(((df['home_team'] == teamA) | (df['away_team'] == teamA)) & ((df['home_team'] == teamB) | (df['away_team'] == teamB)))]
before_given_date = subset[pd.to_datetime(subset['date']) < pd.to_datetime(data)]
before_given_date = before_given_date.sort_values(by='date', ascending=False)
last_before_date = before_given_date.head(5)
return last_before_date, "_5btw"
def seasonMatches(season, teamA, data, df): def seasonMatches(season, teamA, data, df):
# Wybierz rekordy dla danej pary drużyn i sezonu # Wybierz rekordy dla danej pary drużyn i sezonu
@ -40,7 +72,7 @@ def seasonMatches(season, teamA, data, df):
# Posortuj wg daty w odwrotnej kolejności # Posortuj wg daty w odwrotnej kolejności
before_given_date = before_given_date.sort_values(by='date', ascending=False) before_given_date = before_given_date.sort_values(by='date', ascending=False)
return before_given_date return before_given_date, "_s"
def getResult(score,teamHome): def getResult(score,teamHome):
x,y = score.split('-') x,y = score.split('-')
@ -53,7 +85,24 @@ def getResult(score,teamHome):
return "draw" return "draw"
else: else:
return "loss" return "loss"
# def calculateAggression(matches, team):
# aggression = 0
# for index, row in matches.iterrows():
# if team == row['home_team']:
# yellow_cards = row['home_yellow_cards']
# red_cards = row['home_red_cards']
# else:
# yellow_cards = row['away_yellow_cards']
# red_cards = row['away_red_cards']
# aggression_result = calculateFuzzyAggression(yellow_cards, red_cards)
# #print(aggression_result['aggression'])
# aggression = aggression + aggression_result['aggression']
# if matches.shape[0] != 0:
# aggression_avg = aggression / matches.shape[0]
# else:
# aggression_avg = 0
# return aggression_avg
def calculatePoints(matches, team): def calculatePoints(matches, team):
points = 0 points = 0
@ -90,3 +139,18 @@ def calculateGoalDifference(matches, team):
else: else:
goal_diff = goal_diff + (y-x) goal_diff = goal_diff + (y-x)
return goal_diff return goal_diff
def calculateColumn(matches, team, column_name):
result = 0
for index, row in matches.iterrows():
if team == row['home_team']:
column = row[column_name]
else:
column = row[column_name]
result = result + column
if matches.shape[0] != 0:
result_avg = result / matches.shape[0]
else:
result_avg = 0
return result_avg

1032
df_parts.csv Normal file

File diff suppressed because it is too large Load Diff

222
fuzzy.py Normal file
View File

@ -0,0 +1,222 @@
from simpful import *
from data_filters import *
import pandas as pd
FS = FuzzySystem()
# Dominacja OK
# Jakość strzałów - Witek
# Agresesywnosc (zolte + czerwone kartki) - Wojtek
# odbiory i wslizgi (xDef) - Michał, ekspert od xDef
# statystyki z calego sezonu - Wojtek OK
# 5 ostatnich spotkan miedzy druzynami - Witek
def categorizeFuzzyPasses(passes,possession):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Pass1 = TriangleFuzzySet(300,300,600, term="low")
Pass2 = TriangleFuzzySet(110,440,770, term="average")
Pass3 = TriangleFuzzySet(200,600,600, term="high")
FS.add_linguistic_variable("passes", LinguisticVariable([Pass1, Pass2, Pass3], universe_of_discourse=[0,1000]))
Poss1 = TriangleFuzzySet(30,30,50, term="low")
Poss2 = TriangleFuzzySet(35,50,65, term="average")
Poss3 = TriangleFuzzySet(50,70,70, term="high")
FS.add_linguistic_variable("possession", LinguisticVariable([Poss1, Poss2, Poss3], universe_of_discourse=[0,100]))
#Pass_domination1 = TriangleFuzzySet(2,2,6, term="low")
#Pass_domination2 = TriangleFuzzySet(3,5,7, term="average")
#Pass_domination3 = TriangleFuzzySet(4,8,8, term="high")
#FS.add_linguistic_variable("passes_domination", LinguisticVariable([Pass_domination1, Pass_domination2, Pass_domination3], universe_of_discourse=[0,10]))
FS.add_rules([
"IF (passes IS low) AND (possession IS low) THEN (pass_domination IS low)",
"IF (passes IS high) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS average) AND (possession IS average) THEN (pass_domination IS average)",
"IF (passes IS low) AND (possession IS high) THEN (pass_domination IS average)",
"IF (passes IS high ) AND (possession IS low) THEN (pass_domination IS average)",
"IF (passes IS average) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS high) AND (possession IS average) THEN (pass_domination IS high)",
"IF (passes IS low) AND (possession IS average) THEN (pass_domination IS low)",
"IF (passes IS average) AND (possession IS low) THEN (pass_domination IS average)"
])
FS.set_variable("passes", passes)
FS.set_variable("possession", possession)
pass_domination = FS.inference()
return pass_domination['pass_domination']
def categorizeFuzzyDefence(tackles,clearances):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Tackle1 = TriangleFuzzySet(12,12,24, term="low")
Tackle2 = TriangleFuzzySet(6,18,30, term="average")
Tackle3 = TriangleFuzzySet(12,24,24, term="high")
FS.add_linguistic_variable("tackles", LinguisticVariable([Tackle1, Tackle2, Tackle3], universe_of_discourse=[0,80]))
Clear1 = TriangleFuzzySet(12,12,36, term="low")
Clear2 = TriangleFuzzySet(9,27,45, term="average")
Clear3 = TriangleFuzzySet(18,42,42, term="high")
FS.add_linguistic_variable("clearances", LinguisticVariable([Clear1, Clear2, Clear3], universe_of_discourse=[0,80]))
FS.add_rules([
"IF (tackles IS low) AND (clearances IS low) THEN (defence_actions IS low)",
"IF (tackles IS high) AND (clearances IS high) THEN (defence_actions IS high)",
"IF (tackles IS average) AND (clearances IS average) THEN (defence_actions IS average)",
"IF (tackles IS low) AND (clearances IS high) THEN (defence_actions IS average)",
"IF (tackles IS high ) AND (clearances IS low) THEN (defence_actions IS average)",
"IF (tackles IS average) AND (clearances IS high) THEN (defence_actions IS high)",
"IF (tackles IS high) AND (clearances IS average) THEN (defence_actions IS high)",
"IF (tackles IS low) AND (clearances IS average) THEN (defence_actions IS low)",
"IF (tackles IS average) AND (clearances IS low) THEN (defence_actions IS average)"
])
FS.set_variable("tackles", tackles)
FS.set_variable("clearances", clearances)
defence_actions = FS.inference()
return defence_actions['defence_actions']
def categorizeFuzzyShots(shots_overall, shots_on_target):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Shot_ov1 = TriangleFuzzySet(3,3,12, term="low") #pozmieniać przedziały (nakładają się)
Shot_ov2 = TriangleFuzzySet(2,12,22, term="average")
Shot_ov3 = TriangleFuzzySet(6,25,25, term="high")
FS.add_linguistic_variable("shots_overall", LinguisticVariable([Shot_ov1, Shot_ov2, Shot_ov3], universe_of_discourse=[0,35]))
Shot_ont1 = TriangleFuzzySet(1,1,6, term="low")
Shot_ont2 = TriangleFuzzySet(2,5,8, term="average")
Shot_ont3 = TriangleFuzzySet(3,10,10, term="high")
FS.add_linguistic_variable("shots_on_target", LinguisticVariable([Shot_ont1, Shot_ont2, Shot_ont3], universe_of_discourse=[0,15]))
#Qual_of_shots1 = TriangleFuzzySet(0,0,0.3, term="low")
#Qual_of_shots2 = TriangleFuzzySet(0.2,0.5,0.8, term="medium")
#Qual_of_shots3 = TriangleFuzzySet(0.7,1,1, term="high")
#FS.add_linguistic_variable("expected_goals", LinguisticVariable([Qual_of_shots1, Qual_of_shots2, Qual_of_shots3], universe_of_discourse=[0,1]))
FS.add_rules([
"IF (shots_overall IS low) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS high) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS average) AND (shots_on_target IS average) THEN (quality_of_shots IS average)",
"IF (shots_overall IS low) AND (shots_on_target IS high) THEN (quality_of_shots IS average)",
"IF (shots_overall IS high ) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS average) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS high) AND (shots_on_target IS average) THEN (quality_of_shots IS average)",
"IF (shots_overall IS low) AND (shots_on_target IS average) THEN (quality_of_shots IS average)",
"IF (shots_overall IS average) AND (shots_on_target IS low) THEN (quality_of_shots IS low)"
])
FS.set_variable("shots_overall", shots_overall)
FS.set_variable("shots_on_target", shots_on_target)
quality_of_shots = FS.inference()
return quality_of_shots['quality_of_shots']
def calculateFuzzyAggression(yellow_cards, red_cards):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Yellow_cards1 = TriangleFuzzySet(0, 2, 4, term="low")
Yellow_cards2 = TriangleFuzzySet(1, 3, 5, term="average")
Yellow_cards3 = TriangleFuzzySet(2, 4, 4, term="high")
FS.add_linguistic_variable("yellow_cards", LinguisticVariable([Yellow_cards1, Yellow_cards2, Yellow_cards3], universe_of_discourse=[0, 10]))
Red_cards1 = TriangleFuzzySet(0, 0, 1, term="low")
Red_cards2 = TriangleFuzzySet(0, 1, 2, term="average")
Red_cards3 = TriangleFuzzySet(1, 2, 2, term="high")
FS.add_linguistic_variable("red_cards", LinguisticVariable([Red_cards1, Red_cards2, Red_cards3], universe_of_discourse=[0, 4]))
# Pass_domination1 = TriangleFuzzySet(2,2,6, term="low")
# Pass_domination2 = TriangleFuzzySet(3,5,7, term="average")
# Pass_domination3 = TriangleFuzzySet(4,8,8, term="high")
# FS.add_linguistic_variable("passes_domination", LinguisticVariable([Pass_domination1, Pass_domination2, Pass_domination3], universe_of_discourse=[0,10]))
FS.add_rules([
"IF (yellow_cards IS low) AND (red_cards IS low) THEN (aggression IS low)",
"IF (yellow_cards IS high) AND (red_cards IS high) THEN (aggression IS high)",
"IF (yellow_cards IS average) AND (red_cards IS average) THEN (aggression IS average)",
"IF (yellow_cards IS low) AND (red_cards IS high) THEN (aggression IS high)",
"IF (yellow_cards IS high ) AND (red_cards IS low) THEN (aggression IS high)",
"IF (yellow_cards IS average) AND (red_cards IS high) THEN (aggression IS high)",
"IF (yellow_cards IS high) AND (red_cards IS average) THEN (aggression IS high)",
"IF (yellow_cards IS low) AND (red_cards IS average) THEN (aggression IS average)",
"IF (yellow_cards IS average) AND (red_cards IS low) THEN (aggression IS average)"
])
FS.set_variable("yellow_cards", yellow_cards)
FS.set_variable("red_cards", red_cards)
aggression = FS.inference()
return aggression['aggression']

205
main.py
View File

@ -2,13 +2,13 @@ import pandas as pd
from simpful import * from simpful import *
from rules import * from rules import *
from data_filters import * from data_filters import *
from fuzzy import *
from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report from sklearn.metrics import classification_report
from sklearn.ensemble import GradientBoostingClassifier
# Ostatnie 5 spotkań # Ostatnie 5 spotkań
@ -22,18 +22,25 @@ from sklearn.metrics import classification_report
if __name__ == "__main__": if __name__ == "__main__":
df = pd.read_csv('df_full_premierleague.csv')
result = last5Matches('10/11', 'Stoke City', '2010-10-02', df) df = pd.read_csv('df_parts.csv')
'''
df = pd.read_csv('df_full_premierleague.csv')
result = last5Matches('10/11', 'Stoke City', '2010-10-02', df)[0]
#print(result.to_markdown()) #print(result.to_markdown())
#print(result) #print(result)
result = last5Matches('10/11', 'Blackburn Rovers', '2010-10-02', df) result = last5Matches('10/11', 'Blackburn Rovers', '2010-10-02', df)[0]
#print(result.to_markdown()) #print(result.to_markdown())
#print(result) #print(result)
print(calculatePoints(result,'Blackburn Rovers')) print(calculatePoints(result,'Blackburn Rovers'))
print(calculateGoalDifference(result, 'Blackburn Rovers')) print(calculateGoalDifference(result, 'Blackburn Rovers'))
# df = generateTrainingData(df) # df = generateTrainingData(df)
# df = add_column(df, categorize_passes, "c_away_passes", "away_passes") # df = add_column(df, categorize_passes, "c_away_passes", "away_passes")
# df = add_column(df, categorize_passes, "c_home_passes", "home_passes") # df = add_column(df, categorize_passes, "c_home_passes", "home_passes")
@ -44,19 +51,160 @@ if __name__ == "__main__":
# df = add_column(df, categorize_shots, "c_away_shots", "away_shots") # df = add_column(df, categorize_shots, "c_away_shots", "away_shots")
# df = add_column(df, categorize_shots, "c_home_shots", "home_shots") # df = add_column(df, categorize_shots, "c_home_shots", "home_shots")
# print(df.columns) # print(df.columns)
###############################################################################################
# df = add_column(df, getColumnMethod(df, True, 'home_yellow_cards', seasonMatches), "c_home_yellow_cards_s")
# df = add_column(df, getColumnMethod(df, True, 'away_yellow_cards', seasonMatches), "c_away_yellow_cards_s")
# df = add_column(df, getColumnMethod(df, True, 'home_red_cards', seasonMatches), "c_home_red_cards_s")
# df = add_column(df, getColumnMethod(df, True, 'away_red_cards', seasonMatches), "c_away_red_cards_s")
df = df.sort_values(by='date', ascending=False)
df = add_column(df, getColumnMethod(df, True, 'home_yellow_cards', last5Matches), "c_home_yellow_cards_5m")
df = add_column(df, getColumnMethod(df, False, 'away_yellow_cards', last5Matches), "c_away_yellow_cards_5m")
df = add_column(df, getColumnMethod(df, True, 'home_red_cards', last5Matches), "c_home_red_cards_5m")
df = add_column(df, getColumnMethod(df, False, 'away_red_cards', last5Matches), "c_away_red_cards_5m")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_yellow_cards', last5MatchesBtwTeams),
"c_home_yellow_cards_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_yellow_cards', last5MatchesBtwTeams),
"c_away_yellow_cards_5btw")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_red_cards', last5MatchesBtwTeams), "c_home_red_cards_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_red_cards', last5MatchesBtwTeams), "c_away_red_cards_5btw")
###################################################################################################################
# df = add_column(df, getColumnMethod(df, True, 'home_shots', seasonMatches), "c_home_shots_s")
# df = add_column(df, getColumnMethod(df, True, 'away_shots', seasonMatches), "c_away_shots_s")
# df = add_column(df, getColumnMethod(df, True, 'home_shots_on_target', seasonMatches), "c_home_shots_on_target_s")
# df = add_column(df, getColumnMethod(df, True, 'away_shots_on_target', seasonMatches), "c_away_shots_on_target_s")
df = add_column(df, getColumnMethod(df, True, 'home_shots', last5Matches), "c_home_shots_5m")
df = add_column(df, getColumnMethod(df, False, 'away_shots', last5Matches), "c_away_shots_5m")
df = add_column(df, getColumnMethod(df, True, 'home_shots_on_target', last5Matches), "c_home_shots_on_target_5m")
df = add_column(df, getColumnMethod(df, False, 'away_shots_on_target', last5Matches), "c_away_shots_on_target_5m")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_shots', last5MatchesBtwTeams), "c_home_shots")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_shots', last5MatchesBtwTeams), "c_away_shots")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_shots_on_target', last5MatchesBtwTeams),
"c_home_shots_on_target_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_shots_on_target', last5MatchesBtwTeams),
"c_away_shots_on_target_5btw")
###################################################################################################################################
# df = add_column(df, getColumnMethod(df, True, 'home_tackles', seasonMatches), "c_home_tackles_s")
# df = add_column(df, getColumnMethod(df, True, 'away_tackles', seasonMatches), "c_away_tackles_s")
# df = add_column(df, getColumnMethod(df, True, 'home_clearances', seasonMatches), "c_home_clearances_s")
# df = add_column(df, getColumnMethod(df, True, 'away_clearances', seasonMatches), "c_away_clearances_s")
df = add_column(df, getColumnMethod(df, True, 'home_tackles', last5Matches), "c_home_tackles_5m")
df = add_column(df, getColumnMethod(df, False, 'away_tackles', last5Matches), "c_away_tackles_5m")
df = add_column(df, getColumnMethod(df, True, 'home_clearances', last5Matches), "c_home_clearances_5m")
df = add_column(df, getColumnMethod(df, False, 'away_clearances', last5Matches), "c_away_clearances_5m")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_tackles', last5MatchesBtwTeams), "c_home_tackles_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_tackles', last5MatchesBtwTeams), "c_away_tackles_5btw")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_clearances', last5MatchesBtwTeams),
"c_home_clearances_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_clearances', last5MatchesBtwTeams),
"c_away_clearances_5btw")
####################################################################################################################################
# df = add_column(df, getColumnMethod(df, True, 'home_passes', seasonMatches), "c_home_passes_s")
# df = add_column(df, getColumnMethod(df, True, 'away_passes', seasonMatches), "c_away_passes_s")
# df = add_column(df, getColumnMethod(df, True, 'home_possession', seasonMatches), "c_home_possession_s")
# df = add_column(df, getColumnMethod(df, True, 'away_possession', seasonMatches), "c_away_possession_s")
df = add_column(df, getColumnMethod(df, True, 'home_passes', last5Matches), "c_home_passes_5m")
df = add_column(df, getColumnMethod(df, False, 'away_passes', last5Matches), "c_away_passes_5m")
df = add_column(df, getColumnMethod(df, True, 'home_possession', last5Matches), "c_home_possession_5m")
df = add_column(df, getColumnMethod(df, False, 'away_possession', last5Matches), "c_away_possession_5m")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_passes', last5MatchesBtwTeams), "c_home_passes_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_passes', last5MatchesBtwTeams), "c_away_passes_5btw")
df = add_column(df, getColumnMethod5Btw(df, True, 'home_possession', last5MatchesBtwTeams),
"c_home_possession_5btw")
df = add_column(df, getColumnMethod5Btw(df, False, 'away_possession', last5MatchesBtwTeams),
"c_away_possession_5btw")
# TU
##########################################################################################################################################
df = add_column(df, get_method(df, True, categorize_points, last5Matches), "c_home_form_5m") df = add_column(df, get_method(df, True, categorize_points, last5Matches), "c_home_form_5m")
df = add_column(df, get_method(df, False, categorize_points, last5Matches), "c_away_form_5m") df = add_column(df, get_method(df, False, categorize_points, last5Matches), "c_away_form_5m")
df = add_column(df, get_method(df, True, categorize_diff, last5Matches), "c_home_diff_5m")#categorize_diff df = add_column(df, get_method(df, True, categorize_diff, seasonMatches), "c_home_diff_5m")
df = add_column(df, get_method(df, False, categorize_diff,last5Matches), "c_away_diff_5m") df = add_column(df, get_method(df, False, categorize_diff, seasonMatches), "c_away_diff_5m")
df = add_column(df, get_method(df, True, categorize_points, seasonMatches), "c_home_form_season") df = add_column(df, get_method(df, True, categorize_points_Btw, last5MatchesBtwTeams), "c_home_form_5btw")
df = add_column(df, get_method(df, False, categorize_points, seasonMatches), "c_away_form_season") df = add_column(df, get_method(df, False, categorize_points_Btw, last5MatchesBtwTeams), "c_away_form_5btw")
df = add_column(df, get_method(df, True, categorize_diff, seasonMatches), "c_home_diff_season")#categorize_diff df = add_column(df, get_method(df, True, categorize_diff_Btw, last5MatchesBtwTeams), "c_home_diff_5btw")
df = add_column(df, get_method(df, False, categorize_diff,seasonMatches), "c_away_diff_season") df = add_column(df, get_method(df, False, categorize_diff_Btw, last5MatchesBtwTeams), "c_away_diff_5btw")
df = add_column(df, get_method(df, True, categorize_points, seasonMatches), "c_home_form_5s")
df = add_column(df, get_method(df, False, categorize_points, seasonMatches), "c_away_form_5s")
df = add_column(df, get_method(df, True, categorize_diff, seasonMatches), "c_home_diff_5s")
df = add_column(df, get_method(df, False, categorize_diff, seasonMatches), "c_away_diff_5s")
df.to_csv('df.csv', index=False)
#TU sie zapisuje zbior
part_size = 50
for part in split_to_parts(df, part_size):
part = add_column(part,
getFuzzyMethod(part, calculateFuzzyAggression, True, "c_home_yellow_cards_5m", "c_home_red_cards_5m"),
"c_home_aggression_5m")
part = add_column(part, getFuzzyMethod(part, calculateFuzzyAggression, False, "c_away_yellow_cards_5m",
"c_away_red_cards_5m"), "c_away_aggression_5m")
part = add_column(part, getFuzzyMethod(part, calculateFuzzyAggression, True, "c_home_yellow_cards_5btw",
"c_home_red_cards_5btw"), "c_home_aggression_5btw")
part = add_column(part, getFuzzyMethod(part, calculateFuzzyAggression, False, "c_away_yellow_cards_5btw",
"c_away_red_cards_5btw"), "c_away_aggression_5btw")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyShots, True, "c_home_shots_5m", "c_home_shots_5m"),
"c_home_shots_5m")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyShots, False, "c_away_shots_5m", "c_away_shots_5m"),
"c_away_shots_5m")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyShots, True, "c_home_shots_on_target_5btw",
"c_home_shots_on_target_5btw"), "c_home_shots_5btw")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyShots, False, "c_away_shots_on_target_5btw",
"c_away_shots_on_target_5btw"), "c_away_shots_5btw")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyDefence, True, "c_home_tackles_5m", "c_home_clearances_5m"),
"c_home_defence_5m")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyDefence, False, "c_away_tackles_5m", "c_away_clearances_5m"),
"c_away_defence_5m")
part = add_column(part,
getFuzzyMethod(part, categorizeFuzzyDefence, True, "c_home_tackles_5btw", "c_home_clearances_5btw"),
"c_home_defence_5btw")
part = add_column(part,
getFuzzyMethod(part, categorizeFuzzyDefence, False, "c_away_tackles_5btw", "c_away_clearances_5btw"),
"c_away_defence_5btw")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyPasses, True, "c_home_passes_5m", "c_home_possession_5m"),
"c_home_passing_5m")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyPasses, False, "c_away_passes_5m", "c_away_possession_5m"),
"c_away_passing_5m")
part = add_column(part, getFuzzyMethod(part, categorizeFuzzyPasses, True, "c_home_passes_5btw", "c_home_possession_5btw"),
"c_home_passing_5btw")
part = add_column(part,
getFuzzyMethod(part, categorizeFuzzyPasses, False, "c_away_passes_5btw", "c_away_possession_5btw"),
"c_away_passing_5btw")
save_to_csv("df_parts", part)
'''
df = generateFuzzyLogicData(df) df = generateFuzzyLogicData(df)
label_encoder = LabelEncoder() label_encoder = LabelEncoder()
@ -68,9 +216,12 @@ if __name__ == "__main__":
df[['home_team', 'away_team']] = temp.unstack() df[['home_team', 'away_team']] = temp.unstack()
X = df.drop(['result_full', 'date', 'c_home_result', 'c_away_result'], axis=1) X = df.drop(['result_full', 'date', 'c_home_result', 'c_away_result'], axis=1)
y = df['c_home_result'] y = df['c_home_result']
#y = label_encoder.fit_transform(df['c_home_result'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42) model = RandomForestClassifier(n_estimators=500, random_state=42)
#model = GradientBoostingClassifier(learning_rate=0.1, n_estimators=100, random_state = 42)
model.fit(X_train, y_train) model.fit(X_train, y_train)
y_pred = model.predict(X_test) y_pred = model.predict(X_test)
@ -81,9 +232,29 @@ if __name__ == "__main__":
accuracy = accuracy_score(y_test, y_pred) accuracy = accuracy_score(y_test, y_pred)
print(f'Dokładność modelu: {accuracy}') print(f'Dokładność modelu: {accuracy}')
print(classification_report(y_test, y_pred)) print(classification_report(y_test, y_pred))
result = last5Matches('10/11', 'Manchester United', '2010-12-16', df)
print(calculatePoints(result,'Manchester United'))
print(calculateGoalDifference(result, 'Manchester United'))
print(categorize_fuzzy_passes(450,50)) #print(model.feature_importances_)
#print(categorize_fuzzy_passes(450,50))
#df.to_csv('df.csv', index=False)
# df = add_column(df, get_method(df, True, categorize_aggression, last5Matches), "c_home_aggression_5m")#categorize_diff
# df = add_column(df, get_method(df, False, categorize_aggression,last5Matches), "c_away_aggression_5m")
# df = add_column(df, get_method(df, True, categorize_points, seasonMatches), "c_home_form_season")
# df = add_column(df, get_method(df, False, categorize_points, seasonMatches), "c_away_form_season")
#df = add_column(df, get_method(df, True, categorize_diff, seasonMatches), "c_home_diff_season")#categorize_diff
# df = add_column(df, get_method(df, False, categorize_diff,seasonMatches), "c_away_diff_season")
# df = add_column(df, get_method(df, True, categorize_aggression, seasonMatches), "c_home_aggression_season")#categorize_diff
# df = add_column(df, get_method(df, False, categorize_aggression,seasonMatches), "c_away_aggression_season")
#df = add_column(df, get_method(df, True, categorize_points, last5MatchesBtwTeams), "c_home_form_5btw")
#df = add_column(df, get_method(df, False, categorize_points, last5MatchesBtwTeams), "c_away_form_5btw")
#df = add_column(df, get_method(df, True, categorize_diff, last5MatchesBtwTeams), "c_home_diff_5btw")#categorize_diff
#df = add_column(df, get_method(df, False, categorize_diff,last5MatchesBtwTeams), "c_away_diff_5btw")

222
rules.py
View File

@ -1,121 +1,6 @@
import simpful
from data_filters import * from data_filters import *
import pandas as pd import pandas as pd
FS = FuzzySystem()
# Dominacja OK
# Jakość strzałów - Witek
# Agresesywnosc (zolte + czerwone kartki) - Wojtek
# odbiory i wslizgi (xDef) - Michał, ekspert od xDef
# statystyki z calego sezonu - Wojtek OK
# 5 ostatnich spotkan miedzy druzynami - Witek
def categorize_fuzzy_passes(passes,possession):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Pass1 = TriangleFuzzySet(300,300,500, term="low")
Pass2 = TriangleFuzzySet(300,450,600, term="average")
Pass3 = TriangleFuzzySet(400,600,600, term="high")
FS.add_linguistic_variable("passes", LinguisticVariable([Pass1, Pass2, Pass3], universe_of_discourse=[0,1000]))
Poss1 = TriangleFuzzySet(30,30,45, term="low")
Poss2 = TriangleFuzzySet(40,50,60, term="average")
Poss3 = TriangleFuzzySet(55,70,70, term="high")
FS.add_linguistic_variable("possession", LinguisticVariable([Poss1, Poss2, Poss3], universe_of_discourse=[0,100]))
#Pass_domination1 = TriangleFuzzySet(2,2,6, term="low")
#Pass_domination2 = TriangleFuzzySet(3,5,7, term="average")
#Pass_domination3 = TriangleFuzzySet(4,8,8, term="high")
#FS.add_linguistic_variable("passes_domination", LinguisticVariable([Pass_domination1, Pass_domination2, Pass_domination3], universe_of_discourse=[0,10]))
FS.add_rules([
"IF (passes IS low) AND (possession IS low) THEN (pass_domination IS low)",
"IF (passes IS high) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS average) AND (possession IS average) THEN (pass_domination IS average)",
"IF (passes IS low) AND (possession IS high) THEN (pass_domination IS average)",
"IF (passes IS high ) AND (possession IS low) THEN (pass_domination IS average)",
"IF (passes IS average) AND (possession IS high) THEN (pass_domination IS high)",
"IF (passes IS high) AND (possession IS average) THEN (pass_domination IS high)",
"IF (passes IS low) AND (possession IS average) THEN (pass_domination IS low)",
"IF (passes IS average) AND (possession IS low) THEN (pass_domination IS average)"
])
FS.set_variable("passes", passes)
FS.set_variable("possession", possession)
pass_domination = FS.inference()
return pass_domination
def categorize_fuzzy_shots(shots_overall, shots_on_target):
FS.set_crisp_output_value("low", 0.0)
FS.set_crisp_output_value("average", 0.5)
FS.set_crisp_output_value("high", 1.0)
Shot_ov1 = TriangleFuzzySet(0,0,5, term="low") #pozmieniać przedziały (nakładają się)
Shot_ov2 = TriangleFuzzySet(5,10,15, term="medium")
Shot_ov3 = TriangleFuzzySet(15,25,25, term="high")
FS.add_linguistic_variable("shots_overall", LinguisticVariable([Shot_ov1, Shot_ov2, Shot_ov3], universe_of_discourse=[0,35]))
Shot_ont1 = TriangleFuzzySet(0,0,2, term="low")
Shot_ont2 = TriangleFuzzySet(2,4,6, term="medium")
Shot_ont3 = TriangleFuzzySet(6,10,10, term="high")
FS.add_linguistic_variable("shots_on_target", LinguisticVariable([Shot_ont1, Shot_ont2, Shot_ont3], universe_of_discourse=[0,15]))
#Qual_of_shots1 = TriangleFuzzySet(0,0,0.3, term="low")
#Qual_of_shots2 = TriangleFuzzySet(0.2,0.5,0.8, term="medium")
#Qual_of_shots3 = TriangleFuzzySet(0.7,1,1, term="high")
#FS.add_linguistic_variable("expected_goals", LinguisticVariable([Qual_of_shots1, Qual_of_shots2, Qual_of_shots3], universe_of_discourse=[0,1]))
FS.add_rules([
"IF (shots_overall IS low) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS high) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS average) AND (shots_on_target IS average) THEN (quality_of_shots IS average)",
"IF (shots_overall IS low) AND (shots_on_target IS high) THEN (quality_of_shots IS high)",
"IF (shots_overall IS high ) AND (shots_on_target IS low) THEN (quality_of_shots IS low)",
"IF (shots_overall IS average) AND (possession IS high) THEN (quality_of_shots IS high)",
"IF (passes IS high) AND (possession IS average) THEN (quality_of_shots IS high)",
"IF (passes IS low) AND (possession IS average) THEN (quality_of_shots IS low)",
"IF (passes IS average) AND (possession IS low) THEN (quality_of_shots IS average)"
])
def categorize_shots(shots): def categorize_shots(shots):
if shots >= 12: if shots >= 12:
@ -143,10 +28,10 @@ def categorize_possesion(shots):
def categorize_points(data, row, teamHome, matches_type): def categorize_points(data, row, teamHome, matches_type):
if teamHome: if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data) data_5 = matches_type(row['season'], row['home_team'], row['date'], data)[0]
points = calculatePoints(data_5,row['home_team']) points = calculatePoints(data_5,row['home_team'])
else: else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data) data_5 = matches_type(row['season'], row['away_team'], row['date'], data)[0]
points = calculatePoints(data_5,row['away_team']) points = calculatePoints(data_5,row['away_team'])
if points <=1: if points <=1:
return 0 return 0
@ -155,12 +40,30 @@ def categorize_points(data, row, teamHome, matches_type):
else: else:
return 1 return 1
def categorize_points_Btw(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['home_team'], row['away_team'], row['date'], data)[0]
points = calculatePoints(data_5,row['home_team'])
else:
data_5 = matches_type(row['home_team'], row['away_team'], row['date'], data)[0]
points = calculatePoints(data_5,row['away_team'])
if points <=1:
return 0
elif points >=2:
return 2
else:
return 1
def get_method(data, home_away, method, matches_type): def get_method(data, home_away, method, matches_type):
values = [] values = []
for index, row in data.iterrows(): for index, row in data.iterrows():
values.append(method(data, row, home_away, matches_type)) values.append(method(data, row, home_away, matches_type))
return values return values
def get_points_home(data): def get_points_home(data):
points = [] points = []
for index, row in data.iterrows(): for index, row in data.iterrows():
@ -180,17 +83,53 @@ def get_points_away(data):
def categorize_diff(data, row, teamHome, matches_type): def categorize_diff(data, row, teamHome, matches_type):
if teamHome: if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data) data_5 = matches_type(row['season'], row['home_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['home_team']) diff = calculateGoalDifference(data_5,row['home_team'])
else: else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data) data_5 = matches_type(row['season'], row['away_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['away_team'])
if diff <=0:
return 0
else:
return 1
def categorize_diff_Btw(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['home_team'], row['away_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['home_team'])
else:
data_5 = matches_type(row['home_team'], row['away_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['away_team'])
if diff <=0:
return 0
else:
return 1
def categorize_diff(data, row, teamHome, matches_type):
if teamHome:
data_5 = matches_type(row['season'], row['home_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['home_team'])
else:
data_5 = matches_type(row['season'], row['away_team'], row['date'], data)[0]
diff = calculateGoalDifference(data_5,row['away_team']) diff = calculateGoalDifference(data_5,row['away_team'])
if diff <=0: if diff <=0:
return 0 return 0
else: else:
return 1 return 1
# def categorize_aggression(data, row, teamHome, matches_type):
# if teamHome:
# data_5 = matches_type(row['season'], row['home_team'], row['date'], data)
# diff = calculateAggression(data_5,row['home_team'])
# else:
# data_5 = matches_type(row['season'], row['away_team'], row['date'], data)
# diff = calculateAggression(data_5,row['away_team'])
# return diff
# # if diff <=0:
# # return 0
# # else:
# # return 1
def get_diff_home(data): def get_diff_home(data):
points = [] points = []
for index, row in data.iterrows(): for index, row in data.iterrows():
@ -223,3 +162,46 @@ def get_result_list(df, home_team):
for score in df['result_full']: for score in df['result_full']:
results.append(getResult(score,home_team)) results.append(getResult(score,home_team))
return results return results
def getColumnMethod(data, home_away, column, matches_type):
values = []
for index, row in data.iterrows():
values.append(getColumnByMatches(data, row, home_away, matches_type,column))
return values
def getColumnMethod5Btw(data, home_away, column, matches_type):
values = []
for index, row in data.iterrows():
values.append(getColumnByMatches5Btw(data, row, home_away, matches_type,column))
return values
def getFuzzyMethod(data, method, home_away, frist_column, second_column):
values = []
for index, row in data.iterrows():
values.append(getFuzzyByMatches(method, row, home_away, frist_column, second_column))
return values
def getColumnByMatches(data, row, teamHome, matches_type, column):
if teamHome:
data_5, end = matches_type(row['season'], row['home_team'], row['date'], data)
result = calculateColumn(data_5, row['home_team'], column)
else:
data_5, end = matches_type(row['season'], row['away_team'], row['date'], data)
result = calculateColumn(data_5, row['away_team'], column)
return result
def getColumnByMatches5Btw(data, row, teamHome, matches_type, column):
if teamHome:
data_5, end = last5MatchesBtwTeams(row['home_team'], row['away_team'], row['date'], data)
result = calculateColumn(data_5, row['home_team'], column)
else:
data_5, end = matches_type(row['home_team'], row['away_team'], row['date'], data)
result = calculateColumn(data_5, row['away_team'], column)
return result
def getFuzzyByMatches(method, row, teamHome, frist_column, second_column):
if teamHome:
result = method(row[frist_column], row[second_column])
else:
result = method(row[frist_column], row[second_column])
return result