Spaces:
Build error
Build error
File size: 3,901 Bytes
1e61263 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
"""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"
# initialize empty players
players = [""]
# initialize empty seasons
seasons = [1977, 2021]
# load data
nba_dataset = load_dataset("andrewkroening/538-NBA-Historical-Raptor")
# initialize a dataframe from the nba dataset
nba_df = pd.DataFrame(nba_dataset["train"])
# make a player_df with seasons and every player for that season
player_df = nba_df[["season", "player_name"]].copy()
def get_players(this_season, df=player_df):
"""Get the players for a given season"""
# get the players for the 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"""
# load data
nba_data_fore = load_dataset("andrewkroening/538-NBA-Historical-Raptor")
# initialize a dataframe from the nba dataset
df = pd.DataFrame(nba_data_fore["train"])
# truncate to the player selected
dataset = df[df["player_name"] == this_player]
player_data = dataset[["season", "war_total"]].copy()
# player_perform = player_df.copy()
# make a list of the seasons the player played in
player_seasons = player_data["season"].unique().tolist()
# make two dfs, one for actual performance and one for the model
# player_perform = player_perform[player_perform["season"] <= year + 5]
player_data = player_data[player_data["season"] <= this_year]
# convert the season column to a datetime object
player_data["season"] = pd.to_datetime(player_data["season"], format="%Y")
# set the df for prophet
player_data.columns = ["ds", "y"]
player_data = player_data.sort_values("ds")
# build the prophet model
m = Prophet(seasonality_mode="multiplicative").fit(player_data)
future = m.make_future_dataframe(periods=5, freq="Y")
forecast = m.predict(future)
# plot the forecast
fig1 = m.plot(forecast, xlabel="Year", ylabel="Wins Above Replacement")
# plot the actual performance
# fig2 = player_perform.plot(
# x="season", y="war_total", title="Actual Performance", template="plotly_white")
# return the figure
return fig1, gr.Dropdown.update(choices=player_seasons), gr.update(visible=True)
with gr.Blocks() as demo:
gr.Markdown(
"""
### This is a slightly comical 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()
|