Update stuff_model/feature_engineering.py
Browse files- stuff_model/feature_engineering.py +139 -132
stuff_model/feature_engineering.py
CHANGED
@@ -1,133 +1,140 @@
|
|
1 |
-
import polars as pl
|
2 |
-
import numpy as np
|
3 |
-
|
4 |
-
def feature_engineering(df: pl.DataFrame) -> pl.DataFrame:
|
5 |
-
# Extract the year from the game_date column
|
6 |
-
df = df.with_columns(
|
7 |
-
pl.col('game_date').str.slice(0, 4).alias('year')
|
8 |
-
)
|
9 |
-
|
10 |
-
df = df.with_columns([
|
11 |
-
|
12 |
-
(-(pl.col('vy0')**2 - (2 * pl.col('ay') * (pl.col('y0') - 17/12)))**0.5).alias('vy_f'),
|
13 |
-
])
|
14 |
-
|
15 |
-
df = df.with_columns([
|
16 |
-
((pl.col('vy_f') - pl.col('vy0')) / pl.col('ay')).alias('t'),
|
17 |
-
])
|
18 |
-
|
19 |
-
df = df.with_columns([
|
20 |
-
(pl.col('vz0') + (pl.col('az') * pl.col('t'))).alias('vz_f'),
|
21 |
-
(pl.col('vx0') + (pl.col('ax') * pl.col('t'))).alias('vx_f')
|
22 |
-
])
|
23 |
-
|
24 |
-
df = df.with_columns([
|
25 |
-
(-np.arctan(pl.col('vz_f') / pl.col('vy_f')) * (180 / np.pi)).alias('vaa'),
|
26 |
-
(-np.arctan(pl.col('vx_f') / pl.col('vy_f')) * (180 / np.pi)).alias('haa')
|
27 |
-
])
|
28 |
-
|
29 |
-
# Mirror horizontal break for left-handed pitchers
|
30 |
-
df = df.with_columns(
|
31 |
-
pl.when(pl.col('pitcher_hand') == 'L')
|
32 |
-
.then(-pl.col('ax'))
|
33 |
-
.otherwise(pl.col('ax'))
|
34 |
-
.alias('ax')
|
35 |
-
)
|
36 |
-
|
37 |
-
# Mirror horizontal break for left-handed pitchers
|
38 |
-
df = df.with_columns(
|
39 |
-
pl.when(pl.col('pitcher_hand') == 'L')
|
40 |
-
.then(-pl.col('hb'))
|
41 |
-
.otherwise(pl.col('hb'))
|
42 |
-
.alias('hb')
|
43 |
-
)
|
44 |
-
|
45 |
-
# Mirror horizontal release point for left-handed pitchers
|
46 |
-
df = df.with_columns(
|
47 |
-
pl.when(pl.col('pitcher_hand') == 'L')
|
48 |
-
.then(pl.col('x0'))
|
49 |
-
.otherwise(-pl.col('x0'))
|
50 |
-
.alias('x0')
|
51 |
-
)
|
52 |
-
|
53 |
-
# Define the pitch types to be considered
|
54 |
-
pitch_types = ['SI', 'FF', 'FC']
|
55 |
-
|
56 |
-
# Filter the DataFrame to include only the specified pitch types
|
57 |
-
df_filtered = df.filter(pl.col('pitch_type').is_in(pitch_types))
|
58 |
-
|
59 |
-
# Group by pitcher_id and year, then aggregate to calculate average speed and usage percentage
|
60 |
-
df_agg = df_filtered.group_by(['pitcher_id', 'year', 'pitch_type']).agg([
|
61 |
-
pl.col('start_speed').mean().alias('avg_fastball_speed'),
|
62 |
-
pl.col('az').mean().alias('avg_fastball_az'),
|
63 |
-
pl.col('ax').mean().alias('avg_fastball_ax'),
|
64 |
-
pl.len().alias('count')
|
65 |
-
])
|
66 |
-
|
67 |
-
# Sort the aggregated data by count and average fastball speed
|
68 |
-
df_agg = df_agg.sort(['count', 'avg_fastball_speed'], descending=[True, True])
|
69 |
-
df_agg = df_agg.unique(subset=['pitcher_id', 'year'], keep='first')
|
70 |
-
|
71 |
-
# Join the aggregated data with the main DataFrame
|
72 |
-
df = df.join(df_agg, on=['pitcher_id', 'year'])
|
73 |
-
|
74 |
-
# If no fastball, use the fastest pitch for avg_fastball_speed
|
75 |
-
df = df.with_columns(
|
76 |
-
pl.when(pl.col('avg_fastball_speed').is_null())
|
77 |
-
.then(pl.col('start_speed').max().over('pitcher_id'))
|
78 |
-
.otherwise(pl.col('avg_fastball_speed'))
|
79 |
-
.alias('avg_fastball_speed')
|
80 |
-
)
|
81 |
-
|
82 |
-
# If no fastball, use the fastest pitch for avg_fastball_az
|
83 |
-
df = df.with_columns(
|
84 |
-
pl.when(pl.col('avg_fastball_az').is_null())
|
85 |
-
.then(pl.col('az').max().over('pitcher_id'))
|
86 |
-
.otherwise(pl.col('avg_fastball_az'))
|
87 |
-
.alias('avg_fastball_az')
|
88 |
-
)
|
89 |
-
|
90 |
-
# If no fastball, use the fastest pitch for avg_fastball_ax
|
91 |
-
df = df.with_columns(
|
92 |
-
pl.when(pl.col('avg_fastball_ax').is_null())
|
93 |
-
.then(pl.col('ax').max().over('ax'))
|
94 |
-
.otherwise(pl.col('avg_fastball_ax'))
|
95 |
-
.alias('avg_fastball_ax')
|
96 |
-
)
|
97 |
-
|
98 |
-
# Calculate pitch differentials
|
99 |
-
df = df.with_columns(
|
100 |
-
(pl.col('start_speed') - pl.col('avg_fastball_speed')).alias('speed_diff'),
|
101 |
-
(pl.col('az') - pl.col('avg_fastball_az')).alias('az_diff'),
|
102 |
-
(pl.col('ax') - pl.col('avg_fastball_ax')).abs().alias('ax_diff')
|
103 |
-
)
|
104 |
-
|
105 |
-
# Cast the year column to integer type
|
106 |
-
df = df.with_columns(
|
107 |
-
pl.col('year').cast(pl.Int64)
|
108 |
-
)
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
df = df.with_columns([
|
113 |
-
pl.lit('All').alias('all')
|
114 |
-
])
|
115 |
-
|
116 |
-
# Calculate mound_to_release as 60.5 - extension
|
117 |
-
df = df.with_columns([
|
118 |
-
(60.5 - df["extension"]).alias("release_pos_y")
|
119 |
-
])
|
120 |
-
|
121 |
-
# Calculate delta time (Δt)
|
122 |
-
delta_t = (df["release_pos_y"] - df["y0"]) / df["vy0"]
|
123 |
-
|
124 |
-
# Corrected back-calculation of release_pos_x and release_pos_z
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
return df
|
|
|
1 |
+
import polars as pl
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def feature_engineering(df: pl.DataFrame) -> pl.DataFrame:
|
5 |
+
# Extract the year from the game_date column
|
6 |
+
df = df.with_columns(
|
7 |
+
pl.col('game_date').str.slice(0, 4).alias('year')
|
8 |
+
)
|
9 |
+
|
10 |
+
df = df.with_columns([
|
11 |
+
|
12 |
+
(-(pl.col('vy0')**2 - (2 * pl.col('ay') * (pl.col('y0') - 17/12)))**0.5).alias('vy_f'),
|
13 |
+
])
|
14 |
+
|
15 |
+
df = df.with_columns([
|
16 |
+
((pl.col('vy_f') - pl.col('vy0')) / pl.col('ay')).alias('t'),
|
17 |
+
])
|
18 |
+
|
19 |
+
df = df.with_columns([
|
20 |
+
(pl.col('vz0') + (pl.col('az') * pl.col('t'))).alias('vz_f'),
|
21 |
+
(pl.col('vx0') + (pl.col('ax') * pl.col('t'))).alias('vx_f')
|
22 |
+
])
|
23 |
+
|
24 |
+
df = df.with_columns([
|
25 |
+
(-np.arctan(pl.col('vz_f') / pl.col('vy_f')) * (180 / np.pi)).alias('vaa'),
|
26 |
+
(-np.arctan(pl.col('vx_f') / pl.col('vy_f')) * (180 / np.pi)).alias('haa')
|
27 |
+
])
|
28 |
+
|
29 |
+
# Mirror horizontal break for left-handed pitchers
|
30 |
+
df = df.with_columns(
|
31 |
+
pl.when(pl.col('pitcher_hand') == 'L')
|
32 |
+
.then(-pl.col('ax'))
|
33 |
+
.otherwise(pl.col('ax'))
|
34 |
+
.alias('ax')
|
35 |
+
)
|
36 |
+
|
37 |
+
# Mirror horizontal break for left-handed pitchers
|
38 |
+
df = df.with_columns(
|
39 |
+
pl.when(pl.col('pitcher_hand') == 'L')
|
40 |
+
.then(-pl.col('hb'))
|
41 |
+
.otherwise(pl.col('hb'))
|
42 |
+
.alias('hb')
|
43 |
+
)
|
44 |
+
|
45 |
+
# Mirror horizontal release point for left-handed pitchers
|
46 |
+
df = df.with_columns(
|
47 |
+
pl.when(pl.col('pitcher_hand') == 'L')
|
48 |
+
.then(pl.col('x0'))
|
49 |
+
.otherwise(-pl.col('x0'))
|
50 |
+
.alias('x0')
|
51 |
+
)
|
52 |
+
|
53 |
+
# Define the pitch types to be considered
|
54 |
+
pitch_types = ['SI', 'FF', 'FC']
|
55 |
+
|
56 |
+
# Filter the DataFrame to include only the specified pitch types
|
57 |
+
df_filtered = df.filter(pl.col('pitch_type').is_in(pitch_types))
|
58 |
+
|
59 |
+
# Group by pitcher_id and year, then aggregate to calculate average speed and usage percentage
|
60 |
+
df_agg = df_filtered.group_by(['pitcher_id', 'year', 'pitch_type']).agg([
|
61 |
+
pl.col('start_speed').mean().alias('avg_fastball_speed'),
|
62 |
+
pl.col('az').mean().alias('avg_fastball_az'),
|
63 |
+
pl.col('ax').mean().alias('avg_fastball_ax'),
|
64 |
+
pl.len().alias('count')
|
65 |
+
])
|
66 |
+
|
67 |
+
# Sort the aggregated data by count and average fastball speed
|
68 |
+
df_agg = df_agg.sort(['count', 'avg_fastball_speed'], descending=[True, True])
|
69 |
+
df_agg = df_agg.unique(subset=['pitcher_id', 'year'], keep='first')
|
70 |
+
|
71 |
+
# Join the aggregated data with the main DataFrame
|
72 |
+
df = df.join(df_agg, on=['pitcher_id', 'year'])
|
73 |
+
|
74 |
+
# If no fastball, use the fastest pitch for avg_fastball_speed
|
75 |
+
df = df.with_columns(
|
76 |
+
pl.when(pl.col('avg_fastball_speed').is_null())
|
77 |
+
.then(pl.col('start_speed').max().over('pitcher_id'))
|
78 |
+
.otherwise(pl.col('avg_fastball_speed'))
|
79 |
+
.alias('avg_fastball_speed')
|
80 |
+
)
|
81 |
+
|
82 |
+
# If no fastball, use the fastest pitch for avg_fastball_az
|
83 |
+
df = df.with_columns(
|
84 |
+
pl.when(pl.col('avg_fastball_az').is_null())
|
85 |
+
.then(pl.col('az').max().over('pitcher_id'))
|
86 |
+
.otherwise(pl.col('avg_fastball_az'))
|
87 |
+
.alias('avg_fastball_az')
|
88 |
+
)
|
89 |
+
|
90 |
+
# If no fastball, use the fastest pitch for avg_fastball_ax
|
91 |
+
df = df.with_columns(
|
92 |
+
pl.when(pl.col('avg_fastball_ax').is_null())
|
93 |
+
.then(pl.col('ax').max().over('ax'))
|
94 |
+
.otherwise(pl.col('avg_fastball_ax'))
|
95 |
+
.alias('avg_fastball_ax')
|
96 |
+
)
|
97 |
+
|
98 |
+
# Calculate pitch differentials
|
99 |
+
df = df.with_columns(
|
100 |
+
(pl.col('start_speed') - pl.col('avg_fastball_speed')).alias('speed_diff'),
|
101 |
+
(pl.col('az') - pl.col('avg_fastball_az')).alias('az_diff'),
|
102 |
+
(pl.col('ax') - pl.col('avg_fastball_ax')).abs().alias('ax_diff')
|
103 |
+
)
|
104 |
+
|
105 |
+
# Cast the year column to integer type
|
106 |
+
df = df.with_columns(
|
107 |
+
pl.col('year').cast(pl.Int64)
|
108 |
+
)
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
df = df.with_columns([
|
113 |
+
pl.lit('All').alias('all')
|
114 |
+
])
|
115 |
+
|
116 |
+
# Calculate mound_to_release as 60.5 - extension
|
117 |
+
df = df.with_columns([
|
118 |
+
(60.5 - df["extension"]).alias("release_pos_y")
|
119 |
+
])
|
120 |
+
|
121 |
+
# Calculate delta time (Δt)
|
122 |
+
delta_t = (df["release_pos_y"] - df["y0"]) / df["vy0"]
|
123 |
+
# print((df["vx0"] * delta_t + 0.5 * df["ax"] * delta_t ** 2))
|
124 |
+
# Corrected back-calculation of release_pos_x and release_pos_z
|
125 |
+
|
126 |
+
|
127 |
+
df = df.with_columns(
|
128 |
+
pl.when(pl.col('pitcher_hand')== 'R')
|
129 |
+
.then(df["x0"] - df["vx0"] * delta_t - 0.5 * df["ax"] * delta_t ** 2)
|
130 |
+
.otherwise(df["x0"] + df["vx0"] * delta_t - 0.5 * df["ax"] * delta_t ** 2)
|
131 |
+
.alias('release_pos_x')
|
132 |
+
)
|
133 |
+
|
134 |
+
df = df.with_columns([
|
135 |
+
(df["z0"] + df["vz0"] * delta_t + 0.5 * df["az"] * delta_t ** 2).alias("release_pos_z")
|
136 |
+
])
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
return df
|