Prerequisites
- Python 3.8 or later
pippackage manager
Installation
Install the required dependencies:The model must be trained before you can make predictions. Complete all steps below in order — skipping
train_model_teamwins() will cause predict_game_teamwins() to fail with a missing model file error.Run the full pipeline
Fetch NBA game data
Call The season format is
fetch_games_teamwins() with a season string to pull team game logs from the NBA Stats API and store them in a local SQLite database (Data/nba_stats.db)."YYYY-YY" (e.g. "2024-25", "2025-26"). Data is written to the games table and existing rows are replaced on conflict.Generate features
Call The default rolling window is 5 games. Pass a different integer to
generate_features_teamwins() to compute 5-game rolling averages for points, rebounds, and assists for each team. The results are written to the team_game_stats table.rolling_window to change it:Train the model
Call The function prints test accuracy on a held-out 30% split before saving:The exact value depends on the season data used for training.
train_model_teamwins() to fit a 200-estimator Random Forest classifier on the team_game_stats table. The trained model is saved to models/nba_model.pkl.Make a prediction
Call
predict_game_teamwins() with the rolling-average stats for both teams to get a win probability between 0.0 and 1.0.| Parameter | Type | Description |
|---|---|---|
points_diff | float | Team’s rolling points average minus the opponent’s rolling points average |
team_reb_roll | float | Team’s rolling rebound average |
opponent_reb_roll | float | Opponent’s rolling rebound average |
team_ast_roll | float | Team’s rolling assist average |
opponent_ast_roll | float | Opponent’s rolling assist average |
home | int | 1 if the team plays at home, 0 if away |
Complete example
The following is the full end-to-end script frommain.py:
Expected output
"Good Bet", "Slight Edge", or "Avoid".