Skip to main content

Module path

Data.generate_features

generate_features_teamwins()

Reads raw game data from the games table, computes rolling averages and derived features for each team, and writes the result to the team_game_stats table. This table is consumed directly by the ML training step.

Signature

def generate_features_teamwins(rolling_window: int = 5):

Parameters

rolling_window
integer
default:"5"
Number of prior games used to compute each rolling average. A min_periods=1 setting means rows are included even when fewer than rolling_window games exist for a team.
Returns: None Side effects:
  • Reads all rows from the games table in Data/nba_stats.db.
  • Drops and recreates the team_game_stats table (via if_exists='replace').
  • Prints the loaded row count and the generated row count to stdout.
Run fetch_games_teamwins() before this function so the games table is populated.

Usage example

from Data.generate_features import generate_features_teamwins

# Use the default 5-game rolling window
generate_features_teamwins()

# Use a 10-game rolling window
generate_features_teamwins(rolling_window=10)

team_game_stats output columns

The function creates or replaces the team_game_stats table with the following columns:
ColumnTypeDescription
game_idTEXTNBA game identifier. Primary key.
teamTEXTTeam abbreviation.
opponentTEXTOpponent team abbreviation.
points_diffREALteam_points_roll - opponent_points_roll. Positive values favour the team.
elo_diffREALElo rating difference. Currently always 0 (reserved for future use).
homeINTEGER1 if the team played at home, 0 if away.
winINTEGER1 if the team won, 0 if the team lost.
team_points_rollREALRolling average of team_points over the last N games for this team.
opponent_points_rollREALRolling average of opponent_points over the last N games for this team.
team_reb_rollREALRolling average of team_reb over the last N games for this team.
opponent_reb_rollREALRolling average of opponent_reb over the last N games for this team.
team_ast_rollREALRolling average of team_ast over the last N games for this team.
opponent_ast_rollREALRolling average of opponent_ast over the last N games for this team.
Rolling averages are computed per team using groupby('team') before being joined back to the full DataFrame. Game ordering follows the natural order of rows in the games table.

Build docs developers (and LLMs) love