forked from seanahmad/Predict-NBA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardizeStats.py
executable file
·61 lines (47 loc) · 3.15 KB
/
standardizeStats.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
# standardizeStats.py - Uses Z Scores ((Obs - Mean) / St Dev.) to standardize any of the different statistics scraped
from nba_api.stats.endpoints import leaguedashteamstats
import statistics
from getStats import getStatsForTeam
import time
from customHeaders import customHeaders
# Finds league mean for the entered basic or advanced statistic (statType = 'Base' or 'Advanced')
def basicOrAdvancedStatMean(startDate, endDate, stat,statType = 'Base', season='2018-19'):
time.sleep(.2)
# Gets list of dictionaries with stats for every team
allTeamsInfo = leaguedashteamstats.LeagueDashTeamStats(per_mode_detailed='Per100Possessions',
measure_type_detailed_defense=statType,
date_from_nullable=startDate,
date_to_nullable=endDate,
season=season,
headers=customHeaders,
timeout=120)
allTeamsDict = allTeamsInfo.get_normalized_dict()
allTeamsList = allTeamsDict['LeagueDashTeamStats']
specificStatAllTeams = []
for i in range(len(allTeamsList)): # Loops through and appends specific stat to new list until every team's stat has been added
specificStatAllTeams.append(allTeamsList[i][stat])
mean = statistics.mean(specificStatAllTeams) # Finds mean of stat
return mean
# Finds league standard deviation for the entered basic or advanced statistic (statType = 'Base' or 'Advanced')
def basicOrAdvancedStatStandardDeviation(startDate, endDate, stat,statType = 'Base', season='2018-19'):
time.sleep(.2)
# Gets list of dictionaries with stats for every team
allTeamsInfo = leaguedashteamstats.LeagueDashTeamStats(per_mode_detailed='Per100Possessions',
measure_type_detailed_defense=statType,
date_from_nullable=startDate,
date_to_nullable=endDate,
season=season,
headers=customHeaders,
timeout=120
)
allTeamsDict = allTeamsInfo.get_normalized_dict()
allTeamsList = allTeamsDict['LeagueDashTeamStats']
specificStatAllTeams = []
for i in range(len(allTeamsList)): # Loops and appends specific stat to new list until every team's stat has been added
specificStatAllTeams.append(allTeamsList[i][stat])
standardDeviation = statistics.stdev(specificStatAllTeams) # Finds standard deviation of stat
return standardDeviation
# Returns a standardized version of each data point via the z-score method
def basicOrAdvancedStatZScore(observedStat, mean, standardDeviation):
zScore = (observedStat-mean)/standardDeviation # Calculation for z-score
return(zScore)