354 lines
15 KiB
Python
354 lines
15 KiB
Python
import pandas as pd
|
|
import sys
|
|
import numpy as np
|
|
import skfuzzy as fuzz
|
|
import matplotlib.pyplot as plt
|
|
|
|
def fuzzy(goalsV, xGPerMatchV, shotsPerMatchV, onTargetPerMatchV):
|
|
goals = np.arange(0, 35, 1)
|
|
xGPerMatch = np.arange(0, 1, 0.01)
|
|
shotsPerMatch = np.arange(0, 5, 0.01)
|
|
onTargetPerMatch = np.arange(0, 5, 0.01)
|
|
ocena = np.arange(0, 101, 1)
|
|
|
|
goals_low = fuzz.trapmf(goals, [0, 0, 10, 15])
|
|
goals_md = fuzz.trimf(goals, [10, 15, 20])
|
|
goals_hi = fuzz.trapmf(goals, [15, 20, 35, 100])
|
|
xGPerMatch_lo = fuzz.trapmf(xGPerMatch, [0, 0, 0.2, 0.5])
|
|
xGPerMatch_md = fuzz.trapmf(xGPerMatch, [0.2, 0.5, 0.7, 1])
|
|
xGPerMatch_hi = fuzz.trapmf(xGPerMatch, [0.75, 1, 2, 10])
|
|
shotsPerMatch_lo = fuzz.trapmf(shotsPerMatch, [0, 0, 1, 2])
|
|
shotsPerMatch_md = fuzz.trapmf(shotsPerMatch, [1, 2, 3, 4])
|
|
shotsPerMatch_hi = fuzz.trapmf(shotsPerMatch, [3, 4, 4.5, 10])
|
|
onTargetPerMatch_lo = fuzz.trapmf(onTargetPerMatch, [0, 0, 0.25, 0.75])
|
|
onTargetPerMatch_md = fuzz.trapmf(onTargetPerMatch, [0.25,0.5, 1, 1.25])
|
|
onTargetPerMatch_hi = fuzz.trapmf(onTargetPerMatch, [1, 1.5,3.5, 5])
|
|
ocena_lo = fuzz.trapmf(ocena, [0, 0, 40, 50])
|
|
ocena_md = fuzz.trapmf(ocena, [40, 50, 60, 80])
|
|
ocena_hi = fuzz.trapmf(ocena, [70, 80, 100, 100])
|
|
|
|
#
|
|
# fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(nrows=5, figsize=(8, 9))
|
|
#
|
|
# ax0.plot(goals, goals_low, 'b', linewidth=1.5, label='Mało')
|
|
# ax0.plot(goals, goals_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax0.plot(goals, goals_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax0.set_title('Liczba bramek')
|
|
# ax0.legend()
|
|
#
|
|
# ax1.plot(xGPerMatch, xGPerMatch_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax1.plot(xGPerMatch, xGPerMatch_md, 'g', linewidth=1.5, label='Średno')
|
|
# ax1.plot(xGPerMatch, xGPerMatch_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax1.set_title('Service quality')
|
|
# ax1.legend()
|
|
#
|
|
# ax2.plot(shotsPerMatch, shotsPerMatch_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax2.plot(shotsPerMatch, shotsPerMatch_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax2.plot(shotsPerMatch, shotsPerMatch_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax2.set_title('Strzały na mecz')
|
|
# ax2.legend()
|
|
#
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax3.set_title('Strzały w światlo bramki na mecz')
|
|
# ax3.legend()
|
|
#
|
|
# ax4.plot(ocena, ocena_lo, 'b', linewidth=1.5, label='Niska')
|
|
# ax4.plot(ocena, ocena_md, 'g', linewidth=1.5, label='Średnia')
|
|
# ax4.plot(ocena, ocena_hi, 'r', linewidth=1.5, label='Wysoka')
|
|
# ax4.set_title('Ocena')
|
|
# ax4.legend()
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0, ax1, ax2, ax3, ax4):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
goals_value = goalsV
|
|
xGPerMatch_value = xGPerMatchV
|
|
shotsPerMatch_value = shotsPerMatchV
|
|
onTargetPerMatch_value = onTargetPerMatchV
|
|
|
|
goals_level_lo = fuzz.interp_membership(goals, goals_low, goals_value)
|
|
goals_level_md = fuzz.interp_membership(goals, goals_md, goals_value)
|
|
goals_level_hi = fuzz.interp_membership(goals, goals_hi, goals_value)
|
|
|
|
xGPerMatch_level_lo = fuzz.interp_membership(xGPerMatch, xGPerMatch_lo, xGPerMatch_value)
|
|
xGPerMatch_level_md = fuzz.interp_membership(xGPerMatch, xGPerMatch_md, xGPerMatch_value)
|
|
xGPerMatch_level_hi = fuzz.interp_membership(xGPerMatch, xGPerMatch_hi, xGPerMatch_value)
|
|
|
|
shotsPerMatch_level_lo = fuzz.interp_membership(shotsPerMatch, shotsPerMatch_lo, shotsPerMatch_value)
|
|
shotsPerMatch_level_md = fuzz.interp_membership(shotsPerMatch, shotsPerMatch_md, shotsPerMatch_value)
|
|
shotsPerMatch_level_hi = fuzz.interp_membership(shotsPerMatch, shotsPerMatch_hi, shotsPerMatch_value)
|
|
|
|
onTargetPerMatch_level_lo = fuzz.interp_membership(onTargetPerMatch, onTargetPerMatch_lo, onTargetPerMatch_value)
|
|
onTargetPerMatch_level_md = fuzz.interp_membership(onTargetPerMatch, onTargetPerMatch_md, onTargetPerMatch_value)
|
|
onTargetPerMatch_level_hi = fuzz.interp_membership(onTargetPerMatch, onTargetPerMatch_hi, onTargetPerMatch_value)
|
|
|
|
#fmax OR
|
|
#fmin AND
|
|
|
|
active_rule1 = np.fmax(goals_level_hi, xGPerMatch_level_hi)
|
|
active_rule2 = np.fmax(active_rule1, onTargetPerMatch_level_hi)
|
|
|
|
ocena_activation_hi = np.fmin(active_rule2, ocena_hi) # removed entirely to 0
|
|
|
|
active_rule3=np.fmin(onTargetPerMatch_level_md,goals_level_md)
|
|
active_rule4=np.fmin(xGPerMatch_level_hi,goals_level_md)
|
|
active_rule5=np.fmax(active_rule3,active_rule4)
|
|
active_rule6=np.fmin(onTargetPerMatch_level_hi,goals_level_lo)
|
|
active_rule7=np.fmax(active_rule5,active_rule6)
|
|
ocena_activation_md = np.fmin(active_rule7, ocena_md)
|
|
|
|
active_rule8=np.fmin(shotsPerMatch_level_hi,onTargetPerMatch_level_lo)
|
|
active_rule9=np.fmin(xGPerMatch_level_hi,goals_level_md)
|
|
active_rule10=np.fmax(active_rule8,active_rule9)
|
|
active_rule11=np.fmax(active_rule10,goals_level_lo)
|
|
ocena_activation_lo = np.fmin(active_rule11, ocena_lo)
|
|
ocena0 = np.zeros_like(ocena)
|
|
|
|
#
|
|
# fig, ax0 = plt.subplots(figsize=(8, 3))
|
|
#
|
|
# ax0.fill_between(ocena, ocena0, ocena_activation_lo, facecolor='b', alpha=0.7)
|
|
# ax0.plot(ocena, ocena_lo, 'b', linewidth=0.5, linestyle='--', )
|
|
# ax0.fill_between(ocena, ocena0, ocena_activation_md, facecolor='g', alpha=0.7)
|
|
# ax0.plot(ocena, ocena_md, 'g', linewidth=0.5, linestyle='--')
|
|
# ax0.fill_between(ocena, ocena0, ocena_activation_hi, facecolor='r', alpha=0.7)
|
|
# ax0.plot(ocena, ocena_hi, 'r', linewidth=0.5, linestyle='--')
|
|
# ax0.set_title('Output membership activity')
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0,):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
aggregated = np.fmax(ocena_activation_lo,
|
|
np.fmax(ocena_activation_md, ocena_activation_hi))
|
|
|
|
# Calculate defuzzified result
|
|
ocena_output = fuzz.defuzz(ocena, aggregated, 'centroid')
|
|
ocena_activation = fuzz.interp_membership(ocena, aggregated, ocena_output) # for plot
|
|
|
|
# # Visualize this
|
|
# fig, ax0 = plt.subplots(figsize=(8, 3))
|
|
#
|
|
# ax0.plot(ocena, ocena_lo, 'b', linewidth=0.5, linestyle='--', )
|
|
# ax0.plot(ocena, ocena_md, 'g', linewidth=0.5, linestyle='--')
|
|
# ax0.plot(ocena, ocena_hi, 'r', linewidth=0.5, linestyle='--')
|
|
# ax0.fill_between(ocena, ocena0, aggregated, facecolor='Orange', alpha=0.7)
|
|
# ax0.plot([ocena_output, ocena_output], [0, ocena_activation], 'k', linewidth=1.5, alpha=0.9)
|
|
# ax0.set_title('Aggregated membership and result (line)')
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0,):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
return ocena_output
|
|
|
|
def fuzzy2(season_rateV, ageV, market_valueV):
|
|
season_rate = np.arange(0, 101, 1)
|
|
age = np.arange(15, 50, 1)
|
|
market_value = np.arange(0, 200, 0.5)
|
|
transfer_rate = np.arange(0, 101, 1)
|
|
|
|
season_rate_lo = fuzz.trapmf(season_rate, [0, 0, 40, 50])
|
|
season_rate_md = fuzz.trapmf(season_rate, [40, 50, 60,80])
|
|
season_rate_hi = fuzz.trapmf(season_rate, [70, 80, 100, 100])
|
|
age_lo = fuzz.trapmf(age, [15, 15, 20, 25])
|
|
age_md = fuzz.trapmf(age, [20, 25, 28, 30])
|
|
age_hi = fuzz.trapmf(age, [28, 30, 40, 100])
|
|
market_value_lo = fuzz.trapmf(market_value, [0, 0, 15, 40])
|
|
market_value_md = fuzz.trimf(market_value, [30, 50, 80])
|
|
market_value_hi = fuzz.trapmf(market_value, [70, 80, 180, 1000])
|
|
transfer_rate_lo = fuzz.trapmf(transfer_rate, [0, 0, 40, 50])
|
|
transfer_rate_md = fuzz.trapmf(transfer_rate, [40, 50, 60, 80])
|
|
transfer_rate_hi = fuzz.trapmf(transfer_rate, [70, 80, 100, 100])
|
|
|
|
#
|
|
# fig, (ax0, ax1, ax2, ax3, ax4) = plt.subplots(nrows=5, figsize=(8, 9))
|
|
#
|
|
# ax0.plot(season_rate, season_rate_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax0.plot(season_rate, season_rate_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax0.plot(season_rate, season_rate_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax0.set_title('Liczba bramek')
|
|
# ax0.legend()
|
|
#
|
|
# ax1.plot(age, age_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax1.plot(age, age_md, 'g', linewidth=1.5, label='Średno')
|
|
# ax1.plot(age, age_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax1.set_title('Service quality')
|
|
# ax1.legend()
|
|
#
|
|
# ax2.plot(market_value, market_value_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax2.plot(market_value, market_value_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax2.plot(market_value, market_value_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax2.set_title('Strzały na mecz')
|
|
# ax2.legend()
|
|
#
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_lo, 'b', linewidth=1.5, label='Mało')
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_md, 'g', linewidth=1.5, label='Średnio')
|
|
# ax3.plot(onTargetPerMatch, onTargetPerMatch_hi, 'r', linewidth=1.5, label='Dużo')
|
|
# ax3.set_title('Strzały w światlo bramki na mecz')
|
|
# ax3.legend()
|
|
#
|
|
# ax4.plot(transfer_rate, transfer_rate_lo, 'b', linewidth=1.5, label='Niska')
|
|
# ax4.plot(transfer_rate, transfer_rate_md, 'g', linewidth=1.5, label='Średnia')
|
|
# ax4.plot(transfer_rate, transfer_rate_hi, 'r', linewidth=1.5, label='Wysoka')
|
|
# ax4.set_title('Ocena')
|
|
# ax4.legend()
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0, ax1, ax2, ax3, ax4):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
season_rate_value = season_rateV
|
|
age_value = ageV
|
|
market_value_value = market_valueV
|
|
|
|
|
|
season_rate_level_lo = fuzz.interp_membership(season_rate, season_rate_lo, season_rate_value)
|
|
season_rate_level_md = fuzz.interp_membership(season_rate, season_rate_md, season_rate_value)
|
|
season_rate_level_hi = fuzz.interp_membership(season_rate, season_rate_hi, season_rate_value)
|
|
|
|
age_level_lo = fuzz.interp_membership(age, age_lo, age_value)
|
|
age_level_md = fuzz.interp_membership(age, age_md, age_value)
|
|
age_level_hi = fuzz.interp_membership(age, age_hi, age_value)
|
|
|
|
market_value_level_lo = fuzz.interp_membership(market_value, market_value_lo, market_value_value)
|
|
market_value_level_md = fuzz.interp_membership(market_value, market_value_md, market_value_value)
|
|
market_value_level_hi = fuzz.interp_membership(market_value, market_value_hi, market_value_value)
|
|
#fmax OR
|
|
#fmin AND
|
|
|
|
active_rule1=np.fmin(season_rate_level_hi, age_level_lo)
|
|
active_rule2=np.fmin(season_rate_level_hi, market_value_level_md)
|
|
active_rule3=np.fmin(season_rate_level_hi, age_level_md)
|
|
active_rule4=np.fmax(active_rule1,active_rule2)
|
|
active_rule5=np.fmax(active_rule3,active_rule4)
|
|
|
|
transfer_rate_activation_hi = np.fmin(active_rule5, transfer_rate_hi) # removed entirely to 0
|
|
|
|
|
|
active_rule6=np.fmin(season_rate_level_md,age_level_lo)
|
|
active_rule7=np.fmin(season_rate_level_md, market_value_level_lo)
|
|
active_rule8=np.fmin(market_value_level_lo, age_level_lo)
|
|
active_rule9=np.fmin(age_level_hi,market_value_level_lo)
|
|
active_rule10=np.fmax(active_rule6,active_rule7)
|
|
active_rule11=np.fmax(active_rule8,active_rule9)
|
|
active_rule12=np.fmax(active_rule10,active_rule11)
|
|
|
|
transfer_rate_activation_md = np.fmin(active_rule12, transfer_rate_md)
|
|
|
|
active_rule13=np.fmin(age_level_hi, market_value_level_hi)
|
|
active_rule14=np.fmin(age_level_md,season_rate_md)
|
|
active_rule15=np.fmax(active_rule13,season_rate_level_lo)
|
|
active_rule16=np.fmax(active_rule14,active_rule15)
|
|
|
|
transfer_rate_activation_lo = np.fmin(active_rule16, transfer_rate_lo)
|
|
transfer_rate0 = np.zeros_like(transfer_rate)
|
|
|
|
#
|
|
# fig, ax0 = plt.subplots(figsize=(8, 3))
|
|
#
|
|
# ax0.fill_between(transfer_rate, transfer_rate0, transfer_rate_activation_lo, facecolor='b', alpha=0.7)
|
|
# ax0.plot(transfer_rate, transfer_rate_lo, 'b', linewidth=0.5, linestyle='--', )
|
|
# ax0.fill_between(transfer_rate, transfer_rate0, transfer_rate_activation_md, facecolor='g', alpha=0.7)
|
|
# ax0.plot(transfer_rate, transfer_rate_md, 'g', linewidth=0.5, linestyle='--')
|
|
# ax0.fill_between(transfer_rate, transfer_rate0, transfer_rate_activation_hi, facecolor='r', alpha=0.7)
|
|
# ax0.plot(transfer_rate, transfer_rate_hi, 'r', linewidth=0.5, linestyle='--')
|
|
# ax0.set_title('Output membership activity')
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0,):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
aggregated = np.fmax(transfer_rate_activation_lo,
|
|
np.fmax(transfer_rate_activation_md, transfer_rate_activation_hi))
|
|
|
|
# Calculate defuzzified result
|
|
transfer_rate_output = fuzz.defuzz(transfer_rate, aggregated, 'centroid')
|
|
Transfer_rate_activation = fuzz.interp_membership(transfer_rate, aggregated, transfer_rate_output) # for plot
|
|
|
|
# # Visualize this
|
|
# fig, ax0 = plt.subplots(figsize=(8, 3))
|
|
#
|
|
# ax0.plot(transfer_rate, transfer_rate_lo, 'b', linewidth=0.5, linestyle='--', )
|
|
# ax0.plot(transfer_rate, transfer_rate_md, 'g', linewidth=0.5, linestyle='--')
|
|
# ax0.plot(transfer_rate, transfer_rate_hi, 'r', linewidth=0.5, linestyle='--')
|
|
# ax0.fill_between(transfer_rate, transfer_rate0, aggregated, facecolor='Orange', alpha=0.7)
|
|
# ax0.plot([transfer_rate_output, transfer_rate_output], [0, Transfer_rate_activation], 'k', linewidth=1.5, alpha=0.9)
|
|
# ax0.set_title('Aggregated membership and result (line)')
|
|
#
|
|
# # Turn off top/right axes
|
|
# for ax in (ax0,):
|
|
# ax.spines['top'].set_visible(False)
|
|
# ax.spines['right'].set_visible(False)
|
|
# ax.get_xaxis().tick_bottom()
|
|
# ax.get_yaxis().tick_left()
|
|
#
|
|
# plt.tight_layout()
|
|
|
|
return transfer_rate_output
|
|
|
|
def main():
|
|
data = pd.read_csv("resources/Data.csv")
|
|
# league = sys.argv[1]
|
|
# year = sys.argv[2]
|
|
|
|
# filtered_df = data.loc[data['Year'] == year]
|
|
filtered_df = data.loc[data['League'] == "La Liga"]
|
|
|
|
#print(filtered_df)
|
|
|
|
results_rate = []
|
|
results_transfer_rate = []
|
|
|
|
for item in filtered_df.iterrows():
|
|
print(item)
|
|
goals=item[1]["Goals"]
|
|
xGPerMatch=item[1]["xG Per Avg Match"]
|
|
shotsPerMatch=item[1]["Shots Per Avg Match"]
|
|
onTargetPerMatch=item[1]["On Target Per Avg Match"]
|
|
rate = fuzzy(goals,xGPerMatch,shotsPerMatch,onTargetPerMatch)
|
|
print(rate)
|
|
results_rate.append(rate)
|
|
age=item[1]["Age"]
|
|
market_value=item[1]["Value (in millions)"]
|
|
transfer_rate=fuzzy2(rate,age,market_value)
|
|
print(transfer_rate)
|
|
results_transfer_rate.append(transfer_rate)
|
|
|
|
filtered_df['Rate'] = results_rate
|
|
filtered_df['Transfer rate']=results_transfer_rate
|
|
filtered_df.to_csv('resources/out.csv')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|