# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:nba-win-prob]
#     language: python
#     name: conda-env-nba-win-prob-py
# ---

# +
# %matplotlib notebook
import datetime
import pandas as pd
from IPython.core.display import display, HTML
import matplotlib.pyplot as plt
import numpy as np

display(HTML("<style>.container { width:100% !important; }</style>"))
pd.set_option('display.max_columns', 50)
# -

# # Load datasets

start_year = 1984
end_year = 2018

df_season_summaries = pd.read_csv("../Data/nba_season_summaries_{}_{}.csv".format(start_year, end_year), index_col=[0, 1])
# For MultiIndex slicing support
df_season_summaries.sort_index(inplace=True) 
df_boxscores = pd.read_csv("../Data/nba_boxscores_{}_{}.csv".format(start_year, end_year), index_col=0, parse_dates=[1], infer_datetime_format=True)

df_season_summaries.head()

df_boxscores.head()

# # Functions for querying/masking

# +
boxscore_season_range_mask = lambda df, start_year, end_year:  (df["season"] >= start_year) & (df["season"] <= end_year)
boxscore_date_range_mask = lambda df, start_date, end_date: (df["date"] >= start_date) & (df["date"] <= end_date)
boxscore_team_mask = lambda df, team_initials: (df["team1"] == team_initials) | (df["team2"] == team_initials)
boxscore_regular_season_mask = lambda df: pd.isnull(df["playoff"])

summary_season_range = lambda df, start_year, end_year: df.loc[start_year:end_year]
summary_season_query = lambda df, years, teams, col_names: df.loc[(years, teams), col_names]
summary_season_remove_league_average = lambda: df
# -

# # Cleaning data for plotting

# Only use completed seasons
df_season_summaries = df_season_summaries.loc[1984:2017]
X = df_season_summaries.columns.values.tolist()
remove_cols = ["Rk", "W", "L", "PW", "PL", "MOV", "SOS", "Arena", "Attend.", "Attend./G"]
X = [x for x in X if x not in remove_cols]
y = "W"
df_season_summaries = df_season_summaries.dropna(subset=X)

# # Visualization: Regular season wins vs. season metrics

# Pearson r correlations of wins vs. various season metrics
correlations = [(x, df_season_summaries[[y, x]].corr().loc[x][y]) for x in X]
plt.figure(1, figsize=(16,16), dpi=85)
plt.suptitle("Wins vs season metrics (regular seasons 1984-2017)", fontsize=20)
sqrt_X = np.sqrt(len(X))
rows = int(sqrt_X)
cols = int(len(X) / int(sqrt_X)) + 1
shared_y = plt.subplot(111)
shared_y.set_ylabel("W")
for indx, (x, pearsonR) in enumerate(sorted(correlations, key=lambda t: t[1], reverse=True)):
    ax = plt.subplot(rows, cols, indx+1, sharey=shared_y) 
    ax.set_xlabel(x)
    ax.text(0.025, 0.9, "r={}".format(round(pearsonR, 4)), transform=ax.transAxes)  
    ax.scatter(df_season_summaries[x], df_season_summaries[y], c='b', alpha=0.05)

StarcoderdataJupyterScriptsDedupFiltered