Spaces:
Sleeping
Sleeping
Create batter_arsenal_model.py
Browse files
models/batter_arsenal_model.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from models.batter_zone_model import normalize_pitch_family
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _empty_batter_arsenal_row(player_name: str) -> dict[str, Any]:
|
| 11 |
+
out: dict[str, Any] = {
|
| 12 |
+
"player_name": player_name,
|
| 13 |
+
"arsenal_sample_size": 0,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
for family in ["fastball", "breaking", "offspeed"]:
|
| 17 |
+
out[f"hr_prob_{family}"] = None
|
| 18 |
+
out[f"hit_prob_{family}"] = None
|
| 19 |
+
out[f"tb2p_prob_{family}"] = None
|
| 20 |
+
out[f"whiff_prob_{family}"] = None
|
| 21 |
+
out[f"damage_prob_{family}"] = None
|
| 22 |
+
out[f"sample_size_{family}"] = 0
|
| 23 |
+
|
| 24 |
+
return out
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def build_batter_arsenal_feature_row(
|
| 28 |
+
statcast_df: pd.DataFrame,
|
| 29 |
+
player_name: str,
|
| 30 |
+
) -> dict[str, Any]:
|
| 31 |
+
if statcast_df is None or statcast_df.empty:
|
| 32 |
+
return _empty_batter_arsenal_row(player_name)
|
| 33 |
+
|
| 34 |
+
if "player_name" not in statcast_df.columns:
|
| 35 |
+
return _empty_batter_arsenal_row(player_name)
|
| 36 |
+
|
| 37 |
+
df = statcast_df[statcast_df["player_name"].astype(str) == str(player_name)].copy()
|
| 38 |
+
if df.empty:
|
| 39 |
+
return _empty_batter_arsenal_row(player_name)
|
| 40 |
+
|
| 41 |
+
if "pitch_name" in df.columns:
|
| 42 |
+
pitch_name_series = df["pitch_name"]
|
| 43 |
+
elif "pitch_type" in df.columns:
|
| 44 |
+
pitch_name_series = df["pitch_type"]
|
| 45 |
+
else:
|
| 46 |
+
pitch_name_series = pd.Series(["unknown"] * len(df), index=df.index)
|
| 47 |
+
|
| 48 |
+
df["pitch_family"] = pitch_name_series.apply(normalize_pitch_family)
|
| 49 |
+
|
| 50 |
+
description = df.get("description", pd.Series("", index=df.index)).astype(str).str.lower()
|
| 51 |
+
events = df.get("events", pd.Series("", index=df.index)).astype(str).str.lower()
|
| 52 |
+
launch_speed = pd.to_numeric(df.get("launch_speed"), errors="coerce")
|
| 53 |
+
|
| 54 |
+
df["hit_flag"] = events.isin(["single", "double", "triple", "home_run"]).astype(int)
|
| 55 |
+
df["hr_flag"] = events.eq("home_run").astype(int)
|
| 56 |
+
df["tb2p_flag"] = events.isin(["double", "triple", "home_run"]).astype(int)
|
| 57 |
+
df["whiff_flag"] = description.isin(["swinging_strike", "swinging_strike_blocked"]).astype(int)
|
| 58 |
+
df["damage_flag"] = ((launch_speed >= 95).fillna(False) | df["hr_flag"].eq(1)).astype(int)
|
| 59 |
+
|
| 60 |
+
out = _empty_batter_arsenal_row(player_name)
|
| 61 |
+
out["arsenal_sample_size"] = int(len(df))
|
| 62 |
+
|
| 63 |
+
for family in ["fastball", "breaking", "offspeed"]:
|
| 64 |
+
family_df = df[df["pitch_family"] == family].copy()
|
| 65 |
+
if family_df.empty:
|
| 66 |
+
continue
|
| 67 |
+
|
| 68 |
+
out[f"hr_prob_{family}"] = float(family_df["hr_flag"].mean())
|
| 69 |
+
out[f"hit_prob_{family}"] = float(family_df["hit_flag"].mean())
|
| 70 |
+
out[f"tb2p_prob_{family}"] = float(family_df["tb2p_flag"].mean())
|
| 71 |
+
out[f"whiff_prob_{family}"] = float(family_df["whiff_flag"].mean())
|
| 72 |
+
out[f"damage_prob_{family}"] = float(family_df["damage_flag"].mean())
|
| 73 |
+
out[f"sample_size_{family}"] = int(len(family_df))
|
| 74 |
+
|
| 75 |
+
return out
|