nesticot commited on
Commit
7590b84
·
verified ·
1 Parent(s): eba64c2

Upload stuff_model/feature_engineering.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. stuff_model/feature_engineering.py +143 -139
stuff_model/feature_engineering.py CHANGED
@@ -1,140 +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
- 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
 
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