-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipl.py
452 lines (326 loc) · 14.4 KB
/
ipl.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
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import seaborn as sns
import cricstats
batsman_bowler = st.sidebar.radio("Batsman or Bowler Analysis",
('Batsman',
"Bowler"))
ipl_season = st.sidebar.selectbox("IPL Season",
("2007","2009",
"2010",
"2011",
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020","2021","All Seasons"))
if batsman_bowler == "Batsman":
strategy = st.sidebar.selectbox("Select Analysis Strategy",
('Batsman Ranking ( 6 dimensions )',
'Runs',
'Most Fours',
'Most Sixes',
'Highest Average',
'Highest Strike Rate',
'Most Centuries',
'Most Fifties',
'Most Thirties',
"CHT"))
else:
strategy = st.sidebar.selectbox("Select Analysis Strategy",
('Bowler Ranking ( 4 dimensions )',
'Most Wickets',
'Best Average',
'Best Strike Rate',
'Best Economy',
'Bowler Ranking ( 4 dimensions )'))
title_page_header = "# IPL " + str(ipl_season) + " Analysis"
st.markdown(title_page_header)
import matplotlib.pyplot as plt
import seaborn as sns
def draw_plot(fig_size_x = 10,
fig_size_y = 10,
tick_params_labelsize = 14,
xlabel_name_fontsize = 20,
ylabel_name_fontsize = 20,
title_name_fontsize = 20,
xlabel_name = "",
ylabel_name = "",
title_name = ""):
#get current figure
fig=plt.gcf()
#set the size of the figure
fig.set_size_inches(fig_size_x,fig_size_y)
#get axes of the current figure
ax = fig.gca()
# set the label size of the ticks of the axes
ax.tick_params(labelsize=tick_params_labelsize)
# set the label size of the x axis
ax.set_xlabel(xlabel_name,fontsize = xlabel_name_fontsize)
# set the label size of the y axis
ax.set_ylabel(ylabel_name,fontsize = ylabel_name_fontsize)
# set the title of the plot
ax.set_title(title_name,fontsize = title_name_fontsize)
def display_category(filename,ipl_season= ""):
batsman_stats = cricstats.get_batsman_stats(filename,ipl_season)
if strategy == "Runs":
display_batsman_runs(batsman_stats)
elif strategy == "Most Fours":
display_fours_six(batsman_stats,4,"batsman_fours")
elif strategy == "Most Sixes":
display_fours_six(batsman_stats,6,"batsman_six")
elif strategy == "Highest Average":
display_batsman_average(batsman_stats)
elif strategy == "Highest Strike Rate":
display_batsman_strike_rate(batsman_stats)
elif strategy == "Most Centuries":
display_batsman_milestone(batsman_stats,"century")
elif strategy == "Most Fifties":
display_batsman_milestone(batsman_stats,"fifty")
elif strategy == "Most Thirties":
display_batsman_milestone(batsman_stats,"thirty")
elif strategy == "CHT":
display_CHT(batsman_stats)
elif strategy == "Batsman Ranking ( 6 dimensions )":
display_batsman_ranking(batsman_stats)
def get_bowler_average(bowler_stats):
bowler_stats = bowler_stats.sort_values(by = "average",ascending = True)
xlabel_name = "Average"
ylabel_name = "Bowler"
title_name = " Bowler Average Distribution [ Runs / Wickets ]"
sns.barplot( x = "average" , y = "bowler" , data = bowler_stats.head(10), color = "blue")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(bowler_stats.head(10)["average"].values):
ax.text(2, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_batsman_runs(batsman_runs):
xlabel_name = "Runs"
ylabel_name = "Batsman"
title_name = " Batsman Runs Distribution"
sns.barplot( x = "batsman_runs" , y = "batsman" , data = batsman_runs.head(10), color = "blue")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(batsman_runs.head(10)["batsman_runs"].values):
ax.text(300, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def get_boundary(all_data,boundary,boundary_name):
all_data_boundary = all_data[(all_data["runs_batsman"] == boundary)]
boundary_runs = all_data_boundary.groupby(["batsman"])["batsman"].count() \
.reset_index( name = boundary_name).sort_values( by = boundary_name,ascending = False)
return(boundary_runs)
def display_fours_six(boundary_runs,boundary,boundary_name):
boundary_runs = boundary_runs.sort_values(by =boundary_name,ascending =False)
xlabel_name = str(boundary) + " runs "
ylabel_name = "Batsman"
title_name = " Batsman " + str(boundary) + " Distribution "
sns.barplot( x = boundary_name , y = "batsman" , data = boundary_runs.head(10), color = "purple")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(boundary_runs.head(10)[boundary_name].values):
ax.text(5, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_batsman_average(batsman_all_stats):
batsman_all_stats = batsman_all_stats[batsman_all_stats["batsman_runs"] >= 300]
batsman_all_stats = batsman_all_stats.sort_values(by ="average",ascending =False)
xlabel_name = "average"
ylabel_name = "Batsman"
title_name = " Batsman average Distribution"
sns.barplot( x = "average" , y = "batsman" ,
data = batsman_all_stats.head(10), color = "red")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(batsman_all_stats.head(10)["average"].values):
ax.text(10, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_batsman_strike_rate(batsman_all_stats):
batsman_all_stats = batsman_all_stats[batsman_all_stats["batsman_runs"] >= 300]
batsman_all_stats = batsman_all_stats.sort_values(by ="strike_rate",ascending =False)
xlabel_name = "average"
ylabel_name = "Batsman"
title_name = " Batsman strike rate Distribution"
sns.barplot( x = "strike_rate" , y = "batsman" , data = batsman_all_stats.head(10), color = "brown")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(batsman_all_stats.head(10)["strike_rate"].values):
ax.text(10, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_batsman_milestone(batsman_milestone,milestone_name):
batsman_milestone = batsman_milestone.sort_values(by =milestone_name,ascending =False)
xlabel_name = str(milestone_name) + " milestones "
ylabel_name = "Batsman"
title_name = " Batsman " + str(milestone_name) + " Distribution "
sns.barplot( x = milestone_name , y = "batsman" , data = batsman_milestone.head(10),
color = "blue")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(batsman_milestone.head(10)[milestone_name].values):
ax.text(0.5, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_CHT(batsman_all_stats):
batsman_all_stats = batsman_all_stats.sort_values(by ="CHT",ascending =False)
xlabel_name = "CHT"
ylabel_name = "Batsman"
title_name = " Batsman CHT Distribution"
sns.barplot( x = "CHT" , y = "batsman" , data = batsman_all_stats.head(10), color = "green")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(batsman_all_stats.head(10)["CHT"].values):
ax.text(0.5, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_batsman_ranking(batsman_all_stats):
st.markdown("<hr/>",unsafe_allow_html= True)
title_page = "## The ranking of the best IPL " + str(ipl_season)+ " batsman"
st.markdown(title_page)
st.markdown("<hr/>",unsafe_allow_html= True)
batsman_all_stats_6 = batsman_all_stats[['batsman_runs', 'batsman_fours',
'batsman_six', 'average', 'strike_rate',"CHT"]]
batsman_all_stats_6.index = batsman_all_stats["batsman"]
from sklearn.preprocessing import scale
X = pd.DataFrame(scale(batsman_all_stats_6),
index=batsman_all_stats_6.index,
columns=batsman_all_stats_6.columns)
from sklearn.decomposition import PCA
pca = PCA()
df_plot = pd.DataFrame(pca.fit_transform(X),
columns=['PC1','PC2','PC3','PC4','PC5','PC6'], index=X.index)
pca = PCA().fit(X)
df_plot = df_plot.reset_index()
df_plot = df_plot.sort_values(by = ["PC1","PC2"],ascending = False)
st.table(df_plot.head(10))
plt.figure(figsize=(10,7))
plt.plot(np.cumsum(pca.explained_variance_ratio_), color='k', lw=2)
plt.xlabel('Number of components')
plt.ylabel('Total explained variance')
plt.axhline(0.9, c='r')
plt.show();
fig = plt.gcf()
st.pyplot(fig)
def display_bowling_average(bowler_stats):
bowler_stats = bowler_stats.sort_values(by = "average",ascending = True)
xlabel_name = "Average"
ylabel_name = "Bowler"
title_name = " Bowler Average Distribution [ Runs / Wickets ]"
sns.barplot( x = "average" , y = "bowler" , data = bowler_stats.head(10), color = "blue")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(bowler_stats.head(10)["average"].values):
ax.text(2, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_most_wickets(bowler_stats):
bowler_stats = bowler_stats.sort_values(by = "wickets",ascending = False)
xlabel_name = "Wickets"
ylabel_name = "Bowler"
title_name = " Wickets taken by Bowler"
sns.barplot( x = "wickets" , y = "bowler" , data = bowler_stats.head(10), color = "orange")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(bowler_stats.head(10)["wickets"].values):
ax.text(2, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_bowler_strike_rate(bowler_stats):
bowler_stats = bowler_stats.sort_values(by = "strike_rate",
ascending = True)
xlabel_name = "strike_rate"
ylabel_name = "Bowler"
title_name = " Bowler Strike Rate Distribution [ Balls / Wickets ]"
sns.barplot( x = "strike_rate" , y = "bowler" , data = bowler_stats.head(10), color = "maroon")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(bowler_stats.head(10)["strike_rate"].values):
ax.text(2, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_most_econ(bowler_stats):
bowler_stats = bowler_stats.sort_values(by = "econ",ascending = True)
xlabel_name = "econ"
ylabel_name = "Bowler"
title_name = " Bowler econ Distribution [ Runs / Over ]"
sns.barplot( x = "econ" , y = "bowler" , data = bowler_stats.head(10), color = "red")
draw_plot(xlabel_name = xlabel_name ,
ylabel_name = ylabel_name,
title_name = title_name)
fig = plt.gcf()
ax = fig.gca()
for i , v in enumerate(bowler_stats.head(10)["econ"].values):
ax.text(2, i, v,fontsize=16,color='white',weight='bold')
st.pyplot(fig)
def display_bowler_ranking(bowler_stats):
st.markdown("<hr/>",unsafe_allow_html= True)
title_page = "## The ranking of the best IPL " + str(ipl_season)+ " bowler "
st.markdown(title_page)
st.markdown("<hr/>",unsafe_allow_html= True)
bowler_stats = bowler_stats[bowler_stats["deliveries"] > bowler_stats["deliveries"].median()]
bowler_all_stats_4 = bowler_stats[['wickets', 'average','strike_rate', 'econ']]
bowler_all_stats_4 = bowler_all_stats_4.fillna(0)
bowler_all_stats_4.index =bowler_stats.bowler
from sklearn.preprocessing import scale
X = pd.DataFrame(scale(bowler_all_stats_4), index=bowler_all_stats_4.index, columns=bowler_all_stats_4.columns)
from sklearn.decomposition import PCA
# Fit the PCA model and transform X to get the principal components
pca = PCA()
df_plot = pd.DataFrame(pca.fit_transform(X), columns=['PC1','PC2','PC3','PC4'], index=X.index)
df_plot = df_plot.reset_index()
df_plot = df_plot.sort_values(by = 'PC1',ascending = True).head(15)
st.table(df_plot.head(10))
pca = PCA().fit(X)
plt.figure(figsize=(10,7))
plt.plot(np.cumsum(pca.explained_variance_ratio_), color='k', lw=2)
plt.xlabel('Number of components')
plt.ylabel('Total explained variance')
plt.axhline(0.95, c='r')
fig = plt.gcf()
st.pyplot(fig)
def display_bowling_stats(filename,ipl_season = ""):
bowler_stats = cricstats.get_bowler_stats(filename,ipl_season)
if strategy == "Best Average":
display_bowling_average(bowler_stats)
elif strategy == "Most Wickets":
display_most_wickets(bowler_stats)
elif strategy == "Best Strike Rate":
display_bowler_strike_rate(bowler_stats)
elif strategy == "Best Economy":
display_most_econ(bowler_stats)
elif strategy == "Bowler Ranking ( 4 dimensions )":
display_bowler_ranking(bowler_stats)
filename = "all_matches_IPL.csv"
if ipl_season == "2021":
filename = "ALL_2021_IPL_MATCHES_BALL_BY_BALL.csv"
display_category(filename,ipl_season)
display_bowling_stats(filename,ipl_season)