-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNBA_Player_Analysis.py
394 lines (351 loc) · 14.9 KB
/
NBA_Player_Analysis.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
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
"""
Python Program that:
1. Uses career data of players from 1990-2010, PCA transforms to 5 dimensions, and
performs k means and k medoids clustering into six groups
2. Uses second year stats of players from 1990-2010 and finds clusters averages
3. Place cluster assignments for current second year players. Use cluster averages
of 1990-2010 players to measure euclidean distances to second year players. Place
second year players in closest cluster
4. Visualize data and results through graphs, tables, and spreadsheets
"""
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn_extra.cluster import KMedoids
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics.pairwise import pairwise_distances
import requests
import re
import csv
from bs4 import BeautifulSoup
from sklearn.preprocessing import MinMaxScaler
class Main(object):
"""
init function. second_yrs_current_season contains current player stats, second_yrs_1990_2010_season
contain stats of historical second year players, and second_yrs_1990_2010_career contains historical
career data.
Career data was initially scraped with scrape_player_career() function
Career data is scaled and normalized, goes through PCA transform, and then clustered and visualized
in historic_players_projections.
current_players_projections takes historical data clusters and clusters current players along with
visualization and spreadsheets creation of players per cluster.
"""
def __init__(self):
# all current (2019-2020) second-year players
# these are the players we are predicting the future of
second_yrs_current_season_pd = pd.read_csv('data/second_yrs_season_2019-2020.csv', header=1)
second_yrs_current_season_players_list = second_yrs_current_season_pd.iloc[:,1].to_numpy()
second_yrs_current_season = second_yrs_current_season_pd.to_numpy()[0:,8:32].astype(float)
second_yrs_current_season = np.nan_to_num(second_yrs_current_season)
# list of players and their second-year stats for those who played 1990-2010
second_yrs_1990_2010_season_pd = pd.read_csv('data/second_yrs_season_1990-2010.csv', header=1)
second_yrs_1990_2010_season = second_yrs_1990_2010_season_pd.to_numpy()[0:,8:32].astype(float)
second_yrs_1990_2010_season = np.nan_to_num(second_yrs_1990_2010_season)
# extract player_list from second_yrs_1990_2010_season
player_list = second_yrs_1990_2010_season_pd.iloc[:,1]
player_list = player_list.str.split('\\').str[1]
#
#
# run in order to generate second_yrs_career_1990-2010.csv file
# note this is already generated
#self.scrape_player_career(player_list)
#
#
# second_yrs_career_1990-2010.csv is the career stats for
# players who played second season between 1990 and 2010
second_yrs_1990_2010_career_pd = pd.read_csv('data/second_yrs_career_1990-2010.csv')
second_yrs_1990_2010_career = second_yrs_1990_2010_career_pd.to_numpy()[0:,:24].astype(float)
second_yrs_1990_2010_career = np.nan_to_num(second_yrs_1990_2010_career)
# normalize data before performing PCA
scaler = MinMaxScaler(feature_range=(0,1))
second_yrs_1990_2010_career_normalized = scaler.fit_transform(second_yrs_1990_2010_career)
# PCA Dimension reduction of career data
pca = PCA(n_components=5)
pca.fit(second_yrs_1990_2010_career_normalized)
#variance = pca.explained_variance_ratio_
reduced_representation_career_data = pca.transform(second_yrs_1990_2010_career_normalized)
# cluster and analyze historic players
career_labels_kmeans, career_labels_kmedoids = self.historic_players_projections(reduced_representation_career_data, player_list, second_yrs_1990_2010_career_pd)
#cluster and analyze current second year players
self.current_players_projections(career_labels_kmeans, career_labels_kmedoids, second_yrs_1990_2010_season, second_yrs_current_season, second_yrs_current_season_players_list)
"""
Function that creates pyplot tables of data
"""
def table_creator(self, title, table_data):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
table = ax.table(cellText=table_data, loc='center')
table.auto_set_font_size(False)
table.set_fontsize(8)
ax.axis('off')
plt.title(title)
plt.show()
"""
Function used to gather total all star and all nba selectinos for historical players
"""
def extract_bling_info(self, soup):
""" number of All-star and All-NBA selections can be found in the "bling" element """
ALL_STAR, ALL_NBA = 0, 0
bling = soup.find(id="bling")
if bling:
bling = bling.find_all('li')
for b in bling:
text = b.a.text
if text.find("All Star") != -1:
if text.find("x") != -1:
ALL_STAR = int(text.split("x")[0])
else:
ALL_STAR = 1
elif text.find("All-NBA") != -1:
if text.find("x") != -1:
ALL_NBA = int(text.split("x")[0])
else:
ALL_NBA = 1
return ALL_STAR, ALL_NBA
"""
Function used to scrape and create csv file for career stats of historical players
"""
def scrape_player_career(self, player_list):
"""
given player ID, return player's career averages as pandas data frame
"""
out_path = 'second_yrs_career_1990-2010.csv'
csv_file = open(out_path, 'w')
csv_writer = csv.writer(csv_file)
features = ['Cluster 0','Cluster 1','Cluster 2','Cluster 2','Cluster 2','Cluster 2','3P','3PA','FT','FTA','ORB','DRB','TRB',
'AST','STL','BLK','TOV','PF','PTS','FG%','2P%','3P%','eFG%','FT%','ALL_STAR','ALL_NBA']
csv_writer.writerow(features)
row = [GS_PCT,MP,FG,FGA,TWO_P,TWO_PA,THREE_P,THREE_PA,FT,FTA,ORB,DRB,TRB,
AST,STL,BLK,TOV,PF,PTS,FG_PCT,TWO_PCT,THREE_PCT,EFG_PCT,FT_PCT,ALL_STAR,ALL_NBA]
csv_writer.writerow(row)
for i in range(len(player_list)):
player_id = player_list.iloc[i]
URL = 'https://www.basketball-reference.com/players/{}/{}.html'.format(player_id[0], player_id)
r = requests.get(URL)
soup = BeautifulSoup(r.text, "html5lib")
# extract desired columns from per-game table, career row
# note each has a try except due to missing data for some players
# example: if player has never attempted a three point shot
career_per = soup.find(id="per_game").tfoot.contents[0]
try:
G = career_per.contents[5].contents[0]
except:
G = 0
try:
GS = career_per.contents[6].contents[0]
except:
GS = 0
if int(G) == 0:
GS_PCT = 0
else:
GS_PCT = round(int(GS)/int(G), 2)
try:
MP = career_per.contents[7].contents[0]
except:
MP = 0
try:
FG = career_per.contents[8].contents[0]
except:
FG = 0
try:
FGA = career_per.contents[9].contents[0]
except:
FGA = 0
try:
FG_PCT = career_per.contents[10].contents[0]
except:
FG_PCT = 0
try:
THREE_P = career_per.contents[11].contents[0]
except:
THREE_P = 0
try:
THREE_PA = career_per.contents[12].contents[0]
except:
THREE_PA = 0
try:
THREE_PCT = career_per.contents[13].contents[0]
except:
THREE_PCT = 0
try:
TWO_P = career_per.contents[14].contents[0]
except:
TWO_P = 0
try:
TWO_PA = career_per.contents[15].contents[0]
except:
TWO_PA = 0
try:
TWO_PCT = career_per.contents[16].contents[0]
except:
TWO_PCT = 0
try:
EFG_PCT = career_per.contents[17].contents[0]
except:
EFG_PCT = 0
try:
FT = career_per.contents[18].contents[0]
except:
FT = 0
try:
FTA = career_per.contents[19].contents[0]
except:
FTA = 0
try:
FT_PCT = career_per.contents[20].contents[0]
except:
FT_PCT = 0
try:
ORB = career_per.contents[21].contents[0]
except:
ORB = 0
try:
DRB = career_per.contents[22].contents[0]
except:
DRB = 0
try:
TRB = career_per.contents[23].contents[0]
except:
TRB = 0
try:
AST = career_per.contents[24].contents[0]
except:
AST = 0
try:
STL = career_per.contents[25].contents[0]
except:
STL = 0
try:
BLK = career_per.contents[26].contents[0]
except:
BLK = 0
try:
TOV = career_per.contents[27].contents[0]
except:
TOV = 0
try:
PF = career_per.contents[28].contents[0]
except:
PF = 0
try:
PTS = career_per.contents[29].contents[0]
except:
PTS = 0
ALL_STAR, ALL_NBA = extract_bling_info(soup)
row = [GS_PCT,MP,FG,FGA,TWO_P,TWO_PA,THREE_P,THREE_PA,FT,FTA,ORB,DRB,TRB,
AST,STL,BLK,TOV,PF,PTS,FG_PCT,TWO_PCT,THREE_PCT,EFG_PCT,FT_PCT,ALL_STAR,ALL_NBA]
csv_writer.writerow(row)
"""
The below clusters nba players from 1990 to 2010 into six groups with kmeans and kmedoids
Additionally, the below displays each cluster in excel spreadsheets. and displays pypot tables.
One table describes average cluster characteristics and the other number of members per cluster.
"""
def historic_players_projections(self, reduced_representation_career_data, player_list, second_yrs_1990_2010_career_pd):
all_stars = second_yrs_1990_2010_career_pd.to_numpy()[0:,24].astype(float)
all_nbas = second_yrs_1990_2010_career_pd.to_numpy()[0:,25].astype(float)
gs_percents = second_yrs_1990_2010_career_pd.to_numpy()[0:,0].astype(float)
minutes_per_game = second_yrs_1990_2010_career_pd.to_numpy()[0:,1].astype(float)
for classifier in ['KMeans', 'KMedoids']:
# kmeans clustering of career data into 6 clusters
if classifier == 'KMeans':
print('KMEANS CLUSTERING Historic Players')
clf = KMeans(n_clusters=6, random_state=0).fit(reduced_representation_career_data)
career_labels_kmeans = clf.labels_
else:
print('KMEDOIDS CLUSTERING Historic Players')
clf = KMedoids(n_clusters=6, random_state=0).fit(reduced_representation_career_data)
career_labels_kmedoids = clf.labels_
career_labels = clf.labels_ # cluster labels
career_centers = clf.cluster_centers_ # cluster centers; interesting to see which players
# Graphing cluster assignment data points
# using only 2 dimensions of PCA
fig, ax = plt.subplots()
scatter = ax.scatter(reduced_representation_career_data[:, 0], reduced_representation_career_data[:, 1], c=career_labels, s=50, cmap='Accent')
# produce a legend with the unique colors from the scatter
legend = ax.legend(*scatter.legend_elements(),
loc="best", title="Clusters")
ax.add_artist(legend)
plt.scatter(career_centers[:, 0], career_centers[:, 1], c='black', s=200, alpha=0.7)
plt.title(classifier + ' Clustering')
plt.show()
# categorical averages per cluster including: all star appearances, all nba selections,
# percent games started, average minutes per game, and players per cluster
career_all_stars_averages = np.zeros(len(set(career_labels)))
career_all_nba_averages = np.zeros(len(set(career_labels)))
gs_percents_averages = np.zeros(len(set(career_labels)))
minutes_per_game_averages = np.zeros(len(set(career_labels)))
players_per_cluster = np.zeros(len(set(career_labels)))
player_names = []
for i in range(len(set(career_labels))):
# indices of particular cluster
indices = np.where(career_labels == i)[0]
# average all star appearances per cluster
all_stars_average = np.average(all_stars[indices])
career_all_stars_averages[i] = all_stars_average
# average all nba appearances per cluster
all_nba_average = np.average(all_nbas[indices])
career_all_nba_averages[i] = all_nba_average
# average game start percentagers per cluster
gs_percents_average = np.average(gs_percents[indices])
gs_percents_averages[i] = gs_percents_average*100
# average minutes per game per cluster
minutes_per_game_average = np.average(minutes_per_game[indices])
minutes_per_game_averages[i] = minutes_per_game_average
players_per_cluster[i] = len(indices)
# get players in each cluster
player_names.append(player_list[indices].tolist())
for i in range(6):
df = pd.DataFrame(player_names[i], columns = ['Cluster ' + str(i)])
df.to_csv('data/clustering_historic_players/' + classifier + 'historic_players_cluster_' + str(i) + '.csv', index=False)
table_data=[['Cluster 0', 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5'], players_per_cluster.astype(int).tolist()]
self.table_creator(classifier + ' Historic Players Per Cluster', table_data)
all_star_averages = np.round(career_all_stars_averages, 3).tolist()
all_star_averages.insert(0, 'ALL STARS')
all_nba_averages = np.round(career_all_nba_averages, 3).tolist()
all_nba_averages.insert(0, 'ALL NBAs')
gs_averages = np.round(gs_percents_averages, 3).tolist()
gs_averages.insert(0, 'GS Percent')
min_averages = np.round(minutes_per_game_averages, 3).tolist()
min_averages.insert(0, 'Min/Game')
table_data=[['Averages', 'Cluster 0', 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5'], all_star_averages, all_nba_averages, gs_averages, min_averages]
self.table_creator(classifier + ' Averages Per Cluster', table_data)
return (career_labels_kmeans, career_labels_kmedoids)
"""
The below clusters current nba second year players into six groups with kmeans and kmedoids
Additionally, the below displays each cluster in excel spreadsheets and displays pypot tables.
One table describes the number of members per cluster.
"""
def current_players_projections(self, career_labels_kmeans, career_labels_kmedoids, second_yrs_1990_2010_season, second_yrs_current_season, second_yrs_current_season_players_list):
for classifier in ['KMeans', 'KMedoids']:
# averages of second year performance per cluster
#
if classifier == 'KMeans':
print('KMEANS CLUSTERING Current Players')
career_labels = career_labels_kmeans
else:
print('KMEDOIDS CLUSTERING Current Players')
career_labels = career_labels_kmedoids
second_year_cluster_averages = np.zeros((len(set(career_labels)), len(second_yrs_1990_2010_season[0])))
for i in range(len(set(career_labels))):
indices = np.where(career_labels == i)[0]
cluster_average = np.average(second_yrs_1990_2010_season[indices], axis=0)
second_year_cluster_averages[i] = cluster_average
# assign projected cluster label to current second year players
# using euclidean distance measurement to average second year performances of 1990-2010 clusters
distances = np.array(pairwise_distances(second_yrs_current_season, second_year_cluster_averages, metric='euclidean'))
second_year_current_labels = np.argmin(distances, axis=1)
players_per_cluster = np.zeros(len(set(second_year_current_labels)))
player_names = []
for i in range(len(set(second_year_current_labels))):
indices = np.where(second_year_current_labels == i)[0]
players_per_cluster[i] = len(indices)
player_names.append(second_yrs_current_season_players_list[indices])
table_data=[['Cluster 0', 'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5'], players_per_cluster.astype(int).tolist()]
self.table_creator(classifier + ' CURRENT PLAYERS PER CLUSTER', table_data)
for i in range(6):
df = pd.DataFrame(player_names[i], columns = ['Cluster ' + str(i)])
df.to_csv('data/clustering_second_year_players/' + classifier + 'current_players_cluster_' + str(i) + '.csv', index=False)
if __name__ == "__main__":
Main()