import numpy as np, sys, scipy.stats, pandas as pd, requests, json, matplotlib import matplotlib.pyplot as plt import pylab as pl %matplotlib inline pd.options.display.mpl_style = 'default' df = None players = [] player_stats = {'name':None,'avg_dribbles':None,'avg_touch_time':None,'avg_shot_distance':None,'avg_defender_distance':None, 'shooting_%':None} def find_stats(name,player_id,dateFrom,dateTo): #NBA Stats API using selected player ID url = 'http://stats.nba.com/stats/playerdashptshotlog?'+ \ 'DateFrom='+dateFrom+'&DateTo='+dateTo+'&GameSegment=&LastNGames=0&LeagueID=00&' + \ 'Location=&Month=0&OpponentTeamID=0&Outcome=&Period=0&' + \ 'PlayerID='+player_id+'&Season=2014-15&SeasonSegment=&' + \ 'SeasonType=Regular+Season&TeamID=0&VsConference=&VsDivision=' #Create Dict based on JSON response response = requests.get(url) shots = response.json()['resultSets'][0]['rowSet'] data = json.loads(response.text) #Create df from data and find averages headers = data['resultSets'][0]['headers'] shot_data = data['resultSets'][0]['rowSet'] df = pd.DataFrame(shot_data,columns=headers) avg_def = df['CLOSE_DEF_DIST'].mean(axis=1) avg_dribbles = df['DRIBBLES'].mean(axis=1) avg_shot_distance = df['SHOT_DIST'].mean(axis=1) avg_touch_time = df['TOUCH_TIME'].mean(axis=1) #add Averages to dictionary then to list player_stats['name'] = name player_stats['avg_defender_distance']=avg_def player_stats['avg_shot_distance'] = avg_shot_distance player_stats['avg_touch_time'] = avg_touch_time player_stats['avg_dribbles'] = avg_dribbles player_stats['shooting_%'] = df['FGM'].mean(axis=1) players.append(player_stats.copy()) def find_stats2(name,player_id,dateFrom,dateTo): #NBA Stats API using selected player ID url = 'http://stats.nba.com/stats/playerdashptshotlog?'+ \ 'DateFrom='+dateFrom+'&DateTo='+dateTo+'&GameSegment=&LastNGames=0&LeagueID=00&' + \ 'Location=&Month=0&OpponentTeamID=0&Outcome=&Period=0&' + \ 'PlayerID='+player_id+'&Season=2014-15&SeasonSegment=&' + \ 'SeasonType=Regular+Season&TeamID=0&VsConference=&VsDivision=' #Create Dict based on JSON response response = requests.get(url) shots = response.json()['resultSets'][0]['rowSet'] data = json.loads(response.text) #Create df from data and find averages headers = data['resultSets'][0]['headers'] shot_data = data['resultSets'][0]['rowSet'] df = pd.DataFrame(shot_data,columns=headers) return df cols = ['name','avg_defender_distance','avg_dribbles','avg_shot_distance','avg_touch_time', 'shooting_%'] #name the columns that I will put data into dateFrom='8/1/14' #some arbitrary before season date- dec22 dateTo='12/22/14' find_stats('andrew wiggins','203952', dateFrom, dateTo) df = pd.DataFrame(players,columns = cols) dateFrom='12/22/14' #dec 22- through some arbitrary future date dateTo='12/1/15' find_stats('andrew wiggins','203952', dateFrom, dateTo) df = pd.DataFrame(players,columns = cols) df.head() df = None #lets create a new place for the data dateFrom='8/1/14' dateTo='12/22/14' df = find_stats2('andrew wiggins','203952', dateFrom, dateTo); df.head() bins = [0,3,6,9,12,15,18,21,24,27] binlabels = ['0','1-3','4-6','7-9','10-12','13-15','16-18','19-21','22-24','25-27'] fig = matplotlib.pyplot.gcf() fig.set_size_inches(14,6) plt.subplot(1,2,1) n, bins, patches = plt.hist(df['SHOT_DIST'], bins=bins) plt.xticks(bins+1.5,binlabels) plt.xlabel('Shot Distance (ft)') plt.title('Before Dec 22nd') plt.ylabel('Bin Count'); dateFrom='12/22/14' dateTo='12/1/15' df2 = find_stats2('andrew wiggins','203952', dateFrom, dateTo); plt.subplot(1,2,2) n, bins, patches = plt.hist(df2['SHOT_DIST'], bins) plt.xticks(bins+1.5,binlabels) plt.title('After Dec 22nd') plt.xlabel('Shot Distance (ft)'); fig = matplotlib.pyplot.gcf() fig.set_size_inches(14,6) made = df['SHOT_RESULT'] == 'made' missed = df['SHOT_RESULT'] == 'missed' plt.subplot(1,2,1) n, bins, patches = plt.hist([df[made]['SHOT_DIST'],df[missed]['SHOT_DIST']], bins=bins, label=['shot made',' shot missed']) plt.title('Before Dec 22nd') plt.legend() plt.xticks(bins+1.5,binlabels) plt.xlabel('Shot Distance (ft)') plt.ylabel('Bin Count'); made = df2['SHOT_RESULT'] == 'made' missed = df2['SHOT_RESULT'] == 'missed' plt.subplot(1,2,2) plt.title('After Dec 22nd') n, bins, patches = plt.hist([df2[made]['SHOT_DIST'],df2[missed]['SHOT_DIST']], bins, label=['shot made',' shot missed']) plt.xticks(bins+1.5,binlabels) plt.legend() plt.xlabel('Shot Distance (ft)'); fig = matplotlib.pyplot.gcf() fig.set_size_inches(16,6) plt.subplot(1,2,1) plt.hist2d(df['SHOT_DIST'], df['DRIBBLES']) plt.colorbar() plt.title('Before Dec 22nd') plt.xlabel('Shot Distance (ft)') plt.ylabel('# Dribbles'); plt.subplot(1,2,2) plt.hist2d(df2['SHOT_DIST'], df2['DRIBBLES']) plt.colorbar() plt.title('After Dec 22nd') plt.xlabel('Shot Distance (ft)'); fig = matplotlib.pyplot.gcf() fig.set_size_inches(16,6) plt.subplot(1,2,1) plt.hist2d(df['SHOT_DIST'], df['CLOSE_DEF_DIST']) plt.colorbar() plt.title('Before Dec 22nd') plt.xlabel('Shot Distance (ft)') plt.ylabel('Closest Defender (ft)'); plt.subplot(1,2,2) plt.hist2d(df2['SHOT_DIST'], df2['CLOSE_DEF_DIST']) plt.title('After Dec 22nd') plt.colorbar() plt.xlabel('Shot Distance (ft)');