<jupyter_start><jupyter_text>Player Analysis<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings 
warnings.filterwarnings("ignore")


%matplotlib inline<jupyter_output><empty_output><jupyter_text>Data Cleaning and Exploration<jupyter_code>df1 = pd.read_csv("matches.csv")
df2 = pd.read_csv("deliveries.csv")

df1.rename(columns={"id" : 'match_id'}, inplace=True)

matches= pd.merge(df1, df2, on='match_id', how='outer')
matches.columns
matches = matches[['match_id', 'season','team1', 'team2', 'toss_winner','toss_decision','dl_applied','winner',
       'inning', 'batting_team', 'bowling_team', 'over', 'ball',
       'batsman','non_striker', 'bowler','wide_runs','bye_runs', 'legbye_runs','noball_runs','penalty_runs',
       'batsman_runs', 'extra_runs', 'total_runs', 'player_dismissed',
       'dismissal_kind']]

matches.shape<jupyter_output><empty_output><jupyter_text>Taking in conisderation SRH VS MI matches<jupyter_code>SRH_MI = matches[np.logical_or(np.logical_and(matches['team1']=='Sunrisers Hyderabad',matches['team2']=='Mumbai Indians'),
                            np.logical_and(matches['team2']=='Sunrisers Hyderabad',matches['team1']=='Mumbai Indians'))]
SRH_MI.match_id.nunique()
SRH_MI.season.unique()<jupyter_output><empty_output><jupyter_text>Stamina Score<jupyter_code>def stamina_score (player_name,opposition_team):
    
    #batsman runs
    player = matches[(matches["bowling_team"]==opposition_team)&(matches["batsman"]==player_name)]

    player_runs= player[player["wide_runs"]==0]

    batsman_runs = player_runs.groupby(["match_id"]).agg({"batsman_runs":"sum","ball":"count"})
    
    batsman_runs.columns = ["batsman_runs","total balls"]

    
    #player on crease
    crease = matches[(matches["batsman"]==player_name)|(matches["non_striker"]==player_name)]
    
    crease = crease[(crease["wide_runs"]==0)&(crease["noball_runs"]==0)]
    
    player_on_crease = pd.DataFrame(crease.groupby(["match_id"])["ball"].count())

    player_on_crease.columns = ["player_on_crease"]
    
    
    #boundaries runs
    player_boundaries = player_runs[(player_runs["batsman_runs"]==4)|(player_runs["batsman_runs"]==6)]

    boundary_runs = pd.DataFrame(player_boundaries.groupby(["match_id"])["batsman_runs"].sum())

    boundary_runs.columns = ["boundary runs"]
    
    #runs by 1,2 and 3
    player_running = player_runs[(player_runs["batsman_runs"]==1)|(player_runs["batsman_runs"]==2)|(player_runs["batsman_runs"]==3)]

    batsman_running = pd.DataFrame(player_running.groupby(["match_id"])["batsman_runs"].sum())

    batsman_running.columns = ["running runs"]
    
    
    #merging the data
    final = pd.merge(batsman_runs,player_on_crease,on="match_id",how ="inner")
    final = pd.merge(final,boundary_runs,on="match_id",how ="outer") 
    final = pd.merge(final,batsman_running,on="match_id",how ="outer")

    final["boundary runs"].fillna(0, inplace = True)
    final["running runs"].fillna(0, inplace = True)
    final["batsman_runs"].fillna(0, inplace = True)

    final = final[final["batsman_runs"]>15]
    
    final["Stamina Score"] = ((final["running runs"]/(final["boundary runs"]+1))+(final["total balls"]/final["player_on_crease"])) / final["batsman_runs"]
    
    
    stamina_player = sum(final['Stamina Score'])/len(final['Stamina Score'])
    return stamina_player<jupyter_output><empty_output><jupyter_text>- Batsman<jupyter_code>warner = round(stamina_score("DA Warner","Mumbai Indians"),2)

quinton = round(stamina_score("Q de Kock","Sunrisers Hyderabad"),2)

williamson = round(stamina_score("KS Williamson","Mumbai Indians"),2)

ishan = round(stamina_score("Ishan Kishan","Sunrisers Hyderabad"),2)

pollard = round(stamina_score("KA Pollard","Sunrisers Hyderabad"),2)

manish = round(stamina_score("MK Pandey","Mumbai Indians"),2)

saha = round(stamina_score("WP Saha","Mumbai Indians"),2)

surya = round(stamina_score("SA Yadav","Sunrisers Hyderabad"),2)

hardik = round(stamina_score("HH Pandya","Sunrisers Hyderabad"),2)

bairstow = round(stamina_score("J Bairstow","Mumbai Indians"),2)

rohit = round(stamina_score("RG Sharma","Sunrisers Hyderabad"),2)
batsman = [warner,quinton,williamson,ishan,pollard,manish,saha,surya,hardik,bairstow,rohit]

