-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnbaopt.py
305 lines (230 loc) · 6.61 KB
/
nbaopt.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# This takes the csv file from fanduel, with a column "points" used at the predictor
# this removes anyone who is injured.
#need to fix the injury indicator colum
## Need to add some handling of the case that the optimal value is spending 0 on a position - perhaps just fill the 0 with lowest value ?
import pandas as pd
import numpy as np
#import pickle
#read the file
current = pd.read_csv('current.csv')
df = pd.read_csv('nbapred.csv')
df = pd.merge(df, current, on = ['Id','Salary','Team'], how = 'left')
df['Full Name']=df['First Name']+' '+df['Last Name']
df['Salary']=df['Salary']/100
df['InjuryIndicator']=df['Injury Indicator'].fillna(0)
for z in df.index:
if df.InjuryIndicator[z] !=0 :
print "injured:", df['First Name'][z],df['Last Name'][z]
df = df.drop(z)
#split into dataframes for each position
PG=df[df['Position'] == 'PG']
SG=df[df['Position'] == 'SG']
SF=df[df['Position'] == 'SF']
PF=df[df['Position'] == 'PF']
C=df[df['Position'] == 'C']
#The following function takes a position (i.e a dataframe )
def reduce_two(dfin):
df=dfin.sort(['Salary'])
m1=0
m2=0
for x in df.index:
if df.FanDuel[x]>m1:
m1=df.FanDuel[x]
continue
if df.FanDuel[x]>m2:
m2=df.FanDuel[x]
continue
df=df.drop(x)
return df
def reduce_one(dfin):
df=dfin.sort(['Salary'])
m1=0
for x in df.index:
if df.FanDuel[x]>m1:
m1=df.FanDuel[x]
continue
df=df.drop(x)
return df
SF=reduce_two(SF)
PG=reduce_two(PG)
PF=reduce_two(PF)
SG=reduce_two(SG)
C=reduce_one(C)
def best_two(dfin): #this takes a dataframe with Salary and FPPG and returns a list from 0 to 600 indicating how much FPPG can be purchased for that ammount. I'm sure there's some clever way to optimize this so I don't test as many cases.
value_list=np.zeros(601)
id_list1 = [0]*601
id_list2 = [0]*601
bestotherrem=0
bestchoicerem=0
for i in range(601):
V = 0
for y in dfin.index:
yother = dfin.drop(y)
yother = yother[yother['Salary'] + dfin.Salary[y]<=i]
try :
bestother = yother.FanDuel.idxmax()
bestvalue = yother.FanDuel[bestother]+dfin.FanDuel[y]
except :
bestother = 0
bestvalue = 0
bestchoice = 0
if bestvalue>V :
V=bestvalue
bestchoice = y
bestotherrem=bestother
bestchoicerem=bestchoice
value_list[i]=V
id_list1[i]=bestchoicerem
id_list2[i]=bestotherrem
return value_list, id_list1, id_list2
def best_one(dfin): #this will return the whole list
value_list=np.zeros(601)
id_list = [0]*601
for i in range(601):
V = 0
df2 = dfin[dfin['Salary']<=i]
try :
bestother = df2.FanDuel.idxmax()
bestvalue = df2.FanDuel[bestother]
except :
bestother = 0
bestvalue = 0
if bestvalue>V :
V=bestvalue
bestcenter = bestother
value_list[i]=V
id_list[i]=bestother
return value_list, id_list
PFV, PF1, PF2 = best_two(PF)
print 'pfv done'
PFdf = pd.DataFrame({'a': PFV, 'b': PF1, 'c':PF2})
#print df.head(250)
SFV,SF1,SF2 = best_two(SF)
print 'sfv done'
PGV,PG1,PG2 = best_two(PG)
print 'pgv done'
SGV,SG1,SG2 = best_two(SG)
print 'sgv done'
"""
pickle.dump(PGV, open( "PG.p", "wb" ) )
pickle.dump(SGV, open( "SG.p", "wb" ) )
pickle.dump(SFV, open( "SF.p", "wb" ) )
pickle.dump(PFV, open( "PF.p", "wb" ) )"""
CV, C1 = best_one(C)
print 'c done'
"""
PGV = pickle.load( open( "PG.p", "rb" ) )
SFV = pickle.load( open( "SF.p", "rb" ) )
PFV = pickle.load( open( "PF.p", "rb" ) )
SGV = pickle.load( open( "SG.p", "rb" ) )"""
# now combine the guards
GG = np.zeros(601)
PGinGG=[0]*601
SGinGG=[0]*601
for i in range(601):
A = PGV[:i+1]
B = SGV[:i+1]
Anull = [x !=0 for x in A] #we're setting up a test to only allow values where both positions are used
An=[int(x) for x in Anull]
Bnull = [x !=0 for x in B] #we're setting up a test to only allow values where both positions are used
Bn=[int(x) for x in Anull]
AB=A+B[::-1]
AB=np.multiply(AB,An)
AB=np.multiply(AB,Bn)
try:
Amax = np.argmax(AB)
ABmax = np.amax(AB)
except:
Amax = 0
ABmax = 0
Bmax = i-Amax
GG[i]=ABmax
SGinGG[i]=Bmax
PGinGG[i]=Amax
# now combine the forwards
FF = np.zeros(601)
PFinFF=[0]*601
SFinFF=[0]*601
for i in range(601):
A = PFV[:i+1]
B = SFV[:i+1]
Anull = [x !=0 for x in A] #we're setting up a test to only allow values where both positions are used
An=[int(x) for x in Anull]
Bnull = [x !=0 for x in B] #we're setting up a test to only allow values where both positions are used
Bn=[int(x) for x in Anull]
AB=A+B[::-1]
AB=np.multiply(AB,An)
AB=np.multiply(AB,Bn)
try:
Amax = np.argmax(AB)
ABmax = np.amax(AB)
except:
Amax = 0
ABmax = 0
Bmax = i-Amax
FF[i]=ABmax
SFinFF[i]=Bmax
PFinFF[i]=Amax
#now combine forwards and gaurds
FFGG = np.zeros(601)
FFinFG=[0]*601
GGinFG=[0]*601
for i in range(601):
A = GG[:i+1]
B = FF[:i+1]
Anull = [x !=0 for x in A] #we're setting up a test to only allow values where both positions are used
An=[int(x) for x in Anull]
Bnull = [x !=0 for x in B] #we're setting up a test to only allow values where both positions are used
Bn=[int(x) for x in Anull]
AB=A+B[::-1]
AB=np.multiply(AB,An)
AB=np.multiply(AB,Bn) #this is a bug - the reversed order isn't here!!!!
try:
Amax = np.argmax(AB)
ABmax = np.amax(AB)
except:
Amax = 0
ABmax = 0
Bmax = i-Amax #something here - need to check this is what it is supposed to be
FFGG[i]=ABmax
GGinFG[i]=Amax
FFinFG[i]=Bmax
#finally combine all with the centers
A = FFGG
B = CV
Anull = [x !=0 for x in A] #we're setting up a test to only allow values where both positions are used
An=[int(x) for x in Anull]
Bnull = [x !=0 for x in B] #we're setting up a test to only allow values where both positions are used
Bn=[int(x) for x in Anull]
AB=A+B[::-1]
AB=np.multiply(AB,An)
AB=np.multiply(AB,Bn)
try:
Amax = np.argmax(AB)
ABmax = np.amax(AB)
except:
Amax = 0
ABmax = 0
Bmax = i-Amax
print "maximum is ", ABmax
print "spend ", Bmax, "on centers"
print "spend", Amax, "on the rest"
Cspend = Bmax
Gspend = GGinFG[Amax]
Fspend = FFinFG[Amax]
print "in particular, spending ", Amax, "on FFGG should give you", FFGG[Amax]
print "this last bit splits into GG:", Gspend, GG[Gspend], "FF:", FFinFG[Amax], FF[Fspend]
print "spend ", PGinGG[Gspend], "on PG and ", SGinGG[Gspend], "on SG"
print "spend ", PFinFF[Fspend], "on PF and ", SFinFF[Fspend], "on SF"
pf1 = PF1[PFinFF[Fspend]]
pf2 = PF2[PFinFF[Fspend]]
sf1 = SF1[SFinFF[Fspend]]
sf2 = SF2[SFinFF[Fspend]]
pg1 = PG1[PGinGG[Gspend]]
pg2 = PG2[PGinGG[Gspend]]
sg1 = SG1[SGinGG[Gspend]]
sg2 = SG2[SGinGG[Gspend]]
center = C1[Cspend]
team = [pf1, pf2, sf1, sf2, pg1, pg2, sg1, sg2, center]
for j in team:
print df['Full Name'][j],df['FPPG'][j], df['FanDuel'][j],df['Salary'][j]