SystemyRozmyte/rules.py

148 lines
3.9 KiB
Python
Raw Normal View History

2024-01-06 15:53:05 +01:00
import simpful
2024-01-06 20:36:34 +01:00
from data_filters import *
import pandas as pd
'''
2024-01-06 15:53:05 +01:00
def kategoryzuj_strzaly(ilosc_strzalow):
FS = FuzzySystem()
TLV = AutoTriangle(3, terms=['mało', 'średnio', 'dużo'], universe_of_discourse=[0, 25])
FS.add_linguistic_variable("strzaly", TLV)
shots1 = TriangleFuzzySet(0, 0, 6, term='mało')
shots2 = TriangleFuzzySet(6, 12, 18, term='średnio')
shots3 = TriangleFuzzySet(12, 25, 25, term='dużo')
FS.add_linguistic_variable("bilans", LinguisticVariable([shots1, shots2, shots3], universe_of_discourse=[0, 25]))
FS.add_rules(["IF strzaly IS mało THEN bilans IS mało",
"IF strzaly IS średnio THEN bilans IS średnio",
"IF strzaly IS dużo THEN bilans IS dużo"])
FS.set_variable("strzaly", ilosc_strzalow)
bilans = FS.inference()
if bilans['bilans'] >= 12:
return 2
elif bilans['bilans'] <= 6:
return 0
else:
return 1
def kategorie_strzalow(druzyna, sezon, data, df):
ostatnie_spotkania = last5Matches(sezon, druzyna, data, df)
shots = []
for index, spotkanie in ostatnie_spotkania.iterrows():
if spotkanie['home_team'] == druzyna:
ilosc_strzalow = spotkanie['home_shots']
else:
ilosc_strzalow = spotkanie['away_shots']
kategoria = kategoryzuj_strzaly(ilosc_strzalow)
shots.append(kategoria)
ostatnie_spotkania['cat_shots'] = shots
2024-01-06 20:36:34 +01:00
return ostatnie_spotkania
'''
def categorize_shots(shots):
if shots >= 12:
return 2
elif shots <= 6:
return 0
else:
return 1
def categorize_passes(pass_count):
if pass_count < 400:
return 0 #słabo
elif 400 <= pass_count <= 500:
return 1 #średnio
else:
return 2 #dużo
def categorize_possesion(shots):
if shots >= 56:
return 2
elif shots <= 40:
return 0
else:
return 1
def categorize_points(data, row, teamHome):
if teamHome:
data_5 = last5Matches(row['season'], row['home_team'], row['date'], data)
points = calculatePoints(data_5,row['home_team'])
else:
data_5 = last5Matches(row['season'], row['away_team'], row['date'], data)
points = calculatePoints(data_5,row['away_team'])
if points <=1:
return 0
elif points >=2:
return 2
else:
return 1
def get_points_home(data):
points = []
for index, row in data.iterrows():
points.append(categorize_points(data, row, True))
return points
def get_points_away(data):
points = []
for index, row in data.iterrows():
points.append(categorize_points(data, row, False))
return points
def categorize_diff(data, row, teamHome):
if teamHome:
data_5 = last5Matches(row['season'], row['home_team'], row['date'], data)
diff = calculateGoalDifference(data_5,row['home_team'])
else:
data_5 = last5Matches(row['season'], row['away_team'], row['date'], data)
diff = calculateGoalDifference(data_5,row['away_team'])
if diff <=0:
return 0
else:
return 1
def get_diff_home(data):
points = []
for index, row in data.iterrows():
points.append(categorize_diff(data, row, True))
return points
def get_diff_away(data):
points = []
for index, row in data.iterrows():
points.append(categorize_diff(data, row, False))
return points
def add_column(data_frame, transform_function, new_column, existing_column=None):
if existing_column != None:
new_column_values = data_frame[existing_column].apply(transform_function)
data_frame[new_column] = new_column_values
else:
new_column_values = transform_function
data_frame[new_column] = new_column_values
return data_frame
def get_result_list(df, home_team):
results = []
for score in df['result_full']:
results.append(getResult(score,home_team))
return results