nesticot commited on
Commit
951b75f
·
verified ·
1 Parent(s): a88295a

Upload folder using huggingface_hub

Browse files
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
stuff_model/stuff_apply.py CHANGED
@@ -1,7 +1,7 @@
1
  import polars as pl
2
  import joblib
3
 
4
- model = joblib.load('stuff_model/lgbm_model_2020_2023.joblib')
5
  # Read the values from the text file
6
  with open('stuff_model/target_stats.txt', 'r') as file:
7
  lines = file.readlines()
 
1
  import polars as pl
2
  import joblib
3
 
4
+ model = joblib.load('stuff_model/stuff_model.joblib')
5
  # Read the values from the text file
6
  with open('stuff_model/target_stats.txt', 'r') as file:
7
  lines = file.readlines()
stuff_model/stuff_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0b14dc2756695cfa67a340dfa77616894b3ec0715c61ad73ded704fc24386e8
3
+ size 3109736
stuff_model/target_stats.txt CHANGED
@@ -1,2 +1,2 @@
1
- 0.0034732498406374636
2
- 0.006846752748626548
 
1
+ 0.0036819646648982335
2
+ 0.006743548730130907
stuff_model/tj_stuff_plus_pitch.csv CHANGED
@@ -1,16 +1,18 @@
1
- pitch_type,mean,std,median,min,max,percentile_1,percentile_99
2
- ST,106.44784631565936,5.593943599731136,106.24878922952112,91.18894850636659,125.29541262167034,91.69322149368426,125.25688309207108
3
- SV,103.73183202363764,3.001226780758946,103.50047554089315,93.3173875900245,111.34757479687066,93.32953434698274,111.33689503153641
4
- SL,103.49296290610897,5.265572779780409,103.19144262214559,88.84957017284297,121.88798777026031,89.76670287371176,121.36013955239422
5
- KC,101.8993919341341,4.271694896723436,100.79211889194949,93.69754063161618,119.4933202093256,93.75149298057133,119.38166236091195
6
- All,99.9275100894791,5.01699442232884,99.65265124489378,84.73033633038408,116.94934527087541,86.65905811630736,116.7610246502804
7
- CU,99.88832068607897,4.615228571103906,99.08993373693156,89.84495168337246,119.90089262632986,90.20429983334718,117.89567125997061
8
- FC,98.83449547008738,5.811964883678063,98.54483029899575,83.20928731685326,119.78700324933075,83.34007602984008,118.21186533190846
9
- FS,98.25541635267653,6.898952096824192,98.46204303842217,72.25450024197754,114.88400714657823,73.39595959354874,114.78967217449389
10
- FO,98.15224613640243,1.081819065809178,99.94816563615653,94.0023252668585,100.50624750619224,94.0142169475971,100.50513134245217
11
- FF,97.29024735737988,6.078459125845886,97.09670890504734,81.2230917971995,118.10419744965911,81.32311771953398,117.7938724746093
12
- SC,97.27958020025409,1.2452898498180456,97.27958020025409,93.536223938276,101.02293646223218,93.54371065079995,101.01544974970822
13
- CH,96.35866365133434,6.178939251378385,95.80884625564597,81.28802319264824,121.14136334013493,82.02275793969746,119.09639344796777
14
- SI,95.14161603816645,4.9734372581529955,95.11657827702109,82.5850956341191,112.99618112461533,82.8856383780296,112.72626192694757
15
- CS,93.97853627048322,0.0,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322
16
- KN,93.41890096234394,0.0,93.41890096234394,93.41890096234394,93.41890096234394,93.41890096234394,93.41890096234394
 
 
 
1
+ pitch_type,mean,std,median,min,max,percentile_1,percentile_99
2
+ ST,106.44784631565936,5.593943599731136,106.24878922952112,91.18894850636659,125.29541262167034,91.69322149368426,125.25688309207108
3
+ SV,103.73183202363764,3.001226780758946,103.50047554089315,93.3173875900245,111.34757479687066,93.32953434698274,111.33689503153641
4
+ SL,103.49296290610897,5.265572779780409,103.19144262214559,88.84957017284297,121.88798777026031,89.76670287371176,121.36013955239422
5
+ KC,101.8993919341341,4.271694896723436,100.79211889194949,93.69754063161618,119.4933202093256,93.75149298057133,119.38166236091195
6
+ All,99.9275100894791,5.01699442232884,99.65265124489378,84.73033633038408,116.94934527087541,86.65905811630736,116.7610246502804
7
+ CU,99.88832068607897,4.615228571103906,99.08993373693156,89.84495168337246,119.90089262632986,90.20429983334718,117.89567125997061
8
+ FC,98.83449547008738,5.811964883678063,98.54483029899575,83.20928731685326,119.78700324933075,83.34007602984008,118.21186533190846
9
+ FS,98.25541635267653,6.898952096824192,98.46204303842217,72.25450024197754,114.88400714657823,73.39595959354874,114.78967217449389
10
+ FO,98.15224613640243,1.081819065809178,99.94816563615653,94.0023252668585,100.50624750619224,94.0142169475971,100.50513134245217
11
+ FF,97.29024735737988,6.078459125845886,97.09670890504734,81.2230917971995,118.10419744965911,81.32311771953398,117.7938724746093
12
+ FA,97.29024735737988,6.078459125845886,97.09670890504734,81.2230917971995,118.10419744965911,81.32311771953398,117.7938724746093
13
+ SC,97.27958020025409,1.2452898498180456,97.27958020025409,93.536223938276,101.02293646223218,93.54371065079995,101.01544974970822
14
+ CH,96.35866365133434,6.178939251378385,95.80884625564597,81.28802319264824,121.14136334013493,82.02275793969746,119.09639344796777
15
+ SI,95.14161603816645,4.9734372581529955,95.11657827702109,82.5850956341191,112.99618112461533,82.8856383780296,112.72626192694757
16
+ CS,93.97853627048322,0.0,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322
17
+ EP,93.97853627048322,0.0,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322,93.97853627048322
18
+ KN,93.41890096234394,0.0,93.41890096234394,93.41890096234394,93.41890096234394,93.41890096234394,93.41890096234394