Created
February 27, 2017 21:18
-
-
Save qharlie/55baffff88e425586cef302b9a9eae8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# ## COPY THIS FOR TRAINING DATA | |
# In[64]: | |
from sklearn.model_selection import train_test_split | |
import charba.api as capi | |
import pandas as pd | |
import numpy as np | |
from nba_py.team import * | |
from nba_py.game import * | |
from nba_py.player import * | |
np.set_printoptions(suppress=True) | |
pd.set_option('display.float_format', lambda x: '%.3f' % x) | |
from underscore import _ | |
from IPython.display import display, HTML | |
import qgrid | |
qgrid.nbinstall(overwrite=True) | |
# In[65]: | |
# GET ALL THE GAMES | |
season_game_history = _.flatten([ capi.get_game_history(TEAMS[key]['id']) for key in TEAMS.keys() ]) | |
# In[66]: | |
# GET ALL TEAM STATS FOR EVERY GAME PLAYED UP UNTIL NOW | |
all_games = pd.DataFrame([x.team_stats() for x in season_game_history]) | |
all_games = all_games[0] | |
df = all_games[0] | |
df | |
# In[67]: | |
def cleanDataframeForLeanring(df): | |
if df.columns[0] == 'GAME_ID': | |
df.drop(df.columns[[-2, -1, 0, 1, 2, 3, 4, 5]], axis=1, inplace=True) | |
to_delete = ['FGM', 'FG3M', 'FTM', 'FGA', 'FG3A', 'FTA','TO'] | |
for key in to_delete: | |
if key in df.columns: | |
del df[key] | |
def createWinnerY(df): | |
return 1 if df.loc[0]['PTS'] > df.loc[1]['PTS'] else 2 | |
# In[68]: | |
# CREATE TRAIN AND TEST DATA | |
from underscore import _ | |
all_X = [] | |
all_y = [] | |
for df in all_games: | |
y = createWinnerY(df) | |
# Drop all the textual elements and leave only usable numbers | |
cleanDataframeForLeanring(df) | |
X = df.values.flatten() | |
print(X) | |
all_X.append(X) | |
all_y.append(y) | |
X_train, X_test, y_train, y_test = train_test_split(all_X, all_y, test_size=0.33, random_state=42) | |
# In[ ]: | |
# ## NOW FOR THE SVM | |
# In[69]: | |
from sklearn.svm import SVC | |
# NOW THE SVM STUFF | |
print(np.shape(all_games)) | |
print(np.shape(all_X)) | |
print(np.shape(X_train)) | |
print(len(y_train)) | |
#print(np.shape(y_train)) | |
X = np.array(X_train) | |
y = y_train | |
#y = np.array(y_train) | |
svc = SVC() | |
svc.fit(X,y) | |
# In[88]: | |
print("ACCURACY FROM TEST SET : " + str(svc.score(np.array(X_test),y_test) )) | |
predictions = svc.predict(X_test) | |
#predictions | |
#print(predictions == y_train) | |
#print(predictions) | |
#print(np.array(y_test)) | |
# In[71]: | |
def createXToPredictOn(home_dict): | |
ret = [] | |
stats = ['FG_PCT', 'FG3_PCT', 'FT_PCT', 'OREB', 'DREB', 'REB', 'AST', 'STL', 'BLK', 'PF'] | |
for key in stats: | |
ret.append(home_dict[key]) | |
return ret | |
# ## PREDICTIONS GO HERE | |
# | |
# In[86]: | |
home_id = TeamNames.MIL | |
away_id = TeamNames.CLE | |
home_stats = capi.get_team_splits(home_id) | |
away_stats = capi.get_team_splits(away_id) | |
#df = pd.DataFrame(splits) | |
#df | |
#df = pd.DataFrame(splits['resultSets'][0]['rowSet'][0], ) | |
# | |
home_dict = dict(zip(home_stats['resultSets'][0]['headers'], home_stats['resultSets'][0]['rowSet'][0])) | |
away_dict = dict(zip(away_stats['resultSets'][0]['headers'], away_stats['resultSets'][0]['rowSet'][0])) | |
# df = pd.DataFrame() | |
#df = pd.DataFrame(splits['resultSets'][0]['rowSet'][0],columns=splits['resultSets'][0]['headers']) | |
# df | |
home_df = pd.DataFrame(home_dict, index=range(1,2)) | |
away_df = pd.DataFrame(away_dict, index=range(1,2)) | |
#display(home) | |
#display(away) | |
home = createXToPredictOn(home_dict) | |
away = createXToPredictOn(away_dict) | |
predictDf = pd.concat([pd.Series(home), pd.Series(away)], axis=1) | |
predictDf = predictDf.transpose() | |
display(predictDf) | |
X = predictDf.values.flatten() | |
display(X) | |
prediction = svc.predict(X) | |
print(prediction) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment