Analyzing NBA Stats
When to use this skill
- •User asks for "player props" or "recent form" for NBA players.
- •User wants to analyze "box scores" or "advanced metrics" (PER, TS%, Usage).
- •Integrating new NBA data sources into the
modelcomponent.
Workflow
- • Source Selection: Decide between
nba_api(Python wrapper for stats.nba.com) or external scraping if needing prop betting odds. - • Normalization: Map Player Names to IDs consistently. Handle "J. Brown" vs "Jaylen Brown".
- • Data Frame Creation: Always load data into a Pandas DataFrame for analysis.
- • Feature Engineering: Calculate rolling averages (last 5 games, last 10 games) as these are crucial for betting models.
Instructions
1. Using nba_api (Python)
The model directory is likely where this code lives.
python
from nba_api.stats.endpoints import playergamelog
import pandas as pd
def get_player_last_n_games(player_id, n=5):
# Season '2023-24' needs to be dynamic
log = playergamelog.PlayerGameLog(player_id=player_id, season='2024-25')
df = log.get_data_frames()[0]
return df.head(n)
2. Key Metrics for Betting
Focus on these stats for prop bets:
- •PTS, REB, AST: Standard props.
- •Minutes Played: High correlation with output.
- •USG% (Usage Rate): Good for predicting high-scoring games.
3. Rate Limiting
stats.nba.com is strict.
- •Add user-agent headers if making raw HTTP requests.
- •Implement delays between requests in loops.