<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

%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','winner',
       'inning', 'batting_team', 'bowling_team', 'over', 'ball',
       'batsman','non_striker', 'bowler','wide_runs','bye_runs', 'legbye_runs','noball_runs',
       'batsman_runs', 'extra_runs', 'total_runs', 'player_dismissed',
       'dismissal_kind']]

matches.shape
matches.head()<jupyter_output><empty_output><jupyter_text>Replacing DD with DC<jupyter_code>dicti = {"Delhi Daredevils":"Delhi Capitals"}
matches["team1"]=matches["team1"].replace(dicti)
matches["team2"]=matches["team2"].replace(dicti)
matches["toss_winner"]=matches["toss_winner"].replace(dicti)
matches["batting_team"]=matches["batting_team"].replace(dicti)
matches["bowling_team"]=matches["bowling_team"].replace(dicti)
matches["winner"]=matches["winner"].replace(dicti)<jupyter_output><empty_output><jupyter_text>Taking in Consideration only DC VS KXIP matches<jupyter_code>KXIP_DC=matches[np.logical_or(np.logical_and(matches['team1']=='Delhi Capitals',matches['team2']=='Kings XI Punjab'),
                            np.logical_and(matches['team2']=='Delhi Capitals',matches['team1']=='Kings XI Punjab'))]

KXIP_DC.head()
KXIP_DC.match_id.nunique()<jupyter_output><empty_output><jupyter_text>KL Rahul's strike rate in first 6 overs<jupyter_code>rahul = KXIP_DC[(KXIP_DC["batting_team"]=="Kings XI Punjab") & (KXIP_DC["batsman"]=="KL Rahul")]
rahul = rahul[rahul["over"]<=6]
rahul = rahul[rahul["wide_runs"]==0]


rahul.iloc[30:,4:].head()
rahul.over.unique()
rahul.season.unique()
rahul.match_id.unique()
strike =rahul.groupby(["season",'match_id','batsman']).agg({"batsman_runs":"sum","ball":"count"})

print("Runs scored by KL Rahul against DC :")

strike
strike_rate = pd.DataFrame((strike['batsman_runs']/strike["ball"]) * 100)

strike_rate.columns = ["strike rate"]

print("Strike Rate of KL Rahul while batting against DC :")

strike_rate
sns.distplot(strike_rate["strike rate"])
print("Average strike rate of Rahul against DC :",round(strike_rate['strike rate'].mean()))<jupyter_output>Average strike rate of Rahul against DC : 185<jupyter_text>- Overall<jupyter_code>rahul_overall = matches[(matches["batting_team"]=="Kings XI Punjab") & (matches["batsman"]=="KL Rahul")]
rahul_overall = rahul_overall[rahul_overall["over"]<=6]
rahul_overall = rahul_overall[rahul_overall["wide_runs"]==0]


rahul_overall.iloc[30:,4:].head()
rahul_overall.match_id.nunique()
rahul_overall.over.unique()
strike_overall =rahul_overall.groupby(["season",'match_id','batsman']).agg({"batsman_runs":"sum","ball":"count"})

print("Runs scored by KL Rahul :")

strike_overall
strike_rate_overall = pd.DataFrame((strike_overall['batsman_runs']/strike_overall["ball"]) * 100)

strike_rate_overall.columns = ["strike rate"]

print("Strike Rate of KL Rahul (overall) :")

strike_rate_overall
sns.distplot(strike_rate_overall["strike rate"])
print("Average strike rate of Rahul :",round(strike_rate_overall['strike rate'].mean()))<jupyter_output>Average strike rate of Rahul : 134<jupyter_text>Confidence Interval<jupyter_code>strike_rate_overall.describe().T
mean = strike_rate_overall['strike rate'].mean()
sd = strike_rate_overall['strike rate'].std()
n = len(strike_rate_overall['strike rate'])

n
tstar = 2.064
se = sd/np.sqrt(n)
se
lcb = mean - tstar * se
ucb = mean + tstar * se
lcb = round(lcb)
ucb = round(ucb)

