-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNBAAnalysis.py
More file actions
396 lines (327 loc) · 14.1 KB
/
NBAAnalysis.py
File metadata and controls
396 lines (327 loc) · 14.1 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
from decorator import decorator
import tensorflow as tf
import NBAScrapeNode
import pandas as pd
import matplotlib
import numpy as np
from matplotlib import pyplot as plt
@decorator
def on_start(func,*args, **kwargs):
if kwargs !={}:
try:
if kwargs['Start']:
if 'Verbose' in kwargs['Settings']:
if kwargs['Settings']['Verbose']:
print(func)
pass
response= func(*args,**kwargs)
return response
else:
kwargs['Start'] = False
print(func,"DID NOT START")
return(kwargs)
except Exception as e:
print('NODE ERROR OCCURED TRYING TO START NODE FUNCTION:')
print('===========================================')
print(func,e)
print('===========================================')
print('LAST STATE SET TO:')
print('===========================================')
print('ekwargs')
print('===========================================')
print('LAST NODE FUNCTION SET TO:')
print('===========================================')
print('efunc')
print('===========================================')
global ekwargs
global efunc
ekwargs = kwargs
efunc = func
print('HALTING')
raise
else:
print('Empty kwargs')
return ()
def start():
return {'Start':True,'Settings':{'Verbose':True},'Status':{}}
@on_start
def listCreator1(*args,**kwargs):
kwargs['Data']=[]
return kwargs
@on_start
def NBANode2(*args,**kwargs):
node = NBAScrapeNode.StremeNode()
nkwargs = kwargs.copy()
nkwargs['Data']='https://www.basketball-reference.com/players/b/bryanko01.html'
nkwargs['Data'] = node.run(**nkwargs)['Data']
nkwargs['Data']['Player'] = 'Kobe'
nkwargs['Data']= nkwargs['Data'][nkwargs['Data']['Age']>0]
kwargs['Data'].append(nkwargs['Data'])
return kwargs
@on_start
def NBANode3(*args,**kwargs):
node = NBAScrapeNode.StremeNode()
nkwargs = kwargs.copy()
nkwargs['Data']='https://www.basketball-reference.com/players/o/onealsh01.html'
nkwargs['Data'] = node.run(**nkwargs)['Data']
nkwargs['Data']['Player'] = 'Shaq'
nkwargs['Data']= nkwargs['Data'][nkwargs['Data']['Age']>0]
kwargs['Data'].append(nkwargs['Data'])
return kwargs
@on_start
def NBANode4(*args,**kwargs):
node = NBAScrapeNode.StremeNode()
nkwargs = kwargs.copy()
nkwargs['Data']='https://www.basketball-reference.com/players/j/jordami01.html'
nkwargs['Data'] = node.run(**nkwargs)['Data']
nkwargs['Data']['Player'] = 'Jordan'
nkwargs['Data']= nkwargs['Data'][nkwargs['Data']['Age']>0]
kwargs['Data'].append(nkwargs['Data'])
return kwargs
@on_start
def NBANode5(*args,**kwargs):
node = NBAScrapeNode.StremeNode()
nkwargs = kwargs.copy()
nkwargs['Data']='https://www.basketball-reference.com/players/j/jamesle01.html'
nkwargs['Data'] = node.run(**nkwargs)['Data']
nkwargs['Data']['Player'] = 'Lebron'
nkwargs['Data']= nkwargs['Data'][nkwargs['Data']['Age']>0]
kwargs['Data'].append(nkwargs['Data'])
return kwargs
@on_start
def PandasListCondenserReindex(*args, **kwargs): # Expecting list of DataFrames
getDataFrames = [df.sample(5) for df in kwargs['Data'] if isinstance(df,pd.DataFrame)]
kwargs['Data'] = pd.concat(getDataFrames, ignore_index=True)
return kwargs
@on_start
def reFormat7(*args,**kwargs):
kwargs['Data']['subject'] = kwargs['Data']['Player'] + kwargs['Data']['Age'].astype(str)
kwargs['Data'] = kwargs['Data'][['subject','PER','WS','TOV%','VORP']]
return kwargs
class SOM(object):
"""
2-D Self-Organizing Map with Gaussian Neighbourhood function
and linearly decreasing learning rate.
"""
#To check if the SOM has been trained
_trained = False
def __init__(self, m, n, dim, n_iterations=100, alpha=None, sigma=None):
"""
Initializes all necessary components of the TensorFlow
Graph.
m X n are the dimensions of the SOM. 'n_iterations' should
should be an integer denoting the number of iterations undergone
while training.
'dim' is the dimensionality of the training inputs.
'alpha' is a number denoting the initial time(iteration no)-based
learning rate. Default value is 0.3
'sigma' is the the initial neighbourhood value, denoting
the radius of influence of the BMU while training. By default, its
taken to be half of max(m, n).
"""
#Assign required variables first
self._m = m
self._n = n
if alpha is None:
alpha = 0.3
else:
alpha = float(alpha)
if sigma is None:
sigma = max(m, n) / 2.0
else:
sigma = float(sigma)
self._n_iterations = abs(int(n_iterations))
##INITIALIZE GRAPH
self._graph = tf.Graph()
##POPULATE GRAPH WITH NECESSARY COMPONENTS
with self._graph.as_default():
##VARIABLES AND CONSTANT OPS FOR DATA STORAGE
#Randomly initialized weightage vectors for all neurons,
#stored together as a matrix Variable of size [m*n, dim]
self._weightage_vects = tf.Variable(tf.random_normal(
[m*n, dim]))
#Matrix of size [m*n, 2] for SOM grid locations
#of neurons
self._location_vects = tf.constant(np.array(
list(self._neuron_locations(m, n))))
##PLACEHOLDERS FOR TRAINING INPUTS
#We need to assign them as attributes to self, since they
#will be fed in during training
#The training vector
self._vect_input = tf.placeholder("float", [dim])
#Iteration number
self._iter_input = tf.placeholder("float")
##CONSTRUCT TRAINING OP PIECE BY PIECE
#Only the final, 'root' training op needs to be assigned as
#an attribute to self, since all the rest will be executed
#automatically during training
#To compute the Best Matching Unit given a vector
#Basically calculates the Euclidean distance between every
#neuron's weightage vector and the input, and returns the
#index of the neuron which gives the least value
bmu_index = tf.argmin(tf.sqrt(tf.reduce_sum(
tf.pow(tf.subtract(self._weightage_vects, tf.stack(
[self._vect_input for i in range(m*n)])), 2), 1)),
0)
#This will extract the location of the BMU based on the BMU's
#index
slice_input = tf.pad(tf.reshape(bmu_index, [1]),
np.array([[0, 1]]))
bmu_loc = tf.reshape(tf.slice(self._location_vects, slice_input,
tf.constant(np.array([1, 2]))),
[2])
#To compute the alpha and sigma values based on iteration
#number
learning_rate_op = tf.subtract(1.0, tf.math.divide(self._iter_input,
self._n_iterations))
_alpha_op = tf.multiply(alpha, learning_rate_op)
_sigma_op = tf.multiply(sigma, learning_rate_op)
#Construct the op that will generate a vector with learning
#rates for all neurons, based on iteration number and location
#wrt BMU.
bmu_distance_squares = tf.reduce_sum(tf.pow(tf.subtract(
self._location_vects, tf.stack(
[bmu_loc for i in range(m*n)])), 2), 1)
neighbourhood_func = tf.exp(tf.negative(tf.math.divide(tf.cast(
bmu_distance_squares, "float32"), tf.pow(_sigma_op, 2))))
learning_rate_op = tf.multiply(_alpha_op, neighbourhood_func)
#Finally, the op that will use learning_rate_op to update
#the weightage vectors of all neurons based on a particular
#input
learning_rate_multiplier = tf.reshape(learning_rate_op, [self._m * self._n, 1])
weightage_delta = tf.multiply(
learning_rate_multiplier,
tf.subtract(tf.stack([self._vect_input for i in range(m*n)]),
self._weightage_vects))
new_weightages_op = tf.add(self._weightage_vects,
weightage_delta)
self._training_op = tf.assign(self._weightage_vects,
new_weightages_op)
##INITIALIZE SESSION
self._sess = tf.Session()
##INITIALIZE VARIABLES
init_op = tf.global_variables_initializer()
self._sess.run(init_op)
def _neuron_locations(self, m, n):
"""
Yields one by one the 2-D locations of the individual neurons
in the SOM.
"""
#Nested iterations over both dimensions
#to generate all 2-D locations in the map
for i in range(m):
for j in range(n):
yield np.array([i, j])
def train(self, input_vects):
"""
Trains the SOM.
'input_vects' should be an iterable of 1-D NumPy arrays with
dimensionality as provided during initialization of this SOM.
Current weightage vectors for all neurons(initially random) are
taken as starting conditions for training.
"""
#Training iterations
for iter_no in range(self._n_iterations):
#Train with each vector one by one
for input_vect in input_vects:
self._sess.run(self._training_op,
feed_dict={self._vect_input: input_vect,
self._iter_input: iter_no})
#Store a centroid grid for easy retrieval later on
centroid_grid = [[] for i in range(self._m)]
self._weightages = list(self._sess.run(self._weightage_vects))
self._locations = list(self._sess.run(self._location_vects))
for i, loc in enumerate(self._locations):
centroid_grid[loc[0]].append(self._weightages[i])
self._centroid_grid = centroid_grid
self._trained = True
def get_centroids(self):
"""
Returns a list of 'm' lists, with each inner list containing
the 'n' corresponding centroid locations as 1-D NumPy arrays.
"""
if not self._trained:
raise ValueError("SOM not trained yet")
return self._centroid_grid
def map_vects(self, input_vects):
"""
Maps each input vector to the relevant neuron in the SOM
grid.
'input_vects' should be an iterable of 1-D NumPy arrays with
dimensionality as provided during initialization of this SOM.
Returns a list of 1-D NumPy arrays containing (row, column)
info for each input vector(in the same order), corresponding
to mapped neuron.
"""
if not self._trained:
raise ValueError("SOM not trained yet")
to_return = []
for vect in input_vects:
min_index = min([i for i in range(len(self._weightages))],
key=lambda x: np.linalg.norm(vect-
self._weightages[x]))
to_return.append(self._locations[min_index])
return to_return
@on_start
def SimpleSOM(*args, **kwargs):
df = kwargs['Data']
kwargs['Settings']['SOM']={'Data':df}
params = df.keys()[df.keys()!='subject']
testdata = df[params].values
subjects = df.subject
# Train a 20x30 SOM with 400 iterations
som = SOM(20, 30, len(params), 400)
kwargs['Data']=som
som.train(testdata)
# Get output grid
#kwargs['Data']['Centroids'] = som.get_centroids()
# Map colors to their closest neurons
kwargs['Settings']['SOM']['Mapped']=som.map_vects(testdata)
return kwargs
@on_start
def addPlot9(*args,**kwargs):
matplotlib.rcParams['xtick.major.size'] = 0
matplotlib.rcParams['ytick.major.size'] = 0
matplotlib.rcParams['xtick.labelsize'] = 0
matplotlib.rcParams['ytick.labelsize'] = 0
# Get output grid
centroids = np.array(kwargs['Data'].get_centroids())
centroids[:,:,0] = centroids[:,:,0]/centroids[:,:,0].max()
centroids[:,:,1] = centroids[:,:,1]/centroids[:,:,1].max()
centroids[:,:,2] = centroids[:,:,2]/centroids[:,:,2].max()
centroids[:,:,3] = centroids[:,:,3]/centroids[:,:,3].max()
subjects = kwargs['Settings']['SOM']['Data'].subject
if 'Figures' not in kwargs['Settings']:
kwargs['Settings']['Figures'] ={}
kwargs['Settings']['Figures']['Self Organizing Map'] = plt.figure('Self Organizing Map')
plt.imshow(centroids)
for i, m in enumerate(kwargs['Settings']['SOM']['Mapped']):
plt.text(
m[1],
m[0],
subjects[i],
ha='center',
va='center',
bbox=dict(
facecolor='white',
alpha=0.5,
lw=0))
plt.show() # Remove this line if you do not want your program to wait for this figure to render
return kwargs
class StremeNode:
def __init__(self):
pass
def run(self,*args,**kwargs):
self.kwargs=addPlot9(**SimpleSOM(**reFormat7(**PandasListCondenserReindex(**NBANode5(**NBANode4(**NBANode3(**NBANode2(**listCreator1(**kwargs)))))))))
return (self.kwargs)
class liveprocess:
def __init__(self):
self.status="pending"
def run(self,expname):
self.response=addPlot9(**SimpleSOM(**reFormat7(**PandasListCondenserReindex(**NBANode5(**NBANode4(**NBANode3(**NBANode2(**listCreator1(**start())))))))))
self.status="completed"
return(self.status)
if __name__ == '__main__':
process = liveprocess()
process.run('Local')