Plots the NBA shot_charts and shot distribution for players, teams and the whole league.

class Config[source]

Config()

config = Config()

class URLs[source]

URLs()

Global constants for dataset and model URLs.

Downloading

download_url[source]

download_url(url, dest, overwrite=False, pbar=None, show_progress=True, chunk_size=1048576, timeout=4, retries=5)

Download url to dest unless it exists and not overwrite

download_data[source]

download_data(url, fname=None, c_key='archive', force_download=False)

Download url to fname.

Extract

file_extract[source]

file_extract(fname, dest='.')

Extract fname to dest using tarfile or `zipfile

file_extract is used by default in untar_data to decompress the downloaded file.

URLs.path(URLs.SHOTS_2020)
PosixPath('/Users/ccalderon/.shot_chart/archive/shots-2020.tgz')

untar_data[source]

untar_data(url, fname=None, dest=None, c_key='data', force_download=False, extract_func='file_extract')

Download url to fname if dest doesn't exist, and un-tgz to folder dest.

make_df[source]

make_df(path)

Creates a pandas dataframe from path

shots_2020 = make_df(untar_data(URLs.SHOTS_2020))

Using the delegate function from https://www.fast.ai/2019/08/06/delegation/

delegates[source]

delegates(to=None, keep=False)

Decorator: replace **kwargs in signature with params from to

Creating a class - Shots

class Shots[source]

Shots(dataframe)

Plots shot chart and most/least effective shots using plot_shots and plot_effective

mycmap = plt.cm.Reds
mycmap._init()
mycmap._lut[:,-1] = np.linspace(0, 0.8, 259)
shots = Shots(shots_2020)
shots.players
0                    Anthony Davis
1                 Patrick Beverley
2                     LeBron James
3                    Nicolas Batum
4         Kentavious Caldwell-Pope
                    ...           
141620              James Nunnally
142633              Dewayne Dedmon
142841                  Donta Hall
143243               Robert Franks
146382            Anthony Tolliver
Name: shots_by, Length: 525, dtype: object
viridis = plt.cm.get_cmap('viridis', 12)
print('viridis.colors', viridis.colors)
viridis.colors [[0.267004 0.004874 0.329415 1.      ]
 [0.283072 0.130895 0.449241 1.      ]
 [0.262138 0.242286 0.520837 1.      ]
 [0.220057 0.343307 0.549413 1.      ]
 [0.177423 0.437527 0.557565 1.      ]
 [0.143343 0.522773 0.556295 1.      ]
 [0.119512 0.607464 0.540218 1.      ]
 [0.166383 0.690856 0.496502 1.      ]
 [0.319809 0.770914 0.411152 1.      ]
 [0.525776 0.833491 0.288127 1.      ]
 [0.762373 0.876424 0.137064 1.      ]
 [0.993248 0.906157 0.143936 1.      ]]
shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_shots(date_range=((2021,4,1), (2021,4,18)))
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_shots(date_range=((2021,4,17), (2021,4,18)))
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.list_game_ids(2021, 4, 17)
game_id winner loser
144708 202104170WAS Washington Detroit
144885 202104170PHO San Antonio Phoenix
145064 202104170MIL Memphis Milwaukee
145248 202104170BOS Boston Golden State
145421 202104170LAL LA Lakers Utah
145613 202104170CHI Chicago Cleveland
shots.plot_shots(date_range="202104170WAS")
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_effective(most_or_least="most", metric="efg")
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_effective(most_or_least="most", metric="efg", exclude=['0ft','1ft','2ft','23ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_effective(most_or_least="most", min_shots="auto",exclude=['1ft','2ft','23ft','24ft','25ft','26ft','27ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
shots.plot_effective(most_or_least="least", min_shots='auto',exclude=['24ft','23ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")

List teams

list_teams[source]

list_teams(dataframe)

Lists all the teams in dataframe

print(list_teams(shots_2020))
0          LA Lakers
1        LA Clippers
173         Brooklyn
174     Golden State
362          Chicago
363       Washington
533           Denver
534       Sacramento
708      New Orleans
709          Phoenix
869          Toronto
870     Philadelphia
1043        Oklahoma
1045         Orlando
1226       Milwaukee
1229           Miami
1397            Utah
1569        Portland
1750         Houston
1921         Memphis
2119         Atlanta
2120         Detroit
2652          Dallas
2817       Minnesota
2998       Cleveland
3161     San Antonio
3707       Charlotte
4060         Indiana
5863        New York
5865          Boston
Name: team, dtype: object

List unique game ids

list_game_ids[source]

list_game_ids(dataframe, year, month, day)

Lists unique game ids in dataframe for a given date

list_game_ids(shots_2020,2021,4,17)
game_id winner loser
144708 202104170WAS Washington Detroit
144885 202104170PHO San Antonio Phoenix
145064 202104170MIL Memphis Milwaukee
145248 202104170BOS Boston Golden State
145421 202104170LAL LA Lakers Utah
145613 202104170CHI Chicago Cleveland

Inheriting from Shots to create TeamShots

class TeamShots[source]

TeamShots(dataframe, team) :: Shots

Team shots

team_shots = TeamShots(shots_2020,"Portland")
team_shots.team
'Portland'
team_shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots.plot_effective()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots.dataframe.loc[(team_shots.dataframe['distance']=='24ft') & (team_shots.dataframe['attempt']=='2-pointer')]
Unnamed: 0 game_id year month day winner loser x y play time_remaining quarter shots_by outcome attempt distance team winner_score loser_score
team_shots.plot_effective(most_or_least="least")
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots = TeamShots(shots_2020,"Houston")
team_shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots.plot_shots(date_range=((2021,1,3), (2021,1,8)))
<ipython-input-17-a092ce7bf15d>:118: FutureWarning: Value based partial slicing on non-monotonic DatetimeIndexes with non-existing keys is deprecated and will raise a KeyError in a future Version.
  shots_df = copy_df.loc[str(str(date_range[0][0])+"-"+str(date_range[0][1])+"-"+str(date_range[0][2])):str(str(date_range[1][0])+"-"+str(date_range[1][1])+"-"+str(date_range[1][2]))]
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots.plot_effective("least")
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
team_shots = TeamShots(shots_2020,"LA Lakers")
team_shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")

List players who took shots for a team

list_team_players[source]

list_team_players(dataframe, team)

Lists the players in dataframe who took shots for a team

list_team_players(shots_2020,'LA Lakers')
shots_by count
12 LeBron James 755
6 Dennis Schröder 651
11 Kyle Kuzma 611
15 Montrezl Harrell 526
17 Talen Horton-Tucker 393
9 Kentavious Caldwell-Pope 389
3 Anthony Davis 383
14 Markieff Morris 310
0 Alex Caruso 225
18 Wesley Matthews 207
13 Marc Gasol 180
1 Alfonzo McKinnie 72
2 Andre Drummond 55
4 Ben McLemore 44
7 Devontae Cacok 26
16 Quinn Cook 26
5 Damian Jones 17
10 Kostas Antetokounmpo 8
8 Jared Dudley 7

Inheriting from Shots to create PlayerShots

class PlayerShots[source]

PlayerShots(dataframe, player) :: Shots

Player shots

player_shots = PlayerShots(shots_2020,"Kevin Durant")
player_shots.fg_pct
0.54
player_shots.efg_pct
0.61
player_shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots = PlayerShots(shots_2020,"Russell Westbrook")
player_shots.plot_shots(distance_limit=(16,26),attempt="2-pointer")
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots.plot_effective(metric="efg", exclude=['0ft','22ft',"1ft"])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots.plot_effective(metric="efg", min_shots="auto",exclude=['2ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots = PlayerShots(shots_2020,"James Harden")
player_shots.plot_effective(metric="efg", exclude=['16ft',"0ft","11ft",'1ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots.plot_effective(min_shots="auto",exclude=['1ft','28ft','2ft','3ft'])
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots = PlayerShots(shots_2020, 'Derrick Rose')
player_shots.list_game_ids(2021,4,16)
game_id winner loser
147044 202104160DAL New York Dallas
player_shots.plot_shots()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots.plot_effective()
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")
player_shots.plot_shots(date_range='202104160DAL')
<ipython-input-17-a092ce7bf15d>:42: MatplotlibDeprecationWarning: Directly reading images from URLs is deprecated since 3.4 and will no longer be supported two minor releases later. Please open the URL for reading and pass the result to Pillow, e.g. with ``PIL.Image.open(urllib.request.urlopen(url))``.
  img = plt.imread("http://d2p3bygnnzw9w3.cloudfront.net/req/1/images/bbr/nbahalfcourt.png")