Created
April 4, 2019 15:41
-
-
Save 903124/62195251a6ed3bb597f9b5237ee584a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = ''' | |
SELECT pitch_type,release_speed,release_pos_x,release_pos_z,pitcher,pfx_x,pfx_z,plate_x,plate_z,balls,strikes, | |
vx0,vy0,vz0,ax,ay,az,release_spin_rate,release_extension,description | |
FROM statcast_2018 | |
WHERE release_speed >= 0 | |
''' | |
pitch_stat = pd.read_sql(query,engine) | |
c0 = 5.153E-03 | |
pitch_stat['yR'] = 60-pitch_stat.release_extension | |
pitch_stat['tR'] = (-pitch_stat.vy0 - (pitch_stat.vy0**2-2*pitch_stat.ay*(50-pitch_stat.yR))**0.5)/pitch_stat.ay | |
pitch_stat['vxR'] = pitch_stat.vx0 + pitch_stat.ax * pitch_stat.tR | |
pitch_stat['vyR'] = pitch_stat.vy0 + pitch_stat.ay * pitch_stat.tR | |
pitch_stat['vzR'] = pitch_stat.vz0 + pitch_stat.az * pitch_stat.tR | |
pitch_stat['dv0'] = pitch_stat.release_speed - (pitch_stat.vxR**2+pitch_stat.vyR**2+pitch_stat.vzR**2)**0.5/1.467 | |
pitch_stat['tf'] = (-pitch_stat.vyR-(pitch_stat.vyR**2-2*pitch_stat.ay*(pitch_stat.yR-17/12))**0.5)/pitch_stat.ay | |
pitch_stat['x_mvt'] = pitch_stat.plate_x - pitch_stat.release_pos_x - (pitch_stat.vxR/pitch_stat.vyR)*(17/12-pitch_stat.yR) | |
pitch_stat['z_mvt'] = pitch_stat.plate_z - pitch_stat.release_pos_z - (pitch_stat.vzR/pitch_stat.vyR)*(17/12-pitch_stat.yR) + 0.5*32.174*pitch_stat.tf**2 | |
pitch_stat['vxbar'] = (2*pitch_stat.vxR+pitch_stat.ax*pitch_stat.tf)/2 | |
pitch_stat['vybar'] = (2*pitch_stat.vyR+pitch_stat.ay*pitch_stat.tf)/2 | |
pitch_stat['vzbar'] = (2*pitch_stat.vzR+pitch_stat.az*pitch_stat.tf)/2 | |
pitch_stat['vbar'] = (pitch_stat.vxbar**2+pitch_stat.vybar**2+pitch_stat.vzbar**2)**0.5 | |
pitch_stat['adrag'] = -(pitch_stat.ax*pitch_stat.vxbar+pitch_stat.ay*pitch_stat.vybar+(pitch_stat.az+32.174)*pitch_stat.vzbar)/pitch_stat.vbar | |
pitch_stat['Cd'] = pitch_stat.adrag/(c0*pitch_stat.vbar**2) | |
pitch_stat['amagx'] = pitch_stat.ax+pitch_stat.adrag*pitch_stat.vxbar/pitch_stat.vbar | |
pitch_stat['amagy'] = pitch_stat.ay+pitch_stat.adrag*pitch_stat.vybar/pitch_stat.vbar | |
pitch_stat['amagz'] = pitch_stat.az+pitch_stat.adrag*pitch_stat.vzbar/pitch_stat.vbar+32.174 | |
pitch_stat['amag'] = (pitch_stat.amagx**2+pitch_stat.amagy**2+pitch_stat.amagz**2)**0.5 | |
pitch_stat['Mx'] = 0.5*pitch_stat.amagx*pitch_stat.tf**2*12 | |
pitch_stat['Mz'] = 0.5*pitch_stat.amagz*pitch_stat.tf**2*12 | |
pitch_stat['Cl'] = pitch_stat.amag/(c0*pitch_stat.vbar**2) | |
pitch_stat['S'] = 0.4*pitch_stat.Cl/(1-2.32*pitch_stat.Cl) | |
pitch_stat['spinT'] = 78.92*pitch_stat.S*pitch_stat.vbar | |
pitch_stat['spinTX'] = pitch_stat.spinT*(pitch_stat.vybar*pitch_stat.amagz-pitch_stat.vzbar*pitch_stat.amagy)/(pitch_stat.amag*pitch_stat.vbar) | |
pitch_stat['spinTY'] = pitch_stat.spinT*(pitch_stat.vzbar*pitch_stat.amagx-pitch_stat.vxbar*pitch_stat.amagz)/(pitch_stat.amag*pitch_stat.vbar) | |
pitch_stat['spinTZ'] = pitch_stat.spinT*(pitch_stat.vxbar*pitch_stat.amagy-pitch_stat.vybar*pitch_stat.amagx)/(pitch_stat.amag*pitch_stat.vbar) | |
pitch_stat['spin_check'] = (pitch_stat.spinTX**2+pitch_stat.spinTY**2+pitch_stat.spinTZ**2)**0.5-pitch_stat.spinT | |
pitch_stat['phi'] = np.arctan2(pitch_stat.amagz,pitch_stat.amagx)*180/math.pi | |
pitch_stat['phi'] = pitch_stat['phi'] + pitch_stat['amagz'].apply(lambda x: 360 if x < 0 else 0) | |
pitch_stat['spin_eff'] = pitch_stat.spinT/pitch_stat.release_spin_rate | |
pitch_stat['flag'] = pitch_stat['description'].apply(lambda x: 1 if x == 'swinging_strike' else 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment