nesticot commited on
Commit
adf6a67
·
verified ·
1 Parent(s): 9bbf3f3

Update stuff_model/feature_engineering.py

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