print("95% Confidence Interval for the strike rate with which Rahul is expected to score today  :{}".format((lcb, ucb)))<jupyter_output>95% Confidence Interval for the strike rate with which Rahul is expected to score today  :(106.0, 161.0)<jupyter_text>Iyer 's contribuion to DC's total score - Against KXIP<jupyter_code>Delhi = KXIP_DC[KXIP_DC["batting_team"]=='Delhi Capitals']
total_runs =pd.DataFrame(Delhi.groupby(["match_id"])["total_runs"].sum())

total_runs.reset_index(level=0, inplace=True)

total_runs
iyer = Delhi[Delhi["batsman"]=="<NAME>"]
iyer = iyer[iyer["wide_runs"]==0]
iyer_score = pd.DataFrame(iyer.groupby(["match_id"])["batsman_runs"].sum())

iyer_score.columns = ["Iyer's runs"]

iyer_score.reset_index(level=0, inplace=True)

iyer_score
final = pd.merge(iyer_score, total_runs, on='match_id', how='inner')

final
final["contribution"] = round((final["Iyer's runs"]/final["total_runs"]) * 100)

final
sns.distplot(final["contribution"])
print("Average contribution of Iyer in DC's inning against KXIP :{}%".format(round(final["contribution"].mean())))<jupyter_output>Average contribution of Iyer in DC's inning against KXIP :19%<jupyter_text>- Overall<jupyter_code>Delhi_overall = matches[matches["batting_team"]=='Delhi Capitals']
cond1 = Delhi_overall["season"] == 2018
cond2 = Delhi_overall["season"] == 2019
recent_overall = Delhi_overall[cond1 | cond2]
recent_overall.head()
total_runs12 =pd.DataFrame(recent_overall.groupby(["match_id"])["total_runs"].sum())

total_runs12.reset_index(level=0, inplace=True)

total_runs12
iyer_overall = recent_overall[recent_overall["batsman"]=="SS Iyer"]
iyer_overall = iyer_overall[iyer_overall["wide_runs"]==0]
iyer_score_overall = pd.DataFrame(iyer_overall.groupby(["match_id"])["batsman_runs"].sum())

iyer_score_overall.columns = ["Iyer's runs"]

iyer_score_overall.reset_index(level=0, inplace=True)

iyer_score_overall
final_overall = pd.merge(total_runs12,iyer_score_overall , on='match_id', how='inner')

final_overall
final_overall["contribution"] = round((final_overall["Iyer's runs"]/final_overall["total_runs"]) * 100)

final_overall
sns.distplot(final_overall["contribution"])
sns.boxplot(final_overall["contribution"])
print("Average contribution of Iyer in DC's inning overall in recent years :{}%".format(round(final_overall["contribution"].mean())))<jupyter_output>Average contribution of Iyer in DC's inning overall in recent years :18%<jupyter_text>wickets KXIP will take excluding Shami's wicket<jupyter_code>punjab = KXIP_DC[KXIP_DC["bowling_team"]=="K<NAME> Punjab"]
punjab = punjab[(punjab["over"]>5) & (punjab["over"]<=20)]
punjab = punjab[punjab["season"]==2019]

punjab.iloc[:,2:].head()
except_shami = punjab[punjab["bowler"]!='<NAME>']
except_shami = except_shami[except_shami["dismissal_kind"] !="run out"]
except_shami.match_id.nunique()
punjab.bowler.nunique()
except_shami.bowler.nunique()
wickets = pd.DataFrame(except_shami.groupby(["season","match_id"])["player_dismissed"].count())

wickets.columns = ['except Shami']

wickets
shami = punjab[punjab["bowler"]=="<NAME>"]
shami = shami[shami["dismissal_kind"] !="run out"]

wickets["shami_wicket"] = shami.groupby(["season","match_id"])["dismissal_kind"].count()

wickets
print("Average contribution of bowlers of KXIP (except Shami) in 6-20th over :{} wickets".format(round(wickets["except Shami"].mean())))<jupyter_output>Average contribution of bowlers of KXIP (except Shami) in 6-20th over :4 wickets<jupyter_text>We only have two matches of KXIP when Shami is also in playing XI so this might not help us in predicting wickets .So to have an better idea of posiblity of wickets that other bowlers can take except Shami , we need to analyse their individual performances against DC batsman in recent years. Since, Delhi's team playing XI has completely changed in recent 2-3 years . So it might be not usefull to analyse their complete overall performance that's why considering only last 3 year's performance<jupyter_code>abcd = matches[matches["batting_team"]=="Delhi Capitals"]
abcd = abcd[(abcd["season"]==2017) | (abcd["season"]==2018) | (abcd["season"]==2019)]<jupyter_output><empty_output><jupyter_text>- <NAME><jupyter_code>maxwell = abcd[(abcd["bowler"]=="<NAME>")]
maxwell = maxwell[maxwell["dismissal_kind"] !="run out"]

