Add fuzzylogic aggression metric

This commit is contained in:
Halal37 2024-01-27 20:16:01 +01:00
parent 651b3562da
commit dfb44df7cb
3 changed files with 88 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import pandas as pd import pandas as pd
from simpful import * from simpful import *
FS = FuzzySystem()
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',
'home_possession','away_possession','home_shots','away_shots'] 'home_possession','away_possession','home_shots','away_shots']
@ -10,10 +11,61 @@ 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_aggression_5m',
'c_away_aggression_5m', 'c_home_aggression_season', 'c_away_aggression_season',
'c_home_form_season','c_away_form_season',
'c_home_diff_season', 'c_away_diff_season'] 'c_home_diff_season', 'c_away_diff_season']
return dataframe[columns] return dataframe[columns]
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, 3, term="low")
Yellow_cards2 = TriangleFuzzySet(2, 3, 4, term="average")
Yellow_cards3 = TriangleFuzzySet(3, 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 low)",
"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
def last5Matches(season, teamA, data, df): def last5Matches(season, teamA, data, df):
# Wybierz rekordy dla danej pary drużyn i sezonu # 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)))]
@ -53,7 +105,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

View File

@ -23,7 +23,6 @@ from sklearn.metrics import classification_report
if __name__ == "__main__": if __name__ == "__main__":
df = pd.read_csv('df_full_premierleague.csv') df = pd.read_csv('df_full_premierleague.csv')
result = last5Matches('10/11', 'Stoke City', '2010-10-02', df) result = last5Matches('10/11', 'Stoke City', '2010-10-02', df)
#print(result.to_markdown()) #print(result.to_markdown())
#print(result) #print(result)
@ -50,13 +49,17 @@ if __name__ == "__main__":
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, last5Matches), "c_home_diff_5m")#categorize_diff
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,last5Matches), "c_away_diff_5m")
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, 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, 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, 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, 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 = generateFuzzyLogicData(df) df = generateFuzzyLogicData(df)
label_encoder = LabelEncoder() label_encoder = LabelEncoder()

View File

@ -190,7 +190,18 @@ def categorize_diff(data, row, teamHome, matches_type):
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():