-
Notifications
You must be signed in to change notification settings - Fork 0
/
Break Point Save %.py
551 lines (324 loc) · 14.6 KB
/
Break Point Save %.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
import numpy as np
pd.options.display.min_rows = 999
pd.options.display.max_columns = 999
# In[2]:
years = [i for i in range(2011, 2021)]
slams = ['ausopen', 'frenchopen', 'usopen', 'wimbledon']
data = pd.DataFrame()
for year in years:
for slam in slams:
if year >= 2018 and slam in ['ausopen', 'frenchopen']: #these slams did not have the same data collected
continue
try:
new_data = pd.read_csv('./tennis_slam_pointbypoint-master/' + str(year) + '-' + slam + '-points.csv')
if year == 2011 and slam == 'ausopen':
data = new_data
else:
data = pd.concat([new_data, data])
except FileNotFoundError:
print(year, slam)
continue
# In[3]:
#function to define a breakpoint for either player
def breakPoint(x):
if x['PointServer'] == 2:
if x['P1Score'] == 'GAME':
return True
if (x['P1Score'] == '40' and x['P2Score'] != 'AD') and (x['PointWinner'] == 2 and x['P2Score'] != 'GAME'):
return True
if x['PointServer'] == 1:
if x['P2Score'] == 'GAME':
return True
if (x['P2Score'] == '40' and x['P1Score'] != 'AD') and (x['PointWinner'] == 1 and x['P1Score'] != 'GAME'):
return True
if x['P1BreakPoint'] == 1 or x['P2BreakPoint'] == 1:
return True
return False
#function to identify surface of the match
def surface(x):
if 'ausopen' in x['match_id']:
return 'Hard'
if 'wimbledon' in x['match_id']:
return 'Grass'
if 'frenchopen' in x['match_id']:
return "Clay"
if 'usopen' in x['match_id']:
return 'Hard'
print(x['match_id'])
# In[4]:
import seaborn as sn
data['DoubleFault'] = (data.P1DoubleFault == 1) | (data.P2DoubleFault == 1)
data['Speed_MPH'] = data.Speed_KMH * 0.621371
data = data[(data.Speed_MPH != 0) & (data.DoubleFault != True)] #drop data that did not have a serve speed
#feature collection
data.loc[:, 'Ace'] = (data.P1Ace == 1) | (data.P2Ace == 1)
data.loc[:,'NetPoint'] = (data.P1NetPoint == 1) | (data.P2NetPoint == 1)
data.loc[:,'NetPointWon'] = (data.P1NetPointWon == 1) | (data.P2NetPointWon == 1)
data.loc[:,'UnforcedError'] = (data.P1UnfErr == 1) | (data.P2UnfErr == 1)
data.loc[:,'Winner'] = (data.P1Winner == 1) | (data.P2Winner == 1)
data.loc[:,'FirstSrvIn'] = (data.ServeNumber == 1) | ((data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1))
data.loc[:,'BreakPoint'] = data.loc[:, ['PointServer', 'P1Score', 'P2Score', 'P1BreakPoint', 'P2BreakPoint', 'PointWinner']].apply(lambda x: breakPoint(x), axis = 1)
data.loc[:,'BreakPointWon'] = (data.BreakPoint) & (data.PointServer != data.PointWinner)
data.loc[:,'DF'] = (data.P1DoubleFault == 1) | (data.P2DoubleFault == 1)
data.loc[:,'ServeSpeed'] = data.Speed_KMH * 0.621371
data.loc[:,'ServerWon'] = (data.PointWinner == data.PointServer)
data.loc[:,'ServerLost'] = (data.PointWinner != data.PointServer)
data.loc[:,'Surface'] = data.loc[:, ['match_id', 'P1Score']].apply(lambda x: surface(x), axis = 1)
data.loc[:,'GamesPlayed'] = data.P1GamesWon + data.P2GamesWon
data.loc[:,'ServerNetPoint'] = ((data.P1NetPoint == 1) & (data.PointServer == 1)) | ((data.P2NetPoint == 1) & (data.PointServer == 2))
data.loc[:,"ServerNetPointWon"] = (data.ServerNetPoint & (data.PointServer == data.PointWinner))
# In[5]:
#rally length on every point
plt.grid()
a_plot = sn.histplot(x = data.Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
a_plot.set(ylim = (0, .45))
plt.title("Total Rally Length")
# In[6]:
#rally length on break points
a_plot = sn.histplot(x = data[data.BreakPoint == True].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
a_plot.set(ylim = (0, .45))
plt.grid()
plt.title("Total Rally Length on Break Points")
# In[7]:
#rally length on break point saves
a_plot = sn.histplot(x = data[(data.BreakPoint == True) & (data.BreakPointWon != True)].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
a_plot.set(ylim = (0, .45))
plt.grid()
plt.title("Total Rally Length on Break Point Saves")
# In[8]:
a_plot = sn.lineplot(x = [i for i in range(0,12)], y = data.groupby('GamesPlayed').mean()['BreakPoint'][0:11])
a_plot.set(ylim = (0, .19))
plt.title("Break Points Faced per Game")
plt.grid()
plt.xlabel("Games into Set")
# In[9]:
#bucketing by serve speed and then finding out win percentage and first serve in percentage
#we want the data to be for serves and how serve speed affects break points
#want to make aces known and first serve percentage known
buckets = []
save_percentage = []
rally_length = []
winner_percentage_save = []
winner_percentage_loss = []
unforced_error_percentage_loss = []
unforced_error_percentage_save = []
net_point = []
net_point_won = []
return_net_point = []
return_net_point_won = []
for i in range(80, 145, 5): #going up by fives if between then we bucket them for break point saves and first serves
bucket_data = data[(data.ServeSpeed >= i) & (data.ServeSpeed <= i + 5)]
trimmed_data = bucket_data[bucket_data.BreakPoint]
x = bucket_data.groupby('BreakPoint').mean()
y = trimmed_data.groupby('BreakPointWon').mean()
save_percentage.append(1 - x.loc[True, 'BreakPointWon'])#doing the one because has two entries True and False
rally_length.append(y.loc[True, 'Rally'])
buckets.append(i)
winner_percentage_loss.append(y.loc[False, 'Winner'])
winner_percentage_save.append(y.loc[True, 'Winner'])
unforced_error_percentage_loss.append(y.loc[True, 'UnforcedError'])
unforced_error_percentage_save.append(y.loc[False, 'UnforcedError'])
net_point.append(x.loc[True, 'ServerNetPoint'])
net_point_won.append(x.loc[True, 'ServerNetPointWon'] / x.loc[True, 'ServerNetPoint'])
return_net_point.append(x.loc[True, 'NetPoint'] - x.loc[True, 'ServerNetPoint'])
return_net_point_won.append((x.loc[True, 'NetPointWon'] - x.loc[True, 'ServerNetPointWon']) / (x.loc[True, 'NetPoint'] - x.loc[True, 'ServerNetPoint']))
# In[10]:
#Save percentage by serve speed
import matplotlib.ticker as mtick
ax = sn.lineplot(x = buckets, y = [i * 100 for i in save_percentage])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Save Percentage by Serve Speed")
plt.ylabel("Save Percentage")
plt.xlabel("Serve Speed")
# In[11]:
#winner percentage by player
ax1 = sn.lineplot(x = buckets, y = [i *100 for i in winner_percentage_loss], label = 'Server')
ax2 = sn.lineplot(x = buckets, y = [i * 100 for i in winner_percentage_save], label = 'Returner')
ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Winner Percentage Server vs Returner by Serve Speed")
plt.ylabel("Winner Percentage")
plt.xlabel("Serve Speed MPH")
# In[12]:
#unforced error percentage by player
ax1 = sn.lineplot(x = buckets, y = [i * 100 for i in unforced_error_percentage_save], label = 'Server')
ax2 = sn.lineplot(x = buckets, y = [i * 100 for i in unforced_error_percentage_loss], label = 'Returner')
ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
ax2.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Unforced Error Percentage Server vs Returner by Serve Speed")
plt.ylabel("Unforced Error Percentage")
plt.xlabel("Serve Speed MPH")
# In[13]:
data[data.BreakPoint].groupby('FirstSrvIn').mean()
# In[17]:
#Server percentage chance to get to the net
ax = sn.lineplot(x = buckets, y = [i * 100 for i in net_point])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Server Percentage Chance to Get to the Net")
plt.ylabel("Server Net Point Percentage")
plt.xlabel("Serve Speed MPH")
# In[18]:
#server win percentage at the net
ax = sn.lineplot(x = buckets, y = [i * 100 for i in net_point_won])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Server Win Percentage at the Net by Serve Speed")
plt.ylabel("Server Net Point Win Percentage")
plt.xlabel("Serve Speed MPH")
# In[16]:
#Returner percentage chance to get to the net
ax = sn.lineplot(x = buckets, y = [i * 100 for i in return_net_point])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Returner Percent Chance to get to Net by Serve Speed")
plt.ylabel("Returner Net Point Win Percentage")
plt.xlabel("Serve Speed MPH")
# In[18]:
#returner win percentage at the net
ax = sn.lineplot(x = buckets, y = [i * 100 for i in return_net_point_won])
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.grid()
plt.title("Returner Win Percentage at the Net by Serve Speed")
plt.ylabel("Returner Net Point Win Percentage")
plt.xlabel("Serve Speed MPH")
# In[161]:
#1st serve rally length
a_plot = sn.histplot(x = data[(data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1)].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
plt.title("Rally Length on First Serves")
# In[162]:
#second serve rally length
a_plot = sn.histplot(x = data[(data.P1SecondSrvIn == 1) | (data.P2SecondSrvIn == 1)].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
plt.title("Rally Length on Second Serves")
# In[163]:
# first serve rally length without aces
a_plot = sn.histplot(x = data[((data.P1Ace == 1) | (data.P2Ace != 1)) & ((data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1))].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
plt.title("Rally Length First Serve without Aces")
# In[164]:
# rally length when the server won the point
a_plot = sn.histplot(x = data[data.PointWinner == data.PointServer].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
plt.title("Rally Length When the Server Won the point")
# In[165]:
# rally length when the server loses the point
a_plot = sn.histplot(x = data[data.PointWinner != data.PointServer].Rally, stat = 'probability', binwidth = 3)
a_plot.set(xlim = (0, 35))
a_plot.set(ylim = (0, .45))
plt.title("Rally Length When the Server Loses the Point")
# In[166]:
data['ServerWon'] = (data.PointWinner == data.PointServer)
data['ServerLost'] = (data.PointWinner != data.PointServer)
# In[167]:
#bucketing by fours and then determining serve percentage winning
bucket = []
won_points = []
total_points = []
series = data.groupby('Rally').sum()['ServerWon']
series2 = data.groupby('Rally').sum()['ServerLost'] + series
for i in range(1, 32, 4):
bucket.append(i)
won_points.append(series[i] + series[i+1] + series[i+2] + series[i+3])
total_points.append(series2[i] + series2[i+1] + series2[i+2] + series2[i+3])
final = []
for i in range(len(won_points)):
final.append(won_points[i] / total_points[i])
# In[168]:
a_plot = sn.lineplot(x = bucket, y = final)
a_plot.set(ylim = (.2, .8))
plt.title("Rally Length by Server Win Percentage")
# In[169]:
#bucketing by fours and then determining serve percentage winning on first serve
bucket = []
won_points = []
total_points = []
series = data[(data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1)].groupby('Rally').sum()['ServerWon']
series2 = data[(data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1)].groupby('Rally').sum()['ServerLost'] + series
for i in range(1, 32, 4):
bucket.append(i)
won_points.append(series[i] + series[i+1] + series[i+2] + series[i+3])
total_points.append(series2[i] + series2[i+1] + series2[i+2] + series2[i+3])
final = []
for i in range(len(won_points)):
final.append(won_points[i] / total_points[i])
# In[170]:
a_plot = sn.lineplot(x = bucket, y = final)
a_plot.set(ylim = (.2, .8))
plt.title("Rally Length Win Percentage on First Serves")
# In[171]:
#bucketing by fours and then determining serve percentage winning on second serve
bucket = []
won_points = []
total_points = []
series = data[(data.P1SecondSrvIn == 1) | (data.P2SecondSrvIn == 1)].groupby('Rally').sum()['ServerWon']
series2 = data[(data.P1SecondSrvIn == 1) | (data.P2SecondSrvIn == 1)].groupby('Rally').sum()['ServerLost'] + series
for i in range(1, 32, 4):
bucket.append(i)
won_points.append(series[i] + series[i+1] + series[i+2] + series[i+3])
total_points.append(series2[i] + series2[i+1] + series2[i+2] + series2[i+3])
final = []
for i in range(len(won_points)):
final.append(won_points[i] / total_points[i])
a_plot = sn.lineplot(x = bucket, y = final)
a_plot.set(ylim = (.2, .8))
plt.title("Rally Length Win Percentage on Second Serves")
# In[172]:
#bucketing by fours and then determining win percentage on break points
bucket = []
won_points = []
total_points = []
series = data[(data.P1BreakPoint == 1) | (data.P2BreakPoint == 1)].groupby('Rally').sum()['ServerWon']
series2 = data[(data.P1BreakPoint == 1) | (data.P2BreakPoint == 1)].groupby('Rally').sum()['ServerLost'] + series
for i in range(1, 32, 4):
bucket.append(i)
if i + 3 >= 32:
won_points.append(series[i] + series[i+1] + series[i+2])
total_points.append(series2[i] + series2[i+1] + series2[i+2])
else:
print(i)
won_points.append(series[i] + series[i+1] + series[i+2] + series[i+3])
total_points.append(series2[i] + series2[i+1] + series2[i+2] + series2[i+3])
final = []
for i in range(len(won_points)):
final.append(won_points[i] / total_points[i])
a_plot = sn.lineplot(x = bucket, y = final)
a_plot.set(ylim = (.2, .8))
plt.title("Rally Length Break Point Saved")
# In[173]:
data2 = data[(data.P1FirstSrvIn == 1) | (data.P2FirstSrvIn == 1)]
#bucketing by fours and then determining serve percentage winning on first serve
bucket = []
won_points = []
total_points = []
series = data2[(data2.P1BreakPoint == 1) | (data2.P2BreakPoint == 1)].groupby('Rally').sum()['ServerWon']
series2 = data2[(data2.P1BreakPoint == 1) | (data2.P2BreakPoint == 1)].groupby('Rally').sum()['ServerLost'] + series
for i in range(1, 32, 4):
bucket.append(i)
if i + 3 >= 32:
won_points.append(series[i] + series[i+1] )
total_points.append(series2[i] + series2[i+1])
else:
print(i)
won_points.append(series[i] + series[i+1] + series[i+2] + series[i+3])
total_points.append(series2[i] + series2[i+1] + series2[i+2] + series2[i+3])
final = []
for i in range(len(won_points)):
final.append(won_points[i] / total_points[i])
a_plot = sn.lineplot(x = bucket, y = final)
a_plot.set(ylim = (.2, .8))
plt.title("Rally Length Break Point Saved on First Serve")
# In[ ]: