2024-01-06 20:36:34 +01:00
|
|
|
import pandas as pd
|
|
|
|
from simpful import *
|
|
|
|
|
2024-01-27 20:16:01 +01:00
|
|
|
FS = FuzzySystem()
|
2024-01-06 20:36:34 +01:00
|
|
|
def generateTrainingData(dataframe):
|
|
|
|
columns = ['season','date','home_team','away_team','result_full','home_passes','away_passes',
|
|
|
|
'home_possession','away_possession','home_shots','away_shots']
|
|
|
|
return dataframe[columns]
|
|
|
|
|
|
|
|
|
|
|
|
def generateFuzzyLogicData(dataframe):
|
2024-01-22 15:59:58 +01:00
|
|
|
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',
|
2024-01-27 20:16:01 +01:00
|
|
|
'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',
|
2024-01-22 15:59:58 +01:00
|
|
|
'c_home_diff_season', 'c_away_diff_season']
|
2024-01-06 20:36:34 +01:00
|
|
|
return dataframe[columns]
|
|
|
|
|
2024-01-27 20:16:01 +01:00
|
|
|
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
|
2024-01-06 20:36:34 +01:00
|
|
|
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)))]
|
|
|
|
|
|
|
|
# Filtruj dane, aby zawierały te przed daną datą
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Wybierz 5 ostatnich przed daną datą
|
|
|
|
last_before_date = before_given_date.head(5)
|
|
|
|
|
|
|
|
return last_before_date
|
|
|
|
|
2024-01-23 23:43:00 +01:00
|
|
|
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
|
2024-01-06 20:36:34 +01:00
|
|
|
|
2024-01-21 22:31:56 +01:00
|
|
|
def seasonMatches(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)))]
|
|
|
|
|
|
|
|
# Filtruj dane, aby zawierały te przed daną datą
|
|
|
|
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)
|
2024-01-06 20:36:34 +01:00
|
|
|
|
2024-01-21 22:31:56 +01:00
|
|
|
return before_given_date
|
2024-01-06 20:36:34 +01:00
|
|
|
|
|
|
|
def getResult(score,teamHome):
|
|
|
|
x,y = score.split('-')
|
|
|
|
x = int(x)
|
|
|
|
y = int(y)
|
|
|
|
|
|
|
|
if (x > y and teamHome == True) or (x < y and teamHome == False):
|
|
|
|
return "win"
|
|
|
|
elif x == y:
|
|
|
|
return "draw"
|
|
|
|
else:
|
|
|
|
return "loss"
|
2024-01-27 20:16:01 +01:00
|
|
|
|
|
|
|
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
|
2024-01-06 20:36:34 +01:00
|
|
|
|
|
|
|
def calculatePoints(matches, team):
|
|
|
|
points = 0
|
|
|
|
for index, row in matches.iterrows():
|
|
|
|
if team == row['home_team']:
|
|
|
|
teamHome = True
|
|
|
|
else:
|
|
|
|
teamHome = False
|
|
|
|
x = getResult(row['result_full'], teamHome)
|
|
|
|
#print(x)
|
|
|
|
if x == "win":
|
|
|
|
points = points + 3
|
|
|
|
elif x == "draw":
|
|
|
|
points = points + 1
|
|
|
|
if matches.shape[0] != 0:
|
|
|
|
points_avg = points / matches.shape[0]
|
|
|
|
else:
|
|
|
|
points_avg = 0
|
|
|
|
return points_avg
|
|
|
|
|
|
|
|
|
|
|
|
def calculateGoalDifference(matches, team):
|
|
|
|
goal_diff = 0
|
|
|
|
for index, row in matches.iterrows():
|
|
|
|
if team == row['home_team']:
|
|
|
|
teamHome = True
|
|
|
|
else:
|
|
|
|
teamHome = False
|
|
|
|
x,y = row['result_full'].split('-')
|
|
|
|
x = int(x)
|
|
|
|
y = int(y)
|
|
|
|
if teamHome:
|
|
|
|
goal_diff = goal_diff + (x-y)
|
|
|
|
else:
|
|
|
|
goal_diff = goal_diff + (y-x)
|
|
|
|
return goal_diff
|