SS_batsman = pd.DataFrame(batsman)

SS_batsman.columns = ["Stamina Score"]

SS_batsman<jupyter_output><empty_output><jupyter_text>Final SS - Warner<jupyter_code>final_warner = (warner - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Warner:",round(final_warner,2))<jupyter_output>SS for Warner: 0.18<jupyter_text>- <NAME><jupyter_code>final_quinton = (quinton - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for QDK:",round(final_quinton,2))<jupyter_output>SS for QDK: 0.09<jupyter_text>- Williamson<jupyter_code>final_kane = (williamson - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Williamson:",round(final_kane,2))<jupyter_output>SS for Williamson: 0.09<jupyter_text>- <NAME><jupyter_code>final_kishan = (ishan - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Ishan :",round(final_kishan,2))<jupyter_output>SS for Ishan : 0.27<jupyter_text>- Pollard<jupyter_code>final_pollard = (pollard - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Pollard:",round(final_pollard,2))<jupyter_output>SS for Pollard: 0.0<jupyter_text>- <NAME><jupyter_code>final_manish = (manish - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Manish:",round(final_manish,2))<jupyter_output>SS for Manish: 1.0<jupyter_text>- Saha<jupyter_code>final_saha = (saha - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Saha:",round(final_saha,2))<jupyter_output>SS for Saha: 0.09<jupyter_text>- <NAME><jupyter_code>final_surya = (surya - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for S Yadav:",round(final_surya,2))<jupyter_output>SS for S Yadav: 0.45<jupyter_text>- <NAME><jupyter_code>final_hardik = (hardik - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Hardik:",round(final_hardik,2))<jupyter_output>SS for Hardik: 0.18<jupyter_text>- Bairstow<jupyter_code>final_bairstow = (bairstow - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Bairstow:",round(final_bairstow,2))<jupyter_output>SS for Bairstow: 0.27<jupyter_text>- <NAME><jupyter_code>final_rohit = (rohit - min(SS_batsman["Stamina Score"])) / (max(SS_batsman["Stamina Score"]) - min(SS_batsman["Stamina Score"]))
print("SS for Rohit:",round(final_rohit,2))<jupyter_output>SS for Rohit: 0.27<jupyter_text>So from all the above Stamina score that we have calculated for varius players, we can predict that <NAME> and Manish Pandey can have higher stamina score   Smash rate<jupyter_code>def smash_rate(player_name, opposition_team):
    
    #batsman runs and balls faced
    player = matches[(matches["bowling_team"]==opposition_team)&(matches["batsman"]==player_name)]

    player = player[player["wide_runs"]==0]

    batsman_runs = player.groupby(["match_id"]).agg({"batsman_runs":"sum","ball":"count"})
    
    #batsman boundaries count
    player_boundaries = player[(player["batsman_runs"]==4)|(player["batsman_runs"]==6)]

    boundary_runs = pd.DataFrame(player_boundaries.groupby(["match_id"])["batsman_runs"].count())

    boundary_runs.columns = ["boundary count"]
    
    #merging the data together
    final = pd.merge(batsman_runs,boundary_runs, on = "match_id", how = "outer")
    
    final["boundary count"].fillna(0, inplace = True)
    
    final["batsman_runs"].fillna(0, inplace = True)
    
    #smash rate
    final["smash_rate"] = (final["boundary count"]/final ["ball"])*100
    print(final)
    print(" ")
    
    #visualization
    plt.figure(figsize = (25,18))
    plt.subplot(3,3,1)
    sns.kdeplot(final["smash_rate"])
    plt.subplot(3,3,2)
    sns.boxplot(final["smash_rate"])

    smash_player = sum(final["smash_rate"])/len(final["smash_rate"])
    print("Average Smash Rate :",round(smash_player,2))<jupyter_output><empty_output><jupyter_text>- <NAME><jupyter_code>smash_rate("<NAME>","Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
52                  61    40             9.0   22.500000
7900                13     9             3.0   33.333333
7916                 0     3             0.0    0.000000
11310               17    21             2.0    9.523810
 
Average Smash Rate : 16.34<jupyter_text>- <NAME><jupyter_code>smash_rate("<NAME>","Mumbai Indians")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
10                  49    34             9.0   26.470588
47                   6     6             1.0   16.666667
150                 21    30             1.0    3.333333
170                 15     9             2.0   22.222222
221                 31    15             6.0   40.000000
238                  1     3             0.0    0.000000
282                  1     2             0.0    0.000000
390                 61    37             9.0   24.324324
408                  7     5             1.0   20.000000
477                 65    51             5.0    9.803922
493                 55    31             8.0   25.806452
541                  9     7             2.0   28.571429
572                  6     5             1.0   20.000000
588                 90    59            11.0   18.644068
613                 48    33             8.0   24.242424
11310               16    13   [...]<jupyter_text>- Pollard<jupyter_code>smash_rate("KA Pollard","Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
10                  11    11             1.0    9.090909
47                   5     9             0.0    0.000000
423                 14    19             1.0    5.263158
444                 66    27            10.0   37.037037
477                 78    48             9.0   18.750000
493                  6     7             0.0    0.000000
541                 33    24             4.0   16.666667
613                 11    13             2.0   15.384615
7900                28    23             5.0   21.739130
7916                 9     6             1.0   16.666667
11310               47    26             6.0   23.076923
11342               12    10             1.0   10.000000
 
Average Smash Rate : 14.47<jupyter_text>- <NAME><jupyter_code>smash_rate('MK Pandey',"Mumbai Indians")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
7                   81    47            10.0   21.276596
53                  33    33             2.0    6.060606
188                 40    27             5.0   18.518519
226                 16    18             2.0   11.111111
231                  5     9             0.0    0.000000
277                 59    47             7.0   14.893617
310                  0     5             0.0    0.000000
439                 29    29             2.0    6.896552
458                 64    53             8.0   15.094340
497                 14    21             0.0    0.000000
518                 40    24             5.0   20.833333
567                  1     1             0.0    0.000000
581                 52    29             6.0   20.689655
7900                11     8             1.0   12.500000
7916                16    11             3.0   27.272727
11310               16    21   [...]<jupyter_text>- <NAME><jupyter_code>smash_rate('Q de Kock',"Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
469                 48    30             7.0   23.333333
489                  7    12             0.0    0.000000
561                 50    31            10.0   32.258065
618                 44    31             7.0   22.580645
628                  2     9             0.0    0.000000
11310               19    18             2.0   11.111111
11342               75    58             8.0   13.793103
 
Average Smash Rate : 14.73<jupyter_text>- <NAME><jupyter_code>smash_rate("HH Pandya","Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
10                   2     3             0.0    0.000000
47                  15    24             0.0    0.000000
588                  2     5             0.0    0.000000
613                  7    16             0.0    0.000000
7916                 4    19             0.0    0.000000
11310               14    14             1.0    7.142857
11342               25    12             3.0   25.000000
 
Average Smash Rate : 4.59<jupyter_text>- <NAME><jupyter_code>smash_rate("SA Yadav","Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
14                   4     3             1.0   33.333333
511                 12     5             2.0   40.000000
537                  2     2             0.0    0.000000
555                  6     3             1.0   33.333333
631                  6     3             1.0   33.333333
634                 23    15             2.0   13.333333
 
Average Smash Rate : 25.56<jupyter_text>- <NAME><jupyter_code>smash_rate("WP Saha","Mumbai Indians")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
22                  11    15             0.0    0.000000
50                  93    55            14.0   25.454545
99                   1     6             0.0    0.000000
138                  8     4             1.0   25.000000
479                 59    47             7.0   14.893617
505                  3     8             0.0    0.000000
552                 12    12             1.0    8.333333
619                 56    40             7.0   17.500000
7900                22    20             3.0   15.000000
7916                 0     2             0.0    0.000000
11342               25    15             5.0   33.333333
 
Average Smash Rate : 12.68<jupyter_text>- <NAME><jupyter_code>smash_rate("KS Williamson","Mumbai Indians")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
613                  2     5             0.0    0.000000
7900                 6     4             1.0   25.000000
7916                29    21             5.0   23.809524
11342                3     7             0.0    0.000000
 
Average Smash Rate : 12.2<jupyter_text>- <NAME><jupyter_code>smash_rate("RG Sharma","Sunrisers Hyderabad")<jupyter_output>batsman_runs  ball  boundary count  smash_rate
match_id                                                
10                   4     4             0.0    0.000000
47                  67    45             8.0   17.777778
423                 22    22             2.0    9.090909
444                 20    15             2.0   13.333333
477                  1     5             0.0    0.000000
493                 14     6             3.0   50.000000
541                 24    15             3.0   20.000000
572                  7     2             1.0   50.000000
588                  5     8             0.0    0.000000
613                  5     3             1.0   33.333333
7900                11    10             2.0   20.000000
7916                 2     6             0.0    0.000000
11310               11    14             1.0    7.142857
11342               24    18             5.0   27.777778
 
Average Smash Rate : 17.75<jupyter_text>From all the above smash rate that we have calculated of varius players we can predict that anyone of <NAME> , Warner are expected to have higher smash rate in this match.  Boundary Leaker<jupyter_code>def Boundary_Leaker(player_name,opposition_team):
    
    player = matches[(matches["batting_team"] == opposition_team)&(matches["bowler"]==player_name)]
    
    player = player[(player["wide_runs"]==0)&(player["noball_runs"]==0)]
    
    #boundary count
    player_bound = player[(player["batsman_runs"]==4)|(player["batsman_runs"]==6)]
    
    bound_count = pd.DataFrame(player_bound.groupby(["match_id"])["batsman_runs"].count())

    bound_count.columns = ["boundary count"]
    
    # total balls bowled
    total_balls = pd.DataFrame(player.groupby(["match_id"])["ball"].count())
    
    #merging dataframes
    final = pd.merge(total_balls,bound_count,on = "match_id",how = "outer")
    
    final["boundary count"].fillna(0, inplace = True)
    
    BL =  1 / (final["ball"]/final["boundary count"]+1)
    
    BL_player = sum(BL)/len(BL)
    print("Average BL of the bowler:",round(BL_player,2))<jupyter_output><empty_output><jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("JJ Bumrah","Sunrisers Hyderabad")<jupyter_output>Average BL of the bowler: 0.11<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("TA Boult","Sunrisers Hyderabad")<jupyter_output>Average BL of the bowler: 0.17<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("Rashid Khan","Mumbai Indians")<jupyter_output>Average BL of the bowler: 0.08<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("RD Chahar","Sunrisers Hyderabad")<jupyter_output>Average BL of the bowler: 0.09<jupyter_text>-  Pollard<jupyter_code>Boundary_Leaker("KA Pollard","Sunrisers Hyderabad")<jupyter_output>Average BL of the bowler: 0.15<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("<NAME>","Mumbai Indians")<jupyter_output>Average BL of the bowler: 0.14<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("<NAME>","Mumbai Indians")<jupyter_output>Average BL of the bowler: 0.23<jupyter_text>- <NAME><jupyter_code>Boundary_Leaker("KH Pandya","Sunrisers Hyderabad")<jupyter_output>Average BL of the bowler: 0.04<jupyter_text>From all the above BL score of varius bowlers we can predict that Rashid Khan is expected to have best BL score.  Total wicket fall in the match - Overall<jupyter_code>wickets = pd.DataFrame(SRH_MI.groupby(['match_id'])["player_dismissed"].count())

wickets.columns = ["wickets"]

print("Wicket fall per match in SRH VS MI matches :")

wickets
(wickets["wickets"]>10).value_counts()
wickets.describe().T
sns.kdeplot(wickets['wickets'])
sns.boxplot(wickets['wickets'])
print("Average wicket fall in SRH VS MI matches  :{}".format(round(wickets["wickets"].mean())))
plt.figure(figsize=(10,7))
wickets["Interval"] = pd.cut(wickets["wickets"],[0,6,10,15,20])
sns.countplot(wickets["Interval"])
plt.title("Dot Balls count")<jupyter_output><empty_output><jupyter_text>Inning-wise - First Inning<jupyter_code>first = SRH_MI[SRH_MI["inning"]==1]
first_innings = pd.DataFrame(first.groupby(["match_id","batting_team"])["player_dismissed"].count())

first_innings.columns =["first innings wickets"]

first_innings
first_innings.describe().T
sns.kdeplot(first_innings["first innings wickets"])
sns.boxplot(first_innings["first innings wickets"])
print("Average wicket fall in SRH VS MI matches in 1st innings :{}".format(round(first_innings["first innings wickets"].mean())))
plt.figure(figsize=(10,7))
first_innings["Interval"] = pd.cut(first_innings["first innings wickets"],[0,5,10])
sns.countplot(first_innings["Interval"])
plt.title("Wicket fall count")<jupyter_output><empty_output><jupyter_text>- 2nd innings<jupyter_code>second = SRH_MI[SRH_MI["inning"]==2]
second_innings = pd.DataFrame(second.groupby(["match_id","batting_team"])["player_dismissed"].count())

second_innings.columns =["second innings wickets"]

second_innings
second_innings.describe().T
sns.kdeplot(second_innings["second innings wickets"])
sns.boxplot(second_innings["second innings wickets"])
print("Average wicket fall in SRH VS MI matches in 2nd innings :{}".format(round(second_innings["second innings wickets"].mean())))
plt.figure(figsize=(10,7))
second_innings["Interval"] = pd.cut(second_innings["second innings wickets"],[0,5,10])
sns.countplot(second_innings["Interval"])
plt.title("Wicket fall count")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12112145924817835, 'fraction_numerical': 0.08391642136756261, 'mean_word_length': 2.3555699481865284, 'pattern_counts': {'":': 4, '<': 156, '<?xml version=': 0, '>': 158, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 146, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '39', 'dir': 'jupyter-structured-clean-dedup', 'id': '36883', 'license': 'MIT', 'n_tokens_mistral': 8129, 'n_tokens_neox': 7038, 'n_words': 1324, 'path': 'SRH VS MI/Player Analysis SRH VS MI.ipynb', 'repo_name': 'tacklesta/WPL', 'size': '20894'}