-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootball.py
136 lines (90 loc) · 3.91 KB
/
football.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import pandas
import matplotlib.pyplot as plt
from scrapers import PlayerScraper
from soccerplots.radar_chart import Radar
playerstats = PlayerScraper()
df = playerstats.get_goals_assists_by_player()
# target man: high assists, high goals, high duels
# dribbler : high dribbling, concedes fowls
# ALL: played lots of games..!
#creating the columns for the radar through a function
def extra_columns(df):
df["goals_per_game"] = df["goals"] / df["games"]
df["assists_per_game"] = df["assists"] / df["games"]
df["duels_win_percent"] = df["won"] / df["duels"] * 100
df.loc[(df["dribble_percent"] > 40) &
(df["fouls drawn per game"] > 1) &
(df["penalty scored per game"] > 0.1),
"player type"] = "dribbler"
df.loc[(df["duels_win_percent"] > 0.3) &
(df["goals"] > 10) &
(df["assists_per_game"] > 0.3) &
(df["penalty scored per game"] > 0.1),
"player type"] = "target man"
extra_columns(df)
#calling values for the radar through a function
def plot_radars(df):
ranks = df.copy()
target_man_cols = ["duels_win_percent", "goals", "assists_per_game", "penalty scored per game"]
dribbler_cols = ["dribble_percent", "fouls drawn per game", "penalty scored per game"]
include = list(set(target_man_cols + dribbler_cols))
print(include)
# add up the rank for each of these columns
ranks[include] = ranks[include].rank(method="dense", ascending=True)
factor = len(df) / 10
ranks[include] = ranks[include] / factor
targetmen = ranks.loc[ranks["player type"] == "target man", include + ["name"]]
targetmen["ranksum"] = targetmen[target_man_cols].sum(axis=1)
dribblers = ranks.loc[ranks["player type"] == "dribbler", include + ["name"]]
dribblers["ranksum"] = dribblers[dribbler_cols].sum(axis=1)
#ranking for top target man and dribbler
targetmen = targetmen.sort_values("ranksum", ascending=False)
dribblers = dribblers.sort_values("ranksum", ascending=False)
top_target_man = targetmen.iloc[0]
top_target_man_name = top_target_man["name"]
top_target_man_values = top_target_man[include]
top_dribbler = dribblers.iloc[0]
top_dribbler_name = top_dribbler["name"]
top_dribbler_values = top_dribbler[include]
print(targetmen)
print(dribblers)
#creating a range for the attributes
ranges = []
targetman_value = []
dribbler_value = []
for p in include:
a = min(df[p])
a = a - (a * .25)
b = max(df[p])
b = b + (b * .25)
# ranges.append((a, b))
ranges.append((1, 10))
print(ranges)
# Adding visualise presentation for the radar plot
T = dict(
title_name=top_target_man_name,
title_color='blue',
subtitle_name='top target man',
subtitle_color='blue',
title_name_2=top_dribbler_name,
title_color_2='red',
subtitle_name_2='top dribbler',
subtitle_color_2='red'
)
S1 = [top_target_man_values.to_list(), top_dribbler_values.to_list()]
print(S1)
plt.rcParams["font.family"] = "Arial"
radar = Radar(fontfamily="Arial", background_color="black", patch_color="#28252C", label_color="white",
range_color="#BFE9BF")
fig, ax = radar.plot_radar(ranges=ranges, params=include, fontfamily="Arial",
values=S1, alphas=[0.76, 0.6],
title=T, endnote="Data from API Football", radar_color=['#0f4c75', '#e94560'], compare=True)
plt.show()
pandas.set_option('display.max_columns', None)
plot_radars(df)
# Params is a variable that lists the attributes for the radar
params = ["goals_per_game", "assists_per_game", "duels_win_percent",
"dribble_percent", "duels_win_percent", "penalty scored per game"]
pandas.set_option('display.max_columns', None)
print(df)
range()