-
Notifications
You must be signed in to change notification settings - Fork 0
/
player-probability-determiner.py
executable file
·1660 lines (1252 loc) · 93.6 KB
/
player-probability-determiner.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# player-probability-determiner.py
# determine the probability that a player will perform an action
import reader # input data
import numpy # mean, median
import scipy
from scipy import stats # calculate mode
from tabulate import tabulate # display output
import isolator # isolate player game data which exludes headers and monthly averages
import re # split result data into score data
import determiner # determine consistent streak
from datetime import datetime # convert date str to date so we can see if games 1 day apart and how many games apart
from datetime import timedelta
import pandas as pd # see when was prev game
import writer # display game data
import sorter # sort predictions by degree of belief
import generator # generate stats dicts, all records dicts, all means dicts, all streaks dicts, etc
# === main settings ===
read_all_seasons = False
find_matchups = True
# optional settings
read_new_teams = True # trades only happen a few times a year so set true when players move to new teams
# graph settings
player_of_interest = 'vanvleet'
stat_of_interest = 'ast'
allow_all = False # allow all stats to get to plot fcn so we can focus on single player
display_plots = False # true if we want to use matplotlib to show plot automatically instead of outputting to spreadsheet
# input: game log
# player name
# date, opponent, result, min, fg, fg%, 3pt, 3p%, ft, ft%, reb, ast, blk, stl, pf, to, pts
#print("\n===" + player_name + "===\n")
#row1 = ['Tue 2/7','vs OKC','L 133-130', '34','13-20','65.0','4-6','66.7','8-10','80.0','7','3','0','3','3','4','38']
# for testing
# data_type = 'Player Data'
# player_name = 'Ja Morant'
# # count no. times player hit over line
# pts_line = 1
# r_line = 3
# a_line = 1
# player_names = ['Julius Randle', 'Jalen Brunson', 'RJ Barrett', 'Demar Derozan', 'Paolo Banchero', 'Zach Lavine', 'Franz Wagner', 'Nikola Vucevic', 'Wendell Carter Jr', 'Ayo Dosunmu', 'Markelle Fultz', 'Patrick Williams', 'Brandon Ingram', 'Shai Gilgeous Alexander', 'CJ Mccollum', 'Josh Giddey', 'Trey Murphy III', 'Jalen Williams', 'Herbert Jones', 'Anthony Edwards', 'Luka Doncic', 'Rudy Gobert', 'Kyrie Irving', 'Mike Conley', 'Jaden Mcdaniels', 'Jordan Poole', 'Klay Thompson', 'Draymond Green', 'Kevon Looney','Gary Harris'] #['Bojan Bogdanovic', 'Jaden Ivey', 'Killian Hayes', 'Pascal Siakam', 'Fred Vanvleet', 'Gary Trent Jr', 'Scottie Barnes', 'Isaiah Stewart', 'Jalen Duren', 'Chris Boucher'] #['Ja Morant', 'Desmond Bane', 'Jaren Jackson Jr', 'Dillon Brooks', 'Jayson Tatum', 'Derrick White', 'Robert Williams', 'Malcolm Brogdon', 'Al Horford', 'Xavier Tillman', 'Brandon Clarke']
# pts_lines = [25,25,19,23,19,24,16,19,15,9,14,11,31,18,13,28,33,14,25,11,11,26,26,9,7,10] #[22, 16, 13, 25, 22, 20, 17, 12, 11, 10] #[28, 21, 16, 13, 33, 18, 10, 15, 9, 8, 10]
# r_lines = [10,4,5,5,7,5,2,12,8,2,4,5,2,5,2,8,2,2,2,6,9,11,4,3,4,2,2,8,9,2] #[4, 4, 3, 7, 4, 3, 7, 9, 10, 6] #[7, 5, 7, 3, 9, 5, 10, 5, 6, 7, 6]
# a_lines = [2,6,2,5,2,4,2,2,3,2,6,2,2,6,2,6,2,3,2,2,7,2,6,6,2,5,3,7,2,2] #[3, 6, 7, 6, 7, 2, 5, 2, 2, 2] #[8, 4, 2, 2, 6, 6, 2, 4, 3, 2, 2]
# threes_lines = [3,1,1,1,1,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,5,1,1,2]
# b_lines = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
# s_lines = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
# to_lines = [3,1,1,1,3,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1]
# only need to get the lines for player of interest
#if player_of_interest == '': # if blank do all players in raw projected lines
# determine player outcome probability
# first generate player outcomes and then determine its prob
# players_of_interest = ['chris paul']
# player_outcomes = generator.generate_players_outcomes(players_of_interest, todays_games_date_obj)
# writer.display_player_outcomes(player_outcomes)
# todo: order players from most to least consistent so we can optimize returns by only voting on highly consistent players
# === generate highly consistent streaks for review
# we can take a subset of all player outcomes
def generate_all_player_predictions():
print('\n===Generate All Player Predictions===\n')
todays_games_date_str = '' # format: m/d/y, like 3/14/23. set if we want to look at games in advance
todays_games_date_obj = datetime.today() # by default assume todays game is actually today and we are not analyzing in advance
if todays_games_date_str != '':
todays_games_date_obj = datetime.strptime(todays_games_date_str, '%m/%d/%y')
input_type = str(todays_games_date_obj.month) + '/' + str(todays_games_date_obj.day)
#print('input type from date object: ' + input_type)
#input_type = '3/14' # date as mth/day will become mth_day in file
todays_games_date = input_type + '/23'
todays_games_date_obj = datetime.strptime(todays_games_date, '%m/%d/%y')
current_dow = todays_games_date_obj.strftime('%a').lower()
# print('current_dow: ' + str(current_dow))
# current_dow = datetime.strptime(todays_games_date, '%m/%d/%y').strftime('%a').lower()
# print('current_dow: ' + str(current_dow))
# v2: copy paste raw projected lines direct from website
# raw projected lines in format: [['Player Name', 'O 10 +100', 'U 10 +100', 'Player Name', 'O 10 +100', 'U 10 +100', Name', 'O 10 +100', 'U 10 +100']]
data_type = "Player Lines"
raw_projected_lines = reader.extract_data(data_type, input_type, extension='tsv', header=True) # tsv no header
print("raw_projected_lines: " + str(raw_projected_lines))
player_names = determiner.determine_all_player_names(raw_projected_lines)
player_espn_ids_dict = reader.read_all_player_espn_ids(player_names)
all_player_teams = reader.read_all_players_teams(player_espn_ids_dict, read_new_teams=False)
# convert raw projected lines to projected lines
projected_lines = reader.read_projected_lines(raw_projected_lines, all_player_teams)
# v1: copy from website by hand into organized format
#projected_lines = reader.extract_data(data_type, input_type, header=True) # csv w/ header
if input_type == '': # for testing we make input type blank ''
#projected_lines = reader.read_projected_lines(date)
projected_lines = [['Name', 'PTS', 'REB', 'AST', '3PM', 'BLK', 'STL', 'TO','LOC','OPP'], ['Giannis Antetokounmpo', '34', '13', '6', '1', '1', '1', '1', 'Home', 'ATL']]
print("projected_lines: " + str(projected_lines))
# if copy pasted from website
header_row = ['Name', 'PTS', 'REB', 'AST', '3PM', 'BLK', 'STL', 'TO','LOC','OPP']
projected_lines_dict = {}
header_row = projected_lines[0]
for player_lines in projected_lines[1:]:
player_name = player_lines[0].lower()
projected_lines_dict[player_name] = dict(zip(header_row[1:],player_lines[1:]))
print("projected_lines_dict: " + str(projected_lines_dict))
# get all player season logs
# use player espn ids from above
# all_player_season_logs_dict = { player name: { year: df, .. }, .. }
all_player_season_logs_dict = reader.read_all_players_season_logs(player_names, read_all_seasons, player_espn_ids_dict)
# get position and team from same source espn game
# all_player_teams = {}
# all_player_info = {'positions':{}, 'teams':{}}
all_player_positions = {}
if find_matchups == True:
all_player_positions = reader.read_all_players_positions(player_espn_ids_dict)
#all_player_info['positions'] = reader.read_all_players_positions(player_espn_ids_dict)
# todo:
# get team schedules from espn so we can get next game opponent and location and date
# so we can see performance against opponent and at location and next game after date
print("\n===All Players Season Logs===\n")
#player_game_log = all_player_game_logs[0] # init
# player game log from espn, for 1 season or all seasons
#v1
all_players_stats_dicts = {} # similar format as all_means_dicts but for actual stat values so we can display plot stat val over time/game
#v2
#all_players_stats_dicts = generator.generate_all_players_stats_dicts(all_player_season_logs_dict, projected_lines_dict, todays_games_date_obj)
#all_players_records_dicts = generator.generate_all_players_records_dicts(all_players_stats_dicts, projected_lines_dict) # aligned with stats, but record of over projected stat line
# need all_player_season_logs_dict to get game reference info not included in stats dicts such as game date
# display all players records dicts
#writer.display_all_players_records_dicts(all_players_records_dicts, all_player_season_logs_dict)
#all_players_avg_range_dicts = generator.generate_all_players_avg_range_dicts(all_players_stats_dicts)
all_streak_tables = { } # { 'player name': { 'all': {year:[streaks],...}, 'home':{year:streak}, 'away':{year:streak} } }
# need to store all records in dict so we can refer to it by player, condition, year, and stat
all_records_dicts = { } # { 'player name': { 'all': {year: { pts: '1/1,2/2..', stat: record, .. },...}, 'home':{year:{ stat: record, .. },.. }, 'away':{year:{ stat: record, .. }} } }
# { 'player name': { 'all': {year: { pts: 1, stat: mean, .. },...}, 'home':{year:{ stat: mean, .. },.. }, 'away':{year:{ stat: mean, .. }} } }
all_means_dicts = {}
all_medians_dicts = {}
all_modes_dicts = {}
all_mins_dicts = {}
all_maxes_dicts = {}
# loop through player season logs
# to organize stats in dicts by condition
# generate stats dicts from game logs, organized by condition
# essentially if game log input is organized by condition=season, we are organizing by condition=home games, current season, all seasons, etc
# { 'player name': { 'all': {year: { pts: 1, stat: stat val, .. },...}, 'home':{year:{ stat: stat val, .. },.. }, 'away':{year:{ stat: stat val, .. }} } }
for player_name, player_season_logs in all_player_season_logs_dict.items():
#for player_idx in range(len(all_player_game_logs)):
print('\n===' + player_name.title() + '===\n')
season_year = 2023
# get no. games played this season
current_season_log = player_season_logs[0]
current_reg_season_log = determiner.determine_regular_season_games(current_season_log)
num_games_played = len(current_reg_season_log.index) # see performance at this point in previous seasons
# for game_idx, row in current_season_log.iterrows():
# if re.search('\\*',current_season_log.loc[game_idx, 'OPP']): # all star stats not included in regular season stats
# #print("game excluded")
# continue
# if current_season_log.loc[game_idx, 'Type'] == 'Regular':
# num_games_played += 1
all_seasons_pts_dicts = {}
all_seasons_rebs_dicts = {}
all_seasons_asts_dicts = {}
all_seasons_winning_scores_dicts = {}
all_seasons_losing_scores_dicts = {}
all_seasons_minutes_dicts = {}
all_seasons_fgms_dicts = {}
all_seasons_fgas_dicts = {}
all_seasons_fg_rates_dicts = {}
all_seasons_threes_made_dicts = {}
all_seasons_threes_attempts_dicts = {}
all_seasons_threes_rates_dicts = {}
all_seasons_ftms_dicts = {}
all_seasons_ftas_dicts = {}
all_seasons_ft_rates_dicts = {}
all_seasons_bs_dicts = {}
all_seasons_ss_dicts = {}
all_seasons_fs_dicts = {}
all_seasons_tos_dicts = {}
all_seasons_stats_dicts = {'pts':all_seasons_pts_dicts, 'reb':all_seasons_rebs_dicts, 'ast':all_seasons_asts_dicts, 'w score':all_seasons_winning_scores_dicts, 'l score':all_seasons_losing_scores_dicts, 'min':all_seasons_minutes_dicts, 'fgm':all_seasons_fgms_dicts, 'fga':all_seasons_fgas_dicts, 'fg%':all_seasons_fg_rates_dicts, '3pm':all_seasons_threes_made_dicts, '3pa':all_seasons_threes_attempts_dicts, '3p%':all_seasons_threes_rates_dicts, 'ftm':all_seasons_ftms_dicts, 'fta':all_seasons_ftas_dicts, 'ft%':all_seasons_ft_rates_dicts, 'blk':all_seasons_bs_dicts, 'stl':all_seasons_ss_dicts, 'pf':all_seasons_fs_dicts, 'to':all_seasons_tos_dicts} # loop through to add all new stats with 1 fcn
for player_game_log in player_season_logs:
print("\n===Year " + str(season_year) + "===\n")
#player_game_log = player_season_logs[0] #start with current season. all_player_game_logs[player_idx]
#player_name = player_names[player_idx] # player names must be aligned with player game logs
# all_pts_dicts = {'all':{idx:val,..},..}
all_pts_dicts = { 'all':{}, 'home':{}, 'away':{} } # 'opp eg okc':{}, 'day of week eg tue':{}
all_rebs_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_asts_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_winning_scores_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_losing_scores_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_minutes_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_fgms_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_fgas_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_fg_rates_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_threes_made_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_threes_attempts_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_threes_rates_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_ftms_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_ftas_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_ft_rates_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_bs_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_ss_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_fs_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_tos_dicts = { 'all':{}, 'home':{}, 'away':{} }
all_stats_dicts = {'pts':all_pts_dicts, 'reb':all_rebs_dicts, 'ast':all_asts_dicts, 'w score':all_winning_scores_dicts, 'l score':all_losing_scores_dicts, 'min':all_minutes_dicts, 'fgm':all_fgms_dicts, 'fga':all_fgas_dicts, 'fg%':all_fg_rates_dicts, '3pm':all_threes_made_dicts, '3pa':all_threes_attempts_dicts, '3p%':all_threes_rates_dicts, 'ftm':all_ftms_dicts, 'fta':all_ftas_dicts, 'ft%':all_ft_rates_dicts, 'blk':all_bs_dicts, 'stl':all_ss_dicts, 'pf':all_fs_dicts, 'to':all_tos_dicts} # loop through to add all new stats with 1 fcn
# if getting data from player game logs read from internet
# for game log for particular given season/year
# for season in all seasons
if len(player_game_log) > 0:
#season_year = '23'
#print("player_game_log:\n" + str(player_game_log))
# we pulled game log from internet
opponent = projected_lines_dict[player_name]['OPP'].lower() # collect data against opponent to see previous matchups
# first loop thru all regular season games, then thru subset of games such as home/away
# or just append to subset array predefined such as all_home_pts = []
next_game_date_obj = datetime.today() # need to see if back to back games 1 day apart
reg_season_game_log = determiner.determine_regular_season_games(player_game_log)
total_season_games = len(reg_season_game_log.index) # so we can get game num from game idx
# for game_idx, row in reg_season_game_log.iterrows():
# total_season_games += 1
# print('total_season_games with module: ' + str(total_season_games))
# total_season_games = 0 # reset for test
# for game_idx, row in player_game_log.iterrows():
# #game = player_game_log[game_idx, row]
# #print("game:\n" + str(game))
# #print("player_game_log.loc[game_idx, 'Type']: " + player_game_log.loc[game_idx, 'Type'])
# if re.search('\\*',player_game_log.loc[game_idx, 'OPP']): # all star stats not included in regular season stats
# #print("game excluded")
# continue
# if player_game_log.loc[game_idx, 'Type'] == 'Regular':
# #print("Current Game Num: " + str(game_idx))
# total_season_games += 1
#print('total_season_games from length: ' + str(total_season_games))
for game_idx, row in reg_season_game_log.iterrows():
#game = player_game_log[game_idx, row]
#print("game:\n" + str(game))
#print("player_game_log.loc[game_idx, 'Type']: " + player_game_log.loc[game_idx, 'Type'])
# if re.search('\\*',player_game_log.loc[game_idx, 'OPP']): # all star stats not included in regular season stats
# #print("game excluded")
# continue
# group reg season games together for analysis
#if player_game_log.loc[game_idx, 'Type'] == 'Regular':
#print("Current Game Num: " + str(game_idx))
# === Collect Stats for Current Game ===
pts = int(player_game_log.loc[game_idx, 'PTS'])
rebs = int(player_game_log.loc[game_idx, 'REB'])
asts = int(player_game_log.loc[game_idx, 'AST'])
results = player_game_log.loc[game_idx, 'Result']
#print("results: " + results)
results = re.sub('[a-zA-Z]', '', results)
# remove #OT from result string
results = re.split("\\s+", results)[0]
#print("results_data: " + str(results_data))
score_data = results.split('-')
#print("score_data: " + str(score_data))
winning_score = int(score_data[0])
losing_score = int(score_data[1])
minutes = int(player_game_log.loc[game_idx, 'MIN'])
fgs = player_game_log.loc[game_idx, 'FG']
fg_data = fgs.split('-')
fgm = int(fg_data[0])
fga = int(fg_data[1])
fg_rate = round(float(player_game_log.loc[game_idx, 'FG%']), 1)
#threes = game[three_idx]
#threes_data = threes.split('-')
#print("threes_data: " + str(threes_data))
threes_made = int(player_game_log.loc[game_idx, '3PT_SA'])
threes_attempts = int(player_game_log.loc[game_idx, '3PT_A'])
three_rate = round(float(player_game_log.loc[game_idx, '3P%']), 1)
fts = player_game_log.loc[game_idx, 'FT']
ft_data = fts.split('-')
ftm = int(ft_data[0])
fta = int(ft_data[1])
ft_rate = round(float(player_game_log.loc[game_idx, 'FT%']), 1)
bs = int(player_game_log.loc[game_idx, 'BLK'])
ss = int(player_game_log.loc[game_idx, 'STL'])
fs = int(player_game_log.loc[game_idx, 'PF'])
tos = int(player_game_log.loc[game_idx, 'TO'])
game_stats = [pts,rebs,asts,winning_score,losing_score,minutes,fgm,fga,fg_rate,threes_made,threes_attempts,three_rate,ftm,fta,ft_rate,bs,ss,fs,tos] # make list to loop through so we can add all stats to dicts with 1 fcn
# === Add Stats to Dict ===
# now that we have game stats add them to dict by condition
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
stat_dict['all'][game_idx] = stat
if re.search('vs',player_game_log.loc[game_idx, 'OPP']):
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
stat_dict['home'][game_idx] = stat
else: # if not home then away
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
stat_dict['away'][game_idx] = stat
# matchup against opponent
# only add key for current opp bc we dont need to see all opps here
# look for irregular abbrevs like NO and NY
# opponent in form 'gsw' but game log in form 'gs'
game_log_team_abbrev = re.sub('vs|@','',player_game_log.loc[game_idx, 'OPP'].lower()) # eg 'gs'
#print('game_log_team_abbrev: ' + game_log_team_abbrev)
opp_abbrev = opponent # default if regular
#print('opp_abbrev: ' + opp_abbrev)
irregular_abbrevs = {'nop':'no', 'nyk':'ny', 'sas': 'sa', 'gsw':'gs' } # for these match the first 3 letters of team name instead
if opp_abbrev in irregular_abbrevs.keys():
#print("irregular abbrev: " + team_abbrev)
opp_abbrev = irregular_abbrevs[opp_abbrev]
if opp_abbrev == game_log_team_abbrev:
#print('opp_abbrev == game_log_team_abbrev')
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not opponent in stat_dict.keys():
stat_dict[opponent] = {}
stat_dict[opponent][game_idx] = stat
# see if this game is 1st or 2nd night of back to back bc we want to see if pattern for those conditions
init_game_date_string = player_game_log.loc[game_idx, 'Date'].lower().split()[1] # 'wed 2/15'[1]='2/15'
game_mth = init_game_date_string.split('/')[0]
final_season_year = str(season_year)
if int(game_mth) in range(10,13):
final_season_year = str(season_year - 1)
game_date_string = init_game_date_string + "/" + final_season_year
#print("game_date_string: " + str(game_date_string))
game_date_obj = datetime.strptime(game_date_string, '%m/%d/%Y')
#print("game_date_obj: " + str(game_date_obj))
# if current loop is most recent game (idx 0) then today's game is the next game, if current season
# if last game of prev season then next game after idx 0 (bc from recent to distant) is next season game 1
if game_idx == 0: # see how many days after prev game is date of today's projected lines
# already defined or passed todays_games_date_obj
# todays_games_date_obj = datetime.strptime(todays_games_date, '%m/%d/%y')
# print("todays_games_date_obj: " + str(todays_games_date_obj))
current_year = 2023
if season_year == current_year: # current year
next_game_date_obj = todays_games_date_obj # today's game is the next game relative to the previous game
else:
next_game_date_obj = game_date_obj # should be 0 unless we want to get date of next season game
#print("next_game_date_obj: " + str(next_game_date_obj))
# no need to get next game date like this bc we can see last loop
# else: # if not most recent game then we can see the following game in the game log at prev idx
# next_game_date_string = player_game_log.loc[game_idx-1, 'Date'].lower().split()[1] + "/" + season_year
# print("next_game_date_string: " + str(next_game_date_string))
# next_game_date_obj = datetime.strptime(next_game_date_string, '%m/%d/%y')
# print("next_game_date_obj: " + str(next_game_date_obj))
days_before_next_game_int = (next_game_date_obj - game_date_obj).days
days_before_next_game = str(days_before_next_game_int) + ' before'
#print("days_before_next_game: " + days_before_next_game)
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not days_before_next_game in stat_dict.keys():
stat_dict[days_before_next_game] = {}
stat_dict[days_before_next_game][game_idx] = stat
init_prev_game_date_string = ''
if len(player_game_log.index) > game_idx+1:
init_prev_game_date_string = player_game_log.loc[game_idx+1, 'Date'].lower().split()[1]
prev_game_mth = init_prev_game_date_string.split('/')[0]
final_season_year = str(season_year)
if int(prev_game_mth) in range(10,13):
final_season_year = str(season_year - 1)
prev_game_date_string = init_prev_game_date_string + "/" + final_season_year
#print("prev_game_date_string: " + str(prev_game_date_string))
prev_game_date_obj = datetime.strptime(prev_game_date_string, '%m/%d/%Y')
#print("prev_game_date_obj: " + str(prev_game_date_obj))
days_after_prev_game_int = (game_date_obj - prev_game_date_obj).days
days_after_prev_game = str(days_after_prev_game_int) + ' after'
#print("days_after_prev_game: " + days_after_prev_game)
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not days_after_prev_game in stat_dict.keys():
stat_dict[days_after_prev_game] = {}
stat_dict[days_after_prev_game][game_idx] = stat
# add keys for each day of the week so we can see performance by day of week
# only add key for current dow bc we dont need to see all dows here
game_dow = player_game_log.loc[game_idx, 'Date'].lower().split()[0].lower() # 'wed 2/15'[0]='wed'
if current_dow == game_dow:
print("found same game day of week: " + game_dow)
for stat_idx in range(len(all_stats_dicts.values())):
stat_dict = list(all_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not game_dow in stat_dict.keys():
stat_dict[game_dow] = {}
stat_dict[game_dow][game_idx] = stat
print("stat_dict: " + str(stat_dict))
# Career/All Seasons Stats
# if we find a game played on the same day/mth previous seasons, add a key for this/today's day/mth
#today_date_data = todays_games_date.split('/')
#today_day_mth = today_date_data[0] + '/' + today_date_data[1]
#print("today_day_mth: " + str(today_day_mth))
today_day_mth = str(todays_games_date_obj.month) + '/' + str(todays_games_date_obj.day)
#print("today_day_mth: " + str(today_day_mth))
if init_game_date_string == today_day_mth:
print("found same game day/mth in previous season")
for stat_idx in range(len(all_seasons_stats_dicts.values())):
stat_dict = list(all_seasons_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not game_date_string in stat_dict.keys():
stat_dict[game_date_string] = {}
stat_dict[game_date_string][game_idx] = [stat] # we cant use game idx as key bc it gets replaced instead of adding vals
else:
if game_idx in stat_dict[game_date_string].keys():
stat_dict[game_date_string][game_idx].append(stat)
else:
stat_dict[game_date_string][game_idx] = [stat]
print("all_seasons_stats_dicts: " + str(all_seasons_stats_dicts))
# add key for the current game number for this season and add games played from previous seasons (1 per season)
game_num = total_season_games - game_idx # bc going from recent to past
if game_num == num_games_played:
print("found same game num in previous season")
for stat_idx in range(len(all_seasons_stats_dicts.values())):
stat_dict = list(all_seasons_stats_dicts.values())[stat_idx]
stat = game_stats[stat_idx]
if not num_games_played in stat_dict.keys():
stat_dict[num_games_played] = {}
stat_dict[num_games_played][game_idx] = [stat] # we cant use game idx as key bc it gets replaced instead of adding vals
else:
if game_idx in stat_dict[num_games_played].keys():
stat_dict[num_games_played][game_idx].append(stat)
else:
stat_dict[num_games_played][game_idx] = [stat]
print("all_seasons_stats_dicts: " + str(all_seasons_stats_dicts))
# after all keys are set, set next game as current game for next loop
next_game_date_obj = game_date_obj # next game bc we loop from most to least recent
else:
# if getting data from file
player_season_log = reader.read_season_log_from_file(data_type, player_name, 'tsv')
# no matter how we read data, we should have filled all_pts list
if len(all_pts_dicts['all'].keys()) > 0:
# no matter how we get data,
# next we compute relevant results
# first for all then for subsets like home/away
# all_pts_dict = { 'all':[] }
# all_pts_means_dict = { 'all':0, 'home':0, 'away':0 }
# all_pts_medians_dict = { 'all':0, 'home':0, 'away':0 }
# all_pts_modes_dict = { 'all':0, 'home':0, 'away':0 }
# all_pts_min_dict = { 'all':0, 'home':0, 'away':0 }
# all_pts_max_dict = { 'all':0, 'home':0, 'away':0 }
all_stats_counts_dict = { 'all': [], 'home': [], 'away': [] }
# at this point we have added all keys to dict eg all_pts_dict = {'1of2':[],'2of2':[]}
#print("all_pts_dict: " + str(all_pts_dict))
print("all_pts_dicts: " + str(all_pts_dicts))
# all_pts_dicts = {'all':{1:20}}
# key=condition, val={idx:stat}
#compute stats from data
# key represents set of conditions of interest eg home/away
for conditions in all_pts_dicts.keys(): # all stats dicts have same keys so we use first 1 as reference
# reset for each set of conditions
header_row = ['Output']
stat_means = ['Mean'] #{pts:'',reb...}
stat_medians = ['Median']
stat_modes = ['Mode']
stat_mins = ['Min']
stat_maxes = ['Max']
for stat_key, stat_dict in all_stats_dicts.items(): # stat key eg pts
header_row.append(stat_key.upper())
stat_vals = list(stat_dict[conditions].values())
#print("stat_vals: " + str(stat_vals))
stat_mean = round(numpy.mean(stat_vals), 1)
stat_median = int(numpy.median(stat_vals))
stat_mode = stats.mode(stat_vals, keepdims=False)[0]
stat_min = numpy.min(stat_vals)
stat_max = numpy.max(stat_vals)
stat_means.append(stat_mean)
stat_medians.append(stat_median)
stat_modes.append(stat_mode)
stat_mins.append(stat_min)
stat_maxes.append(stat_max)
# save player stats in dict for reference
# save for all stats, not just streaks
# at first there will not be this player name in the dict so we add it
stat_name = stat_key
if stat_name == '3p':
stat_name = '3pm'
# for now assume if all means dicts is populated then median, mode, min and max are as well
if not player_name in all_means_dicts.keys():
all_means_dicts[player_name] = {} # init bc player name key not in dict so if we attempt to set its val it is error
all_medians_dicts[player_name] = {}
all_modes_dicts[player_name] = {}
all_mins_dicts[player_name] = {}
all_maxes_dicts[player_name] = {}
player_means_dicts = all_means_dicts[player_name] # {player name: { condition: { year: { stat: [1/1,2/2,...],.. },.. },.. },.. }
player_medians_dicts = all_medians_dicts[player_name]
player_modes_dicts = all_modes_dicts[player_name]
player_mins_dicts = all_mins_dicts[player_name]
player_maxes_dicts = all_maxes_dicts[player_name]
player_means_dicts[conditions] = {}
player_medians_dicts[conditions] = {}
player_modes_dicts[conditions] = {}
player_mins_dicts[conditions] = {}
player_maxes_dicts[conditions] = {}
player_all_means_dicts = player_means_dicts[conditions]
player_all_medians_dicts = player_medians_dicts[conditions]
player_all_modes_dicts = player_modes_dicts[conditions]
player_all_mins_dicts = player_mins_dicts[conditions]
player_all_maxes_dicts = player_maxes_dicts[conditions]
player_all_means_dicts[season_year] = { stat_name: stat_mean }
player_all_medians_dicts[season_year] = { stat_name: stat_median }
player_all_modes_dicts[season_year] = { stat_name: stat_mode }
player_all_mins_dicts[season_year] = { stat_name: stat_min }
player_all_maxes_dicts[season_year] = { stat_name: stat_max }
else: # player already in list
player_means_dicts = all_means_dicts[player_name]
player_medians_dicts = all_medians_dicts[player_name]
player_modes_dicts = all_modes_dicts[player_name]
player_mins_dicts = all_mins_dicts[player_name]
player_maxes_dicts = all_maxes_dicts[player_name]
if conditions in player_means_dicts.keys():
#print("conditions " + conditions + " in streak tables")
player_all_means_dicts = player_means_dicts[conditions]
player_all_medians_dicts = player_medians_dicts[conditions]
player_all_modes_dicts = player_modes_dicts[conditions]
player_all_mins_dicts = player_mins_dicts[conditions]
player_all_maxes_dicts = player_maxes_dicts[conditions]
if season_year in player_all_means_dicts.keys():
player_all_means_dicts[season_year][stat_name] = stat_mean
player_all_medians_dicts[season_year][stat_name] = stat_median
player_all_modes_dicts[season_year][stat_name] = stat_mode
player_all_mins_dicts[season_year][stat_name] = stat_min
player_all_maxes_dicts[season_year][stat_name] = stat_max
else:
player_all_means_dicts[season_year] = { stat_name: stat_mean }
player_all_medians_dicts[season_year] = { stat_name: stat_median }
player_all_modes_dicts[season_year] = { stat_name: stat_mode }
player_all_mins_dicts[season_year] = { stat_name: stat_min }
player_all_maxes_dicts[season_year] = { stat_name: stat_max }
#player_streak_tables[conditions].append(prob_table) # append all stats for given key
else:
#print("conditions " + conditions + " not in streak tables")
player_means_dicts[conditions] = {}
player_medians_dicts[conditions] = {}
player_modes_dicts[conditions] = {}
player_mins_dicts[conditions] = {}
player_maxes_dicts[conditions] = {}
player_all_means_dicts = player_means_dicts[conditions]
player_all_medians_dicts = player_medians_dicts[conditions]
player_all_modes_dicts = player_modes_dicts[conditions]
player_all_mins_dicts = player_mins_dicts[conditions]
player_all_maxes_dicts = player_maxes_dicts[conditions]
player_all_means_dicts[season_year] = { stat_name: stat_mean }
player_all_medians_dicts[season_year] = { stat_name: stat_median }
player_all_modes_dicts[season_year] = { stat_name: stat_mode }
player_all_mins_dicts[season_year] = { stat_name: stat_min }
player_all_maxes_dicts[season_year] = { stat_name: stat_max }
output_table = [header_row, stat_means, stat_medians, stat_modes, stat_mins, stat_maxes]
output_title = str(conditions).title() + ", " + str(season_year)
if re.search('before',conditions):
output_title = re.sub('Before','days before next game', output_title).title()
elif re.search('after',conditions):
output_title = re.sub('After','days after previous game', output_title).title()
print("\n===" + player_name.title() + " Average and Range===\n")
print(output_title)
print(tabulate(output_table))
# for same set of conditions, count streaks for stats
min_line_hits = 7
game_sample = 10
current_line_hits = 10 # player reached 0+ stats in all 10/10 games. current hits is for current level of points line
pts_count = 0
r_count = 0
a_count = 0
threes_count = 0
b_count = 0
s_count = 0
to_count = 0
all_pts_counts = []
all_rebs_counts = []
all_asts_counts = []
all_threes_counts = []
all_blks_counts = []
all_stls_counts = []
all_tos_counts = []
# prob = 1.0
# while(prob > 0.7):
#if set_sample_size = True: # if we set a sample size only consider those settings. else take all games
#while(current_line_hits > min_line_hits) # min line hits is considered good odds. increase current line hits count out of 10
# if count after 10 games is greater than min line hits then check next level up
for game_idx in range(len(all_pts_dicts[conditions].values())):
pts = list(all_pts_dicts[conditions].values())[game_idx]
rebs = list(all_rebs_dicts[conditions].values())[game_idx]
asts = list(all_asts_dicts[conditions].values())[game_idx]
threes = list(all_threes_made_dicts[conditions].values())[game_idx]
blks = list(all_bs_dicts[conditions].values())[game_idx]
stls = list(all_ss_dicts[conditions].values())[game_idx]
tos = list(all_tos_dicts[conditions].values())[game_idx]
player_projected_lines = projected_lines_dict[player_name]
if pts >= int(player_projected_lines['PTS']):
pts_count += 1
if rebs >= int(player_projected_lines['REB']):
r_count += 1
if asts >= int(player_projected_lines['AST']):
a_count += 1
if threes >= int(player_projected_lines['3PM']):
threes_count += 1
if blks >= int(player_projected_lines['BLK']):
b_count += 1
if stls >= int(player_projected_lines['STL']):
s_count += 1
if tos >= int(player_projected_lines['TO']):
to_count += 1
all_pts_counts.append(pts_count)
all_rebs_counts.append(r_count)
all_asts_counts.append(a_count)
all_threes_counts.append(threes_count)
all_blks_counts.append(b_count)
all_stls_counts.append(s_count)
all_tos_counts.append(to_count)
# make stats counts to find consistent streaks
all_stats_counts_dict[conditions] = [ all_pts_counts, all_rebs_counts, all_asts_counts, all_threes_counts, all_blks_counts, all_stls_counts, all_tos_counts ]
stats_counts = [ all_pts_counts, all_rebs_counts, all_asts_counts, all_threes_counts, all_blks_counts, all_stls_counts, all_tos_counts ]
header_row = ['Games']
over_pts_line = 'PTS ' + str(player_projected_lines['PTS']) + "+"
over_rebs_line = 'REB ' + str(player_projected_lines['REB']) + "+"
over_asts_line = 'AST ' + str(player_projected_lines['AST']) + "+"
over_threes_line = '3PM ' + str(player_projected_lines['3PM']) + "+"
over_blks_line = 'BLK ' + str(player_projected_lines['BLK']) + "+"
over_stls_line = 'STL ' + str(player_projected_lines['STL']) + "+"
over_tos_line = 'TO ' + str(player_projected_lines['TO']) + "+"
prob_pts_row = [over_pts_line]
prob_rebs_row = [over_rebs_line]
prob_asts_row = [over_asts_line]
prob_threes_row = [over_threes_line]
prob_blks_row = [over_blks_line]
prob_stls_row = [over_stls_line]
prob_tos_row = [over_tos_line]
for game_idx in range(len(all_pts_dicts[conditions].values())):
p_count = all_pts_counts[game_idx]
r_count = all_rebs_counts[game_idx]
a_count = all_asts_counts[game_idx]
threes_count = all_threes_counts[game_idx]
b_count = all_blks_counts[game_idx]
s_count = all_stls_counts[game_idx]
to_count = all_tos_counts[game_idx]
current_total = str(game_idx + 1)
current_total_games = current_total# + ' Games'
header_row.append(current_total_games)
prob_over_pts_line = str(p_count) + "/" + current_total
prob_pts_row.append(prob_over_pts_line)
prob_over_rebs_line = str(r_count) + "/" + current_total
prob_rebs_row.append(prob_over_rebs_line)
prob_over_asts_line = str(a_count) + "/" + current_total
prob_asts_row.append(prob_over_asts_line)
prob_over_threes_line = str(threes_count) + "/" + current_total
prob_threes_row.append(prob_over_threes_line)
prob_over_blks_line = str(b_count) + "/" + current_total
prob_blks_row.append(prob_over_blks_line)
prob_over_stls_line = str(s_count) + "/" + current_total
prob_stls_row.append(prob_over_stls_line)
prob_over_tos_line = str(to_count) + "/" + current_total
prob_tos_row.append(prob_over_tos_line)
game_num_header = 'Games Ago'
game_num_row = [game_num_header]
game_day_header = 'DoW'
game_day_row = [game_day_header]
game_date_header = 'Date'
game_date_row = [game_date_header]
for game_num in all_pts_dicts[conditions].keys():
#game_num = all_pts_dicts[key]
game_num_row.append(game_num)
game_day_date = player_game_log.loc[game_num,'Date']
game_day = game_day_date.split()[0]
game_day_row.append(game_day)
game_date = game_day_date.split()[1]
game_date_row.append(game_date)
#total = str(len(all_pts))
#probability_over_line = str(count) + "/" + total
#total_games = total + " Games"
#header_row = ['Points', total_games]
#print(probability_over_line)
#prob_row = [over_line, probability_over_line]
print("\n===" + player_name.title() + " Probabilities===\n")
game_num_table = [game_num_row, game_day_row, game_date_row]
print(tabulate(game_num_table))
prob_pts_table = [prob_pts_row]
print(tabulate(prob_pts_table))
prob_rebs_table = [prob_rebs_row]
print(tabulate(prob_rebs_table))
prob_asts_table = [prob_asts_row]
print(tabulate(prob_asts_table))
prob_threes_table = [prob_threes_row]
print(tabulate(prob_threes_table))
prob_blks_table = [prob_blks_row]
print(tabulate(prob_blks_table))
prob_stls_table = [prob_stls_row]
print(tabulate(prob_stls_table))
prob_tos_table = [prob_tos_row]
print(tabulate(prob_tos_table))
all_prob_stat_tables = [prob_pts_table, prob_rebs_table, prob_asts_table, prob_threes_table, prob_blks_table, prob_stls_table, prob_tos_table]
# stats counts should include all stats
# so we save in dict for reference
for stat_idx in range(len(stats_counts)):
stat_counts = stats_counts[stat_idx]
prob_table = all_prob_stat_tables[stat_idx][0] # only need first element bc previously formatted for table display
# if blk, stl, or to look for 2+
# for all, check to see if 1+ or not worth predicting bc too risky
#stat_line = prob_table[0].split
stat_line = int(prob_table[0].split()[1][:-1]) # [pts 16+, 1/1, 2/2, ..] -> 16
#print('stat_line: ' + str(stat_line))
if stat_line < 2: # may need to change for 3 pointers if really strong likelihood to get 1
continue
stat_name = prob_table[0].split()[0].lower() # [pts 16+, 1/1, 2/2, ..] -> pts
streak = prob_table[1:] # [pts 16+, 1/1, 2/2, ..] -> [1/1,2/2,...]
if determiner.determine_consistent_streak(stat_counts, stat_name) or allow_all:
# { 'player name': { 'all': {year:[streaks],...}, 'home':{year:streak}, 'away':{year:streak} } }
# at first there will not be this player name in the dict so we add it
if player_name in all_streak_tables.keys():
#print(player_name + " in streak tables")
player_streak_tables = all_streak_tables[player_name]
if conditions in player_streak_tables.keys():
#print("conditions " + conditions + " in streak tables")
player_all_season_streaks = player_streak_tables[conditions]
if season_year in player_all_season_streaks.keys():
player_all_season_streaks[season_year].append(prob_table)
else:
player_all_season_streaks[season_year] = [prob_table]
#player_streak_tables[conditions].append(prob_table) # append all stats for given key
else:
#print("conditions " + conditions + " not in streak tables")
player_streak_tables[conditions] = {}
player_all_season_streaks = player_streak_tables[conditions]
player_all_season_streaks[season_year] = [prob_table]
#player_streak_tables[conditions] = [prob_table]
else:
#print(player_name + " not in streak tables")
all_streak_tables[player_name] = {}
player_streak_tables = all_streak_tables[player_name]
#player_streak_tables[conditions] = [prob_table] # v1
# v2
player_streak_tables[conditions] = {} #[prob_table]
player_all_season_streaks = player_streak_tables[conditions]
player_all_season_streaks[season_year] = [prob_table]
#print("player_all_season_streaks: " + str(player_all_season_streaks))
#print("player_streak_tables: " + str(player_streak_tables))
# if key in player_streak_tables.keys():
# player_streak_tables[key].append(prob_table) # append all stats for given key
# else:
# player_streak_tables[key] = [prob_table]
# save player stats in dict for reference
# save for all stats, not just streaks
# at first there will not be this player name in the dict so we add it
if not player_name in all_records_dicts.keys():
all_records_dicts[player_name] = {} # init bc player name key not in dict so if we attempt to set its val it is error
player_records_dicts = all_records_dicts[player_name] # {player name: { condition: { year: { stat: [1/1,2/2,...],.. },.. },.. },.. }
player_records_dicts[conditions] = {}
player_all_records_dicts = player_records_dicts[conditions]
player_all_records_dicts[season_year] = { stat_name: streak }
else: # player already in list
player_records_dicts = all_records_dicts[player_name]
if conditions in player_records_dicts.keys():
#print("conditions " + conditions + " in streak tables")
player_all_records_dicts = player_records_dicts[conditions]
if season_year in player_all_records_dicts.keys():
player_all_records_dicts[season_year][stat_name] = streak
else:
player_all_records_dicts[season_year] = { stat_name: streak }
#player_streak_tables[conditions].append(prob_table) # append all stats for given key
else:
#print("conditions " + conditions + " not in streak tables")
player_records_dicts[conditions] = {}