|
"""A simple gradio app to predict NBA player performance this season""" |
|
|
|
import gradio as gr |
|
import pandas as pd |
|
from prophet import Prophet |
|
from datasets import load_dataset |
|
|
|
pd.options.plotting.backend = "plotly" |
|
|
|
|
|
|
|
players = [""] |
|
|
|
|
|
seasons = [1977, 2021] |
|
|
|
|
|
nba_dataset = load_dataset("andrewkroening/538-NBA-Historical-Raptor") |
|
|
|
|
|
nba_df = pd.DataFrame(nba_dataset["train"]) |
|
|
|
|
|
player_df = nba_df[["season", "player_name"]].copy() |
|
|
|
|
|
def get_players(this_season, df=player_df): |
|
"""Get the players for a given season""" |
|
|
|
season_players = df[df["season"] == this_season]["player_name"].unique().tolist() |
|
|
|
return gr.Dropdown.update(choices=season_players), gr.update(visible=False) |
|
|
|
|
|
def get_forecast(this_year, this_player): |
|
"""Get the forecast for a given player and year and the performance for entire career""" |
|
|
|
nba_data_fore = load_dataset("andrewkroening/538-NBA-Historical-Raptor") |
|
|
|
|
|
df = pd.DataFrame(nba_data_fore["train"]) |
|
|
|
|
|
dataset = df[df["player_name"] == this_player] |
|
player_data = dataset[["season", "war_total"]].copy() |
|
|
|
|
|
|
|
player_seasons = player_data["season"].unique().tolist() |
|
|
|
|
|
|
|
player_data = player_data[player_data["season"] <= this_year] |
|
|
|
|
|
player_data["season"] = pd.to_datetime(player_data["season"], format="%Y") |
|
|
|
|
|
player_data.columns = ["ds", "y"] |
|
player_data = player_data.sort_values("ds") |
|
|
|
|
|
m = Prophet(seasonality_mode="multiplicative").fit(player_data) |
|
future = m.make_future_dataframe(periods=5, freq="Y") |
|
forecast = m.predict(future) |
|
|
|
|
|
fig1 = m.plot(forecast, xlabel="Year", ylabel="Wins Above Replacement") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fig1, gr.Dropdown.update(choices=player_seasons), gr.update(visible=True) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
gr.Markdown( |
|
""" |
|
### 0xpako's NBA Player Performance Predictor. |
|
|
|
***It is designed to show a projection for performance (Wins Above Replacement) and compare it to the actual performance over a career.*** |
|
|
|
***If the projection hangs, it is because the model is taking a long time to run. Refresh the page and give it another shot... get it?*** |
|
""" |
|
) |
|
with gr.Row(): |
|
year = gr.Slider(1977, 2021, label="Season", interactive=True, step=1) |
|
player = gr.Dropdown(players, label="Player", interactive=True) |
|
|
|
with gr.Column(visible=False) as output_col: |
|
gr.Markdown( |
|
"**Below is the player forecast for the selected season plus 5 years. Next to the graph is a dropdown you can use to change the season and update the chart and see how a player's projection has changed over time.**" |
|
) |
|
with gr.Row(): |
|
plt = gr.Plot() |
|
season = gr.Dropdown(seasons, label="Season", interactive=True, step=1) |
|
|
|
year.change(get_players, inputs=year, outputs=[player, output_col]) |
|
player.change( |
|
get_forecast, inputs=[year, player], outputs=[plt, season, output_col] |
|
) |
|
season.change( |
|
get_forecast, inputs=[season, player], outputs=[plt, season, output_col] |
|
) |
|
|
|
demo.launch() |
|
|