Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 360 additions & 0 deletions scripts/players/shots.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e24e324a",
"metadata": {},
"source": [
"# Data processing - shots"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "23dc2285",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:39.762810Z",
"iopub.status.busy": "2026-05-03T01:58:39.762641Z",
"iopub.status.idle": "2026-05-03T01:58:40.666805Z",
"shell.execute_reply": "2026-05-03T01:58:40.666511Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"DATA_PATH = \"../../data/\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "400eaa7f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:40.668530Z",
"iopub.status.busy": "2026-05-03T01:58:40.668382Z",
"iopub.status.idle": "2026-05-03T01:58:40.670319Z",
"shell.execute_reply": "2026-05-03T01:58:40.670107Z"
}
},
"outputs": [],
"source": [
"def get_season(date):\n",
" year = date.year\n",
" return year if date.month >= 9 else year - 1"
]
},
{
"cell_type": "markdown",
"id": "a6aa3f0b",
"metadata": {},
"source": [
"## Load shot detail data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a00d3dce",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:40.671515Z",
"iopub.status.busy": "2026-05-03T01:58:40.671421Z",
"iopub.status.idle": "2026-05-03T01:58:40.675033Z",
"shell.execute_reply": "2026-05-03T01:58:40.674764Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"58"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import glob\n",
"\n",
"shot_files = sorted(glob.glob(DATA_PATH + \"/nba_play_by_play_shot_data/shotdetail*.csv\"))\n",
"\n",
"len(shot_files)"
]
},
{
"cell_type": "markdown",
"id": "bcb172e9",
"metadata": {},
"source": [
"## Process data"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b04c8b23",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:40.676114Z",
"iopub.status.busy": "2026-05-03T01:58:40.676029Z",
"iopub.status.idle": "2026-05-03T01:58:52.607418Z",
"shell.execute_reply": "2026-05-03T01:58:52.607055Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>season</th>\n",
" <th>personId</th>\n",
" <th>shotValue</th>\n",
" <th>locX</th>\n",
" <th>locY</th>\n",
" <th>shotMade</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1996</td>\n",
" <td>120</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1996</td>\n",
" <td>120</td>\n",
" <td>2</td>\n",
" <td>153</td>\n",
" <td>71</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1996</td>\n",
" <td>363</td>\n",
" <td>2</td>\n",
" <td>-103</td>\n",
" <td>26</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1996</td>\n",
" <td>120</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1996</td>\n",
" <td>87</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" season personId shotValue locX locY shotMade\n",
"0 1996 120 2 0 0 1\n",
"1 1996 120 2 153 71 0\n",
"2 1996 363 2 -103 26 1\n",
"3 1996 120 2 0 0 0\n",
"4 1996 87 2 0 0 0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# GRID_TYPE,GAME_ID,GAME_EVENT_ID,PLAYER_ID,PLAYER_NAME,TEAM_ID,TEAM_NAME,PERIOD,MINUTES_REMAINING,SECONDS_REMAINING,\n",
"# EVENT_TYPE,ACTION_TYPE,SHOT_TYPE,SHOT_ZONE_BASIC,SHOT_ZONE_AREA,SHOT_ZONE_RANGE,SHOT_DISTANCE,LOC_X,LOC_Y,SHOT_ATTEMPTED_FLAG,SHOT_MADE_FLAG,GAME_DATE,HTM,VTM\n",
"\n",
"# hoop is at (LOC_X, LOC_Y) = (0, 0)\n",
"\n",
"cols_to_keep = [\n",
" \"GAME_DATE\", \"PLAYER_ID\",\n",
" \"SHOT_TYPE\", \"LOC_X\", \"LOC_Y\",\n",
" \"SHOT_ATTEMPTED_FLAG\", \"SHOT_MADE_FLAG\"\n",
"]\n",
"\n",
"frames = []\n",
"\n",
"for file_path in shot_files:\n",
" # only regular season data; \"_po_\" files are playoffs\n",
" if \"_po_\" in file_path.lower():\n",
" continue\n",
"\n",
" df = pd.read_csv(file_path, usecols=lambda c: c in cols_to_keep)\n",
"\n",
" # 1 = actual shot attempt\n",
" if \"SHOT_ATTEMPTED_FLAG\" in df.columns:\n",
" df = df[df[\"SHOT_ATTEMPTED_FLAG\"] == 1]\n",
"\n",
" df[\"GAME_DATE\"] = pd.to_datetime(df[\"GAME_DATE\"], format=\"%Y%m%d\", errors=\"coerce\")\n",
" df = df[df[\"GAME_DATE\"].notna()]\n",
" df[\"season\"] = df[\"GAME_DATE\"].apply(get_season)\n",
" df = df.drop(columns=[\"GAME_DATE\", \"SHOT_ATTEMPTED_FLAG\"])\n",
"\n",
" # cleanup coordinates\n",
" df[\"LOC_X\"] = pd.to_numeric(df[\"LOC_X\"], errors=\"coerce\")\n",
" df[\"LOC_Y\"] = pd.to_numeric(df[\"LOC_Y\"], errors=\"coerce\")\n",
" df = df.dropna(subset=[\"LOC_X\", \"LOC_Y\"])\n",
"\n",
" frames.append(df)\n",
"\n",
"shot_events = pd.concat(frames, ignore_index=True)\n",
"\n",
"shot_events = shot_events.rename(columns={\n",
" \"PLAYER_ID\": \"personId\",\n",
" \"SHOT_TYPE\": \"shotType\",\n",
" \"LOC_X\": \"locX\",\n",
" \"LOC_Y\": \"locY\",\n",
" \"SHOT_MADE_FLAG\": \"shotMade\",\n",
"})\n",
"\n",
"# SHOT_TYPE: 3PT Field Goal or 2PT Field Goal\n",
"shot_events[\"shotValue\"] = shot_events[\"shotType\"].str.extract(r\"(\\d)PT\")[0].astype(\"Int64\")\n",
"\n",
"shot_events = shot_events[[\n",
" \"season\", \"personId\", \"shotValue\", \"locX\", \"locY\", \"shotMade\"\n",
"]]\n",
"\n",
"shot_events.head()"
]
},
{
"cell_type": "markdown",
"id": "060371e7",
"metadata": {},
"source": [
"### Check range for locX, locY"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "796fb878",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:52.609116Z",
"iopub.status.busy": "2026-05-03T01:58:52.608994Z",
"iopub.status.idle": "2026-05-03T01:58:52.621124Z",
"shell.execute_reply": "2026-05-03T01:58:52.620863Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"locX: (np.int64(-250), np.int64(250))\n",
"locY: (np.int64(-52), np.int64(884))\n"
]
}
],
"source": [
"print(\"locX:\", (shot_events[\"locX\"].min(), shot_events[\"locX\"].max()))\n",
"print(\"locY:\", (shot_events[\"locY\"].min(), shot_events[\"locY\"].max()))"
]
},
{
"cell_type": "markdown",
"id": "0be67554",
"metadata": {},
"source": [
"## Save data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "66adbed6",
"metadata": {
"execution": {
"iopub.execute_input": "2026-05-03T01:58:52.622454Z",
"iopub.status.busy": "2026-05-03T01:58:52.622375Z",
"iopub.status.idle": "2026-05-03T01:58:57.338711Z",
"shell.execute_reply": "2026-05-03T01:58:57.338403Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/homebrew/Caskroom/miniconda/base/envs/.venv/lib/python3.11/site-packages/numpy/_core/fromnumeric.py:57: FutureWarning: 'DataFrame.swapaxes' is deprecated and will be removed in a future version. Please use 'DataFrame.transpose' instead.\n",
" return bound(*args, **kwds)\n"
]
}
],
"source": [
"import numpy as np\n",
"from pathlib import Path\n",
"\n",
"output_dir = Path(DATA_PATH) / \"processed\"\n",
"output_dir.mkdir(parents=True, exist_ok=True)\n",
"\n",
"parts = np.array_split(shot_events, 3)\n",
"\n",
"for i, part in enumerate(parts, 1):\n",
" part.to_csv(output_dir / f\"shot_events_part{i}.csv\", index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (other-env)",
"language": "python",
"name": "other-env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 3 additions & 0 deletions src/data/nba-court.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h2 class="year"></h2>
<h1 class="name"></h1>
<h2 class="year"></h2>
<p class="content"></p>
<svg id="player-chart" width="500" height="150" style="border: 1px solid #ccc"></svg>
<svg id="player-chart" width="400" height="376"></svg>
</div>
</div>
</section>
Expand Down
1 change: 1 addition & 0 deletions src/js/BubbleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export class BubbleMap {

this.statsItem = item;
this.statsUpdate(this.stats, this.seasonsLoader, this.metadataLoader, this.currentYear, item);
this.statsArea.style.background = bubble.style.background;

this.stats.classList.add("active");
});
Expand Down
Loading