-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseballDatabankNB.py
296 lines (157 loc) · 6.47 KB
/
BaseballDatabankNB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# coding: utf-8
# In[ ]:
# load BaseballDataBank.py
import pandas as pd
import glob, os
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
# ## Reading in BaseballDatabank csv files
# In[ ]:
def read_all_databank_core_csv(directory):
"""
read all csv files in the specified baseball databank directory and
populate a dictionary storing each of the tables keyed to its name
"""
dfs = {}
files = glob.glob('{}/*.csv'.format(directory))
for f in files:
d, name = os.path.split(f)
table = os.path.splitext(name)[0]
df = pd.read_csv(f)
dfs[table] = df
return dfs
bbdfs = read_all_databank_core_csv('baseballdatabank/core/')
# extract a few for further processing
batting = bbdfs['Batting']
pitching = bbdfs['Pitching']
teams = bbdfs['Teams']
# ## Taking a peek
# ### batting is year-by-year for each individual player
# ### teams is year-by-year for each team
# In[ ]:
pd.options.display.max_colwidth = 350
# In[ ]:
batting.head()
# In[ ]:
pitching.columns
# In[ ]:
teams.head()
# ## Filtering data (Hank Aaron's year-by-year batting statistics)
# In[ ]:
batting[batting.playerID=='aaronha01']
# ## Aggregating data (Hank Aaron's career batting statistics)
# In[ ]:
batting[batting.playerID=='aaronha01'].sum(numeric_only=True).drop(['yearID']).astype(int)
# ## Adding derived data: calculating singles (1B) from H,2B,3B,HR
# In[ ]:
batting['1B'] = batting['H'] - batting['2B'] - batting['3B'] - batting['HR']
teams['1B'] = teams['H'] - teams['2B'] - teams['3B'] - teams['HR']
batting.head()
# ## A succinct history of hitting in baseball
# ### (time progresses from light to dark)
# In[ ]:
batting_by_year = batting.groupby('yearID').sum().reset_index()
hit_vars = ['1B', '2B', '3B', 'HR', 'SO', 'BB']
# In[ ]:
#pg = sns.pairplot(batting_by_year, size=2, vars=hit_vars, hue='yearID', palette='Blues')
#pg.hue_names = ["_nolegend_"]
g = sns.PairGrid(batting_by_year, vars=hit_vars, hue='yearID', palette='Blues')
g = g.map_offdiag(plt.scatter, edgecolor="w", s=40)
# g = g.map_diag(plt.hist, edgecolor="w")
# In[ ]:
pg = sns.pairplot(batting_by_year, height=2, vars=hit_vars, hue='yearID', palette='Blues')
# ## The correlation of hitting statistics
# In[ ]:
sns.heatmap(batting_by_year[hit_vars].corr(), annot=True)
# ## Grouping batting data by player
# In[ ]:
pl_bat = batting.groupby('playerID').sum().reset_index()
bbdfs['CareerBatting'] = pl_bat
pl_bat.head()
# ## Adding more derived data: The Slash Line (BA / OBP / SLG)
# In[ ]:
pl_bat['BA']= pl_bat['H'] / pl_bat['AB']
pl_bat['OBP'] = (pl_bat['H']+pl_bat['BB']+pl_bat['HBP']) / (pl_bat['AB']+pl_bat['BB']+pl_bat['HBP']+pl_bat['SF'])
pl_bat['SLG'] = (pl_bat['1B']+2*pl_bat['2B']+3*pl_bat['3B']+4*pl_bat['HR']) / pl_bat['AB']
pl_bat.head()
# ## Filtering data part 2
# ### Top all-time slugging percentages (at least 100 AB)
# In[ ]:
pl_bat[pl_bat.AB >= 100].sort_values(by='SLG', ascending=False).head(30)
# ## Sabermetrics: the "Pythagorean" theorem of baseball
# In[ ]:
teams['Observed_WinRatio'] = teams.W/teams.G
teams['Expected_WinRatio_183'] = 1 / (1 + (teams.RA/teams.R)**1.83)
teams['Expected_WinRatio_2'] = 1 / (1 + (teams.RA/teams.R)**2)
teams['Overachieving'] = teams['Observed_WinRatio']/teams['Expected_WinRatio_183']
teams['Fraction_Runs'] = teams.R/(teams.R + teams.RA)
teams.plot.scatter('Expected_WinRatio_2', 'Observed_WinRatio')
# ## Writing all the dataframes to a SQL database
# In[ ]:
def write_all_tables_to_sqlite(dfs, sql_filename):
engine = create_engine('sqlite:///{}'.format(sql_filename))
for table, df in dfs.items():
df.to_sql(table, con=engine, index=False)
engine.dispose()
sqlite_filename = 'bbdb.sqlite'
try:
os.remove(sqlite_filename)
except FileNotFoundError:
pass
write_all_tables_to_sqlite(bbdfs, sqlite_filename)
# ## Make SQL query to Baseball DB
# In[ ]:
engine = create_engine('sqlite:///bbdb.sqlite')
top_slugging = pd.read_sql_query('select * from CareerBatting where AB>= 100 order by SLG desc limit 30', engine)
top_slugging
# ## More history: the saga of Home Runs
# In[ ]:
ax = batting_by_year.plot('yearID', 'HR', figsize=(16,8))
annot1920 = plt.text(1920, 0, '<End of dead ball era')
annot1942 = plt.text(1942, 400, '<WW2')
annot1961 = plt.text(1961, 1200, '<3 teams added to AL')
annot1962 = plt.text(1962, 1400, '<2 teams added to NL')
annot1969 = plt.text(1969, 1600, '<2 teams added to both AL + NL')
annot1995 = plt.text(1995, 3000, '<Steroids rampant')
annot2003 = plt.text(2003, 3500, '<Steroids tested for')
annot2015 = plt.text(2015, 4000, '<Fascination with launch angle / strikeouts be damned')
# In[ ]:
ax = batting_by_year.plot('yearID', 'HR', figsize=(16,8))
# ## Correcting for demographics: HR per AB
# In[ ]:
batting_by_year = batting.groupby('yearID').sum().reset_index()
batting_by_year.set_index('yearID', inplace=True)
batting_by_year_perAB = batting_by_year.div(batting_by_year.AB, axis=0).reset_index()
batting_by_year.reset_index(inplace=True)
# In[ ]:
ax = batting_by_year_perAB.plot('yearID', 'HR', figsize=(16,8))
annot1920 = plt.text(1920, 0.001, '<End of dead ball era')
annot1942 = plt.text(1942, 0.009, '<WW2')
annot1961 = plt.text(1961, 0.013, '<3 teams added to AL')
annot1962 = plt.text(1962, 0.014, '<2 teams added to NL')
annot1969 = plt.text(1969, 0.015, '<2 teams added to both AL + NL')
annot1995 = plt.text(1995, 0.020, '<Steroids rampant')
annot2003 = plt.text(2003, 0.0225, '<Steroids tested for')
annot2015 = plt.text(2015, 0.025, '<Fascination with launch angle / strikeouts be damned')
# In[ ]:
pitching_by_year = pitching.groupby('yearID').sum().reset_index()
# In[ ]:
pitching_by_year.set_index('yearID', inplace=True)
# In[ ]:
pitching_by_year_perIPouts = pitching_by_year.div(pitching_by_year.IPouts, axis=0).reset_index()
# In[ ]:
pitching_by_year_perIPouts
# In[ ]:
pitching_by_year.reset_index(inplace=True)
# In[ ]:
ax = pitching_by_year_perIPouts.plot('yearID', 'SO', figsize=(16,8))
annot1920 = plt.text(1920, 0.05, '<End of dead ball era')
annot1942 = plt.text(1942, 0.09, '<WW2')
annot1961 = plt.text(1961, 0.13, '<3 teams added to AL')
annot1962 = plt.text(1962, 0.14, '<2 teams added to NL')
annot1969 = plt.text(1969, 0.15, '<2 teams added to both AL + NL')
annot1995 = plt.text(1995, 0.20, '<Steroids rampant')
annot2003 = plt.text(2003, 0.225, '<Steroids tested for')
annot2015 = plt.text(2015, 0.25, '<Fascination with launch angle / strikeouts be damned')