/
nba.py
176 lines (142 loc) · 6.84 KB
/
nba.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
#Contains functions for nba predictions
import json
from pandas import DataFrame
from pandas import Series
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
def team_aggregates():
team_ids = {'POR':1610612757, 'LAC':1610612746, 'HOU':1610612745, 'MIN':1610612750, 'OKC':1610612760, 'PHX':1610612756, 'MIA':1610612748, 'DAL':1610612742, 'SAS':1610612759, 'GSW':1610612744, 'DEN':1610612743, 'ATL':1610612737, 'LAL':1610612747, 'SAC':1610612758, 'DET':1610612765, 'WAS':1610612764, 'TOR':1610612761, 'PHI':1610612755, 'NOP':1610612740, 'IND':1610612754, 'BKN':1610612751, 'NYK':1610612752, 'CLE':1610612739, 'ORL':1610612753, 'MEM':1610612763, 'BOS':1610612738, 'CHA':1610612766, 'UTA':1610612762, 'MIL':1610612749, 'CHI':1610612741}
loaded_data = json.load(open('aggre_data.txt'))
key = team_ids.keys()[team_ids.values().index(1610612757)]
aggre_dict = {}
headers = ['W%', 'FGM', 'FGA', 'FG%', '3FGM', '3FGA', '3FG%', 'FTM', 'FTA', 'FT%', 'OREB', 'DREB', 'REB', 'AST', 'TOV', 'STL', 'BLK', 'BLKA', 'PF', 'PFD', 'PTS', 'O/U']
for i in loaded_data['rowSet']:
key = team_ids.keys()[team_ids.values().index(i[0])]
aggre_dict[key] = Series(i)
aggre_dict[key] = aggre_dict[key].drop(0)
aggre_dict[key] = aggre_dict[key].drop(1)
aggre_dict[key] = aggre_dict[key].drop(2)
aggre_dict[key] = aggre_dict[key].drop(3)
aggre_dict[key] = aggre_dict[key].drop(4)
aggre_dict[key] = aggre_dict[key].drop(6)
aggre_dict[key] = aggre_dict[key].drop(28)
aggre_dict[key] = aggre_dict[key].drop(29)
aggre_dict[key] = aggre_dict[key].reset_index()
aggre_dict[key] = aggre_dict[key].drop('index', axis=1)
aggre_dict[key] = DataFrame(aggre_dict[key]).T
aggre_dict[key].columns = headers
print key
return aggre_dict
def as_dict(symbols, cols):
loaded_data = json.load(open('dictdata.txt'))
cumsums = {}
for k in symbols:
print k
x = loaded_data[k]
team = []
for i in range(len(x)-1):
formatted = [str(x[i][2])]
temp = str(x[i][3]).split()
formatted.append(temp[0])
formatted.append(temp[2])
formatted.append(str(x[i][4]))
if temp[1] == 'vs.':
temp[1] = 0
else:
temp[1] = 1
formatted.append(temp[1])
for j in range(5,24):
formatted.append(float(x[i][j]))
team.append(formatted)
team.reverse()
cumsums[k] = DataFrame(team, columns = cols)
#Calculate cumulative averages-------------------------
for i in range(6,24):
for j in range(len(cumsums[k].ix[:,1])):
try:
#cumsums[k].ix[j,i] = cumsums[k].ix[0:j,i].mean()
cumsums[k].ix[j,i] = cumsums[k].ix[0:len(cumsums[k].ix[:,1]),i].mean()
except:
break
mins = []
for i in range(len(cumsums[k].ix[:,1])):
mins.append(cumsums[k].ix[0:i,5].sum())
for i in range(len(cumsums[k].ix[:,1])):
cumsums[k].ix[i,5] = mins[i]
return cumsums
def arrange_dict(cumsums, symbols):
cols = cumsums['ATL'].columns.tolist()
cols2 = (cumsums['ATL'].ix[:,5:].columns + '1').tolist()
cols.extend(cols2)
ATL = DataFrame(columns = cols)
for team in symbols:
for Date in cumsums[team]['Date']:
Opponent = cumsums[team].ix[cumsums[team]['Date'] == Date, 'Opponent'].all()
for Opponent_Date in cumsums[Opponent]['Date']:
if Opponent_Date == Date:
op_index = cumsums[Opponent].ix[cumsums[Opponent]['Date'].copy() == Date].ix[:,5:].index[0] - 1
te_index = cumsums[team].ix[cumsums[team]['Date'].copy() == Date].ix[:,0:].index[0] - 1
try:
op_temp = DataFrame(cumsums[Opponent].ix[op_index, 5:]).T
te_temp = DataFrame(cumsums[team].ix[te_index, 0:]).T
te_temp['Date'] = Date
te_temp['Opponent'] = cumsums[team].ix[te_index + 1,'Opponent']
te_temp['WL'] = cumsums[team].ix[te_index + 1,'WL']
op_temp = op_temp.reset_index()
te_temp = te_temp.reset_index()
op_temp = op_temp.ix[:,1:]
te_temp = te_temp.ix[:,1:]
op_temp.columns = op_temp.columns + '1'
atl = pd.concat([te_temp, op_temp], axis = 1)
ATL = pd.concat([ATL, atl], axis = 0)
break
except:
break
print team
ATL.to_csv('final.csv', sep=',', index=False)
def arrange_aggregates(cumsums, symbols, aggs):
for i in symbols:
cumsums[i] = cumsums[i].ix[:,0:5]
cols = cumsums['ATL'].columns.tolist()
cols2 = aggs['ATL'].columns.tolist()
cols3 = (aggs['ATL'].columns + '1').tolist()
cols.extend(cols2)
cols.extend(cols3)
ATL = DataFrame(columns = cols)
for team in symbols:
for Date in cumsums[team]['Date']:
Opponent = cumsums[team].ix[cumsums[team]['Date'] == Date, 'Opponent'].all()
cumsums_temp = cumsums[team].ix[cumsums[team]['Date'] == Date]
cumsums_temp = cumsums_temp.reset_index()
team_temp = aggs[team]
oppenent_temp = DataFrame(aggs[Opponent])
oppenent_temp.columns = cols3
atl = pd.concat([cumsums_temp, team_temp, oppenent_temp], axis = 1)
atl = atl.drop('index', axis=1)
atl.columns = cols
ATL = pd.concat([ATL, atl], axis = 0)
print team
ATL.to_csv('final.csv', sep=',', index=False)
def validate_nba(trees=5, workers=1):
print 'Validating predictions.'
data = pd.read_csv('final.csv')
m = RandomForestClassifier(n_estimators = trees, n_jobs=workers)
predictions = []
tally = []
for i in range(0,len(data.ix[:,0])):
train = data.drop(i, axis = 0)
test = DataFrame(data.ix[i,:]).T
m.fit(train.ix[:,4:], train.ix[:,'WL'])
current_pred = m.predict(test.ix[:,4:])[0]
predictions.append(current_pred)
if str(current_pred) == str(test.ix[:,3].values[0]):
tally.append(1)
else:
tally.append(0)
current_score = round((float(sum(tally))/len(tally)) * 100, ndigits=1)
print str(round((float(i)/len(data.ix[:,0]) * 100), ndigits=1)) + '%. ' + str(current_score) + '% correct.'
preds = Series(predictions)
wl = Series(data.ix[:,'WL'])
correct = float((preds == wl).sum())
percent_correct = round(correct / (len(wl)) * 100, ndigits=2)
print "Predictions were " + str(percent_correct) + "% correct."