-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbaPredict.py
executable file
·95 lines (64 loc) · 4.21 KB
/
nbaPredict.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
# nbaPredict.py - Predicts results of NBA games on a specified date
# Call makeInterpretPrediction with current date, season, and start date of season to run predictions
import pickle
import pandas as pd
from getDailyMatchups import dailyMatchupsPresent
from createModel import createMeanStandardDeviationDicts, zScoreDifferential
from availableStats import availableStats
from getStats import getStatsForTeam
from configureCWD import setCurrentWorkingDirectory
# Returns list of games with Z-Score differentials between teams to be put into a Pandas dataframe
# startDate & endDate should be 'mm/dd/yyyy' form
def dailyGamesDataFrame(dailyGames, meanDict, standardDeviationDict, startDate, endDate, season):
fullDataFrame = []
for homeTeam,awayTeam in dailyGames.items():
homeTeamStats = getStatsForTeam(homeTeam, startDate, endDate, season)
awayTeamStats = getStatsForTeam(awayTeam, startDate, endDate, season)
currentGame = [homeTeam,awayTeam]
for stat,statType in availableStats.items(): # Finds Z Score Dif for stats listed above and adds them to list
zScoreDif = zScoreDifferential(homeTeamStats[stat], awayTeamStats[stat], meanDict[stat], standardDeviationDict[stat])
currentGame.append(zScoreDif)
fullDataFrame.append(currentGame) # Adds this list to list of all games on specified date
return(fullDataFrame)
# Returns a list
# Index 0 is the dailyGames in dict form {Home:Away}
# Index 1 is a list with the prediction probabilities for each game [[lossProb, winProb]]
# currentDate should be in form 'mm/dd/yyyy' and season in form 'yyyy-yy'
def predictDailyGames(currentDate, season, startOfSeason):
dailyGames = dailyMatchupsPresent(currentDate) # Gets all games for specified date
meanDict, standardDeviationDict = createMeanStandardDeviationDicts(startOfSeason, currentDate, season)
dailyGamesList = dailyGamesDataFrame(dailyGames, meanDict, standardDeviationDict, startOfSeason, currentDate, season)
# Pandas dataframe holding daily games and Z-Score differentials between teams
gamesWithZScoreDifs = pd.DataFrame(
dailyGamesList,
columns=['Home', 'Away', 'W_PCT', 'REB', 'TOV', 'PLUS_MINUS', 'OFF_RATING', 'DEF_RATING', 'TS_PCT']
)
justZScoreDifs = gamesWithZScoreDifs.loc[:,'W_PCT':'TS_PCT'] # Slices only the features used in the model
with open('finalized_model.pkl', 'rb') as file: # Change filename here if model is named differently
pickleModel = pickle.load(file)
predictions = pickleModel.predict_proba(justZScoreDifs) # Predicts the probability that the home team loses/wins
gamesWithPredictions = [dailyGames, predictions]
return gamesWithPredictions
# Returns the percent chance that the home team will defeat the away team for each game
# gamesWithPredictions should be in form [dailyGames, predictionsList]
def interpretPredictions(gamesWithPredictions):
dailyGames = gamesWithPredictions[0] # Dict holding daily matchups
probabilityPredictions = gamesWithPredictions[1] # List of lists holding probs of loss/win for home team
for gameNum in range(len(probabilityPredictions)): # Loops through each game
winProb = probabilityPredictions[gameNum][1]
winProbRounded = round(winProb,4)
winProbPercent = "{:.2%}".format(winProbRounded) # Formulates percent chance that home team wins
homeTeam = list(dailyGames.keys())[gameNum]
awayTeam = list(dailyGames.values())[gameNum]
print('There is a ' + winProbPercent + ' chance that the ' + homeTeam + ' will defeat the ' + awayTeam + '.')
# Fetches games on set date and returns predictions for each game
# currentDate/startOfSeason should be in form 'mm/dd/yyyy' and season in form 'yyyy-yy'
# Start of 2019-20 season was 10/2/2019
def makeInterpretPredictions(currentDate, season, startOfSeason):
setCurrentWorkingDirectory('SavedModels')
print('Predictions for ' + currentDate + ':')
predictions = predictDailyGames(currentDate, season, startOfSeason)
interpretPredictions(predictions)
# EDIT THIS
# First arg is date to predict (mm/dd/yyyy), second is season (yyyy-yy), and third is start date of season (mm/dd/yyyy)
makeInterpretPredictions('01/04/2020', '2019-20', '10/22/2019')