/
fantstats.py
301 lines (229 loc) · 8.73 KB
/
fantstats.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from data import Data
import numpy as np
import pandas as pd
import time
''' what to do:
- perhaps efficiency metrics
- yards per carry
'''
Data = Data()
MIN_GAMES = 20
# =============== POINTS ===============
PASSING_YARDS = 0.04
PASSING_TD = 4
PASSING_INT = -2
RUSHING_YARDS = 0.1
RUSHING_TD = 6
RECEIVING_YARDS = 0.1
RECEIVING_TD = 6
def main():
start_time = time.time()
players_dict = Data.players()
fant_df = Data.fantasy_df()
_check_if_in_fant_df(players_dict, fant_df)
positions_dict = _get_positions(players_dict, fant_df)
players_dict, dropped = _drop_no_pos_players(players_dict, positions_dict)
pos_df = pd.DataFrame.from_dict(positions_dict, orient='index')
main_df = pd.DataFrame.from_dict(players_dict, orient='index')
main_df = pd.concat([main_df, pos_df], axis=1)
main_df.dropna(inplace=True)
main_df.columns = ['Link', 'Pos']
qbs = main_df[main_df['Pos'] == 'QB'].index.tolist()
rbs = main_df[main_df['Pos'] == 'RB'].index.tolist()
wrs = main_df[main_df['Pos'] == 'WR'].index.tolist()
# ========== QB ==========
qb_dfs = []
tot_qbs = len(qbs)
i = 0
for qb in qbs:
qb_df = get_player_df(qb)
if len(qb_df) >= MIN_GAMES:
try:
final_df = _qb(qb, qb_df)
time.sleep(2)
qb_dfs.append(final_df)
except Exception:
pass
i += 1
print(f'{round(i/tot_qbs, 2)*100}% of QBs complete')
qb_df = pd.concat(qb_dfs)
qb_df.sort_values('FantPts MEDIAN', ascending=False, inplace=True)
qb_df.to_csv('results/qbs20.csv')
# ========================
# ========== RB ==========
rb_dfs = []
tot_rbs = len(rbs)
i = 0
for rb in rbs:
rb_df = get_player_df(rb)
if len(rb_df) >= MIN_GAMES:
try:
final_df = _rb(rb, rb_df)
time.sleep(2)
rb_dfs.append(final_df)
except Exception:
pass
i += 1
print(f'{round(i/tot_rbs, 2)*100}% of RBs complete')
rb_df = pd.concat(rb_dfs)
rb_df.sort_values('FantPts MEDIAN', ascending=False, inplace=True)
rb_df.to_csv('results/rbs20.csv')
# ========================
# ========== WR ==========
wr_dfs = []
tot_wrs = len(wrs)
i = 0
for wr in wrs:
wr_df = get_player_df(wr)
if len(wr_df) >= MIN_GAMES:
try:
final_df = _wr(wr, wr_df)
time.sleep(2)
wr_dfs.append(final_df)
except Exception:
pass
i += 1
print(f'{round(i/tot_wrs, 2)*100}% of WRs complete')
wr_df = pd.concat(wr_dfs)
wr_df.sort_values('FantPts MEDIAN', ascending=False, inplace=True)
wr_df.to_csv('results/wrs20.csv')
# ========================
end_time = time.time()
print(f'Finished in {round(end_time-start_time, 2)}s')
def _check_if_in_fant_df(players_dict, fant_df):
for player in players_dict:
if player not in fant_df['Player'].tolist():
raise ValueError(f'{player} not in the fantasy df')
def _get_positions(players_dict, fant_df):
positions_dict = {}
fant_df['FantPos'] = fant_df['FantPos'].astype(str)
for player in players_dict:
player_entry = fant_df.loc[fant_df['Player'] == player]
pos = player_entry.iloc[0]['FantPos']
positions_dict[player] = pos.upper()
return positions_dict
def _drop_no_pos_players(players_dict, positions_dict):
dropped = []
for player in players_dict:
if positions_dict[player] == 'nan':
dropped.append(player)
for drop in dropped:
del players_dict[drop]
return players_dict, dropped
def _check_if_enough_games(player):
if len(Data.career_stats(player)) >= MIN_GAMES:
return True
else:
return False
def _drop_few_games_players(players_dict):
dropped2 = []
tot = len(players_dict)
i = 0
for player in players_dict:
_good = _check_if_enough_games(player)
print(f'{player} is {_good}')
if _good is False:
dropped2.append(player)
i += 1
time.sleep(2)
print(f'{round(i/tot, 2)*100}% complete!')
for drop in dropped2:
del players_dict[drop]
return players_dict, dropped2
def get_player_df(player):
return Data.career_stats(player)
def _recent_relative_perf(last_5_games, median):
pts_median_diff = []
for pts in last_5_games:
diff = pts - median
pts_median_diff.append(diff)
recent_relative_perf = round(np.mean(np.asarray(pts_median_diff)), 2)
return recent_relative_perf
def _qb(qb, df):
df['Yds'] = df['Yds'].astype(float)
df['TD'] = df['TD'].astype(float)
df['Yds.2'] = df['Yds.2'].astype(float)
df['TD.1'] = df['TD.1'].astype(float)
df['Int'] = df['Int'].astype(float)
df['FantPts'] = ((df['Yds']*PASSING_YARDS) + (df['TD']*PASSING_TD) +
(df['Int']*PASSING_INT) + (df['Yds.2']*RUSHING_YARDS) +
(df['TD.1']*RUSHING_TD))
last_5_games = df['FantPts'].tail(5).tolist()
last_5_games = last_5_games[::-1]
fant_pts_median = df['FantPts'].median()
fant_pts_mean = df['FantPts'].mean()
fant_pts_std = df['FantPts'].std()
fant_pts_30_percentile = np.percentile(df['FantPts'], 30)
fant_pts_plus_std = fant_pts_mean + fant_pts_std
fant_pts_minus_std = fant_pts_mean - fant_pts_std
fant_pts_skew = fant_pts_median - fant_pts_mean
# Outperforming median metric
recent_relative_perf = _recent_relative_perf(last_5_games, fant_pts_median)
data = {'Name': [qb], 'FantPts MEDIAN': [fant_pts_median],
'FantPts SKEW': [fant_pts_skew],
'FantPts +SD': [fant_pts_plus_std],
'FantPts -SD': [fant_pts_minus_std],
'FantPts 30 Percentile': [fant_pts_30_percentile],
'Last 5 Games': [last_5_games],
'Rec XS Pts': [recent_relative_perf]}
final_df = pd.DataFrame.from_dict(data=data)
final_df.set_index('Name', inplace=True)
return final_df
def _rb(rb, df):
df['Yds'] = df['Yds'].astype(float)
df['TD'] = df['TD'].astype(float)
df['Yds.1'] = df['Yds.1'].astype(float)
df['TD.1'] = df['TD.1'].astype(float)
df['FantPts'] = ((df['Yds']*RUSHING_YARDS) + (df['TD']*RUSHING_TD) +
(df['TD.1']*RECEIVING_TD) + (df['Yds.1']*RECEIVING_YARDS))
last_5_games = df['FantPts'].tail(5).tolist()
last_5_games = last_5_games[::-1]
fant_pts_median = df['FantPts'].median()
fant_pts_mean = df['FantPts'].mean()
fant_pts_std = df['FantPts'].std()
fant_pts_30_percentile = np.percentile(df['FantPts'], 30)
fant_pts_plus_std = fant_pts_mean + fant_pts_std
fant_pts_minus_std = fant_pts_mean - fant_pts_std
fant_pts_skew = fant_pts_median - fant_pts_mean
# Outperforming median metric
recent_relative_perf = _recent_relative_perf(last_5_games, fant_pts_median)
data = {'Name': [rb], 'FantPts MEDIAN': [fant_pts_median],
'FantPts SKEW': [fant_pts_skew],
'FantPts +SD': [fant_pts_plus_std],
'FantPts -SD': [fant_pts_minus_std],
'FantPts 30 Percentile': [fant_pts_30_percentile],
'Last 5 Games': [last_5_games],
'Rec XS Pts': [recent_relative_perf]}
final_df = pd.DataFrame.from_dict(data=data)
final_df.set_index('Name', inplace=True)
return final_df
def _wr(wr, df):
df['Yds'] = df['Yds'].astype(float)
df['TD'] = df['TD'].astype(float)
df['Yds.1'] = df['Yds.1'].astype(float)
df['TD.1'] = df['TD.1'].astype(float)
df['FantPts'] = ((df['Yds.1']*RUSHING_YARDS) + (df['TD.1']*RUSHING_TD) +
(df['TD']*RECEIVING_TD) + (df['Yds']*RECEIVING_YARDS))
last_5_games = df['FantPts'].tail(5).tolist()
last_5_games = last_5_games[::-1]
fant_pts_median = df['FantPts'].median()
fant_pts_mean = df['FantPts'].mean()
fant_pts_std = df['FantPts'].std()
fant_pts_30_percentile = np.percentile(df['FantPts'], 30)
fant_pts_plus_std = fant_pts_mean + fant_pts_std
fant_pts_minus_std = fant_pts_mean - fant_pts_std
fant_pts_skew = fant_pts_median - fant_pts_mean
# Outperforming median metric
recent_relative_perf = _recent_relative_perf(last_5_games, fant_pts_median)
data = {'Name': [wr], 'FantPts MEDIAN': [fant_pts_median],
'FantPts SKEW': [fant_pts_skew],
'FantPts +SD': [fant_pts_plus_std],
'FantPts -SD': [fant_pts_minus_std],
'FantPts 30 Percentile': [fant_pts_30_percentile],
'Last 5 Games': [last_5_games],
'Rec XS Pts': [recent_relative_perf]}
final_df = pd.DataFrame.from_dict(data=data)
final_df.set_index('Name', inplace=True)
return final_df
if __name__ == "__main__":
main()