maxwell.head()
maxwell.bowler.unique()
maxwell.match_id.nunique()
Max = pd.DataFrame(maxwell.groupby("match_id")["dismissal_kind"].count())
Max.columns = ["wickets taken"]

Max<jupyter_output><empty_output><jupyter_text>- <NAME><jupyter_code>ashwin = abcd[(abcd["bowler"]=="<NAME>")]
ashwin = ashwin[ashwin["dismissal_kind"] !="run out"]

ashwin.head()
ashwin.bowler.unique()
ashwin.match_id.nunique()
ash = pd.DataFrame(ashwin.groupby("match_id")["dismissal_kind"].count())
ash.columns = ["wickets taken"]

ash<jupyter_output><empty_output><jupyter_text>So taking in consideration all the information regarding bowlers of KXIP which have played IPL in past recent years also (against DC) , we can se that on an average generally 4-5 wickets out of total wickets are taken against DC batsman by bowlers other then Shami.  Gayle Contribution Since Gayle is a part of KXIP from last year only , so considering his performance of last 4 years to get an better idea.<jupyter_code>gayle = matches[matches["batsman"]=="<NAME>"]
gayle = gayle[(gayle["season"]==2016) | (gayle["season"]==2017) | (gayle["season"]==2018) | (gayle["season"]==2019)]
gayle = gayle[gayle["wide_runs"]==0]

gayle.head()
gayle.match_id.nunique()
strike_rate = gayle.groupby(["season","match_id","batsman"]).agg({ "batsman_runs":"sum","ball" : 'count'})

print("Runs scored by Gayle overall :")

strike_rate.head()
strike_rate["strike rate"] = pd.DataFrame(strike['batsman_runs']/strike["ball"] * 100)

print("Strike Rate of Gayle while batting :")

strike_rate
sns.distplot(strike_rate['strike rate'])
 sns.boxplot(strike_rate['strike rate'])
print("On an average strike rate of Gayle :",round(strike_rate['strike rate'].mean()))<jupyter_output>On an average strike rate of Gayle : 116<jupyter_text>Confidence Interval<jupyter_code>strike_rate.describe().iloc[:,2:].T
mean = strike_rate['strike rate'].mean()
sd = strike_rate['strike rate'].std()
n = len(strike_rate['strike rate'])

n
tstar = 2.064
se = sd/np.sqrt(n)
se
lcb = mean - tstar * se
ucb = mean + tstar * se
lcb = round(lcb)
ucb = round(ucb)

print("95% Confidence Interval for the strike rate with which Gayle can score today against DC :{}".format((lcb, ucb)))<jupyter_output>95% Confidence Interval for the strike rate with which Gayle can score today against DC :(96.0, 136.0)<jupyter_text>- Gayle Dismissal<jupyter_code>count = gayle.match_id.nunique()
dismiss = pd.DataFrame(gayle.groupby("match_id")["dismissal_kind"].count())

total = dismiss["dismissal_kind"].sum()
gayle_dismissal_percent = total/count

print("Out of last {} matches Gayle has been dismissed in {} innings".format(count , total))
print("Dismissal percentage of Gayle getting out is {}%".format(round(gayle_dismissal_percent *100)))<jupyter_output>Out of last 43 matches Gayle has been dismissed in 39 innings
Dismissal percentage of Gayle getting out is 91.0%
{'fraction_non_alphanumeric': 0.15677348871085214, 'fraction_numerical': 0.015659140568099052, 'mean_word_length': 8.138935108153078, 'pattern_counts': {'":': 6, '<': 68, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '508721', 'license': 'MIT', 'n_tokens_mistral': 4225, 'n_tokens_neox': 3982, 'n_words': 793, 'path': 'KXIP VS DC/Player Analysis KXIP VS DC.ipynb', 'repo_name': 'tacklesta/WPL', 'size': '11018'}