Last active
February 16, 2017 04:33
-
-
Save qharlie/51078d43a650996620db9352520d9054 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[113]: | |
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 _ | |
import qgrid | |
qgrid.nbinstall(overwrite=True) | |
# In[114]: | |
# GET ALL THE GAMES | |
season_game_history = _.flatten([ capi.get_game_history(TEAMS[key]['id']) for key in TEAMS.keys() ]) | |
# In[115]: | |
# GET ALL TEAM STATS FOR EVERY GAME PLAYED UP UNTIL NOW | |
all_games = pd.DataFrame(_.flatten([x.team_stats() for x in season_game_history])) | |
all_games = all_games[0] | |
print(df.loc[0].tolist()) | |
def createWinnerY(df): | |
y = 0 | |
if df.loc[0]['PTS'] > df.loc[1]['PTS']: | |
y = 1 # df.loc[0]['GAME_ID'] | |
else: | |
y = 2 # df.loc[1]['GAME_ID'] | |
return y | |
# In[117]: | |
# CREATE TRAIN AND TEST DATA | |
from underscore import _ | |
print(type(_)) | |
all_X = [] | |
all_y = [] | |
for df in all_games: | |
y = createWinnerY(df) | |
# Drop all the textual elements and leave only usable numbers | |
if df.columns[0] == 'GAME_ID': | |
df.drop(df.columns[[0, 1, 2, 3, 4, 5]], axis=1, inplace=True) | |
all_X.append(df.values.flatten()) | |
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) | |
# ## NOW FOR THE SVM | |
# In[118]: | |
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) | |
clf = SVC() | |
clf.fit(X,y) | |
# ## PREDICTIONS GO HERE | |
# | |
# In[125]: | |
print("ACCURACY FROM TEST SET : " + str(clf.score(X_test,y_test) )) | |
predictions = clf.predict(X_test) | |
predictions | |
X_test[0] | |
all_X | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment