"""
### IPL Player Performance
"""
"""
we are attempting to predict the ipl player performance for the year 2020 using previous year player performances
"""
"""
Importing header files
"""
import pandas as pd# data processing, CSV file I/O (e.g. pd.read_csv)
import re
import numpy as np
data = pd.read_csv('D:\ka\\training.csv')
data.head()
data_match = pd.read_csv('D:\ka\\Matches IPL 2008-2019.csv')
data_match.head()
"""
We have the player and match details but we dont have detail on which team the player was on for every year, we can use 2020 squad for now to get the team details
"""
squad = pd.read_csv('D:\ka\\IPL 2020 Squads.csv')
"""
had to resave the csv file again in utf-8 format
"""
squad
line = "Royal Challengers Bangalore"
words = line.split()
letters = [word[0] for word in words]
print("".join(letters))
data_match1=data_match.head()
team = data_match1[['team1']]
#print(team)
columns = list(data_match1) 
  
for i in columns: 
  
    # printing the third element of the column 
    print (data_match1[i][1]) 
data_match.head()
team1_short = []
for index, row in data_match.iterrows():
    line = (row['team1'])
    words = line.split()
    letters = [word[0] for word in words]
    fw="".join(letters)
    team1_short.append(fw)
data_match['team1_short'] = team1_short
team2_short = []
for index, row in data_match.iterrows():
    line = (row['team2'])
    words = line.split()
    letters = [word[0] for word in words]
    fw="".join(letters)
    team2_short.append(fw)
data_match['team2_short'] = team2_short
data_match.head()
import re #for regex expressions
str= "1_Bipul Sharma"
re.split('_',str)
player_name = []
match_id = []
for index, row in data.iterrows():
    line = (row['Id'])
    a = re.split('_',line)
    match_id.append(int(a[0]))
    player_name.append(a[1])
data['player_name'] = player_name
data['match_id'] = match_id
data.head()
new_df1= pd.merge(data,data_match[['match_id','team1_short','team2_short','season']])
new_df1.head()
"""
removing details such as captian from name which is not necessary for player score and the two dataframes can be merged
"""
squad=squad.rename(columns={"Player_name": "player_name"})
player_name=[]
squad.head()
for index, row in squad.iterrows():
    line = (row['player_name'])
    a = re.split('�',line)
    player_name.append(a[0])
squad['player_name'] = player_name
squad.head()
#find most common team, this can be used to find which team was the player playing for in each season
from collections import Counter
  
def most_common(test_list1):
    print(test_list1)
    test_list1 = Counter(test_list1) 
    res = test_list1.most_common()[0][0]
    freq = test_list1.most_common()[0][1]
    return freq,res
"""
Splitting Player Name into first Name and last name
"""
player_lname=[]
player_fname=[]
new_df1.head()
for index, row in new_df1.iterrows():
    line = (row['player_name'])
    #print(line)
    ele = re.split(' ',line)
    a=ele[-1]
    b=ele[0]
    #print(a,b)
    #b=ele[0]
    player_lname.append(a)
    player_fname.append(b)
new_df1['lname'] = player_lname
new_df1['fname'] = player_fname
new_df1.head()
"""
#### Finding in which team each player has been playing for the previous years, so that we can map player performance against each team which will enable us to predict better
"""
"""
to find that we are getting the match details of each game player has participated and the most common team(mode) will be the team the player will be playing for
"""
season=[]
player=[]
team=[]
for i in squad['player_name']:
    nname=i #saving name for saving to list later
    filter1=new_df1[new_df1.player_name.isin([i])]
    if(filter1.empty):
        nname=i.split()
        nname=nname[1]
        filter1=new_df1[new_df1.lname.isin([nname])]
    for j in range(2008,2020):
        #print(i,j)
        filter2=filter1[filter1.season.isin([j])]
        if(filter2.empty):
            continue
        else:
            ipdf=filter2['team1_short'].values.tolist()
            ipdf.extend(filter2['team2_short'].values.tolist())
            freq,res=most_common(ipdf)# the mode team and its frequency
            #print(filter2['team1_short'].head)
            print(filter2['team1_short'].values.tolist())
            #print(freq,res)
            season.append(j)
            player.append(i)
            print("Team",res)
            team.append(res)
new_player_team_details = pd.DataFrame()
new_player_team_details['season']  = season
new_player_team_details['p_team']  = team
new_player_team_details['player_name']  = player
new_player_team_details
new_df1= pd.merge(new_df1,new_player_team_details[['season','player_name','p_team']])

new_df1.head()
"""
since we now have data on which team the player is playing for we can get which team the player is playing against so that we can get the layer performace against every team
"""
p_against=[]
for index, row in new_df1.iterrows():
    p_team = (row['p_team'])
    team1= (row['team1_short'])
    team2=(row['team2_short'])
    if team1==p_team:
        p_against.append(team2)
    else:
        p_against.append(team1)
new_df1['p_against'] = p_against
new_df1.head()
season1 = squad
season1.head()
season1['player_code'] = len(season1) - pd.Categorical(season1.player_name, ordered=True).codes
new_df1= pd.merge(new_df1,season1[['player_name','player_code']])
new_df1.head()
"""
since we are using regression we need to convert the strings to numbers. For that we are creating codes for each team
"""
p_against
new_df1['p_against_code'] = 30 - pd.Categorical(new_df1.p_against, ordered=True).codes
"""
Saving file for future use
"""
new_df1.to_csv (r'D:\ka\\customtraining.csv', index = False, header=True)
"""
### Trying XGboost
"""
!pip install xgboost
from numpy import loadtxt
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# load data
dataset = pd.read_csv('D:\ka\\customtraining.csv')
# split data into X and y
Y = dataset['Total Points']
X = dataset[['player_code','p_against_code']]
# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# fit model no training data
model = XGBClassifier()
model.fit(X_train, y_train)
# make predictions for test data
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
"""
### Linear Regression
"""
from sklearn import linear_model
X = dataset[['player_code','p_against_code']]
y = dataset['Total Points']
# split data into train and test sets
seed = 7
test_size = 0.05
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
lm = linear_model.LinearRegression()
model = lm.fit(X_train,y_train) #running with entire dataset
y_pred = model.predict(X_test)
"""
#### RMSE
"""
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, y_pred,squared=False)
We are getting an RMSE value of 25.8 with 95% train and 5% test value using simple linear regression
"""
## Logistic Regression
"""
from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(X_train)
X_scaled = scaler.transform(X_train)
scaler1 = preprocessing.StandardScaler().fit(X_train)
X_testscaled = scaler.transform(X_test)

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(random_state=0).fit(X_scaled,y_train)
y_predl=clf.predict(X_testscaled)
mean_squared_error(y_test, y_predl,squared=False)
"""
## Working with final file
"""
submission= pd.read_csv("D:\ka\\sample_submission.csv")
dataset = pd.read_csv('D:\ka\\customtraining.csv')
submission.head()
"""
splitting into ID and Player Name
"""
player_name = []
match_id = []
for index, row in submission.iterrows():
    line = (row['Id'])
    a = re.split('_',line)
    match_id.append(int(a[0]))
    player_name.append(a[1])
submission['player_name'] = player_name
submission['match_id'] = match_id
submission.head()
data_match2020 = pd.read_csv('D:\ka\\Matches IPL 2020.csv')
data_match2020.head()
player_lname=[]
player_fname=[]
submission.head()
for index, row in submission.iterrows():
    line = (row['player_name'])
    #print(line)
    ele = re.split(' ',line)
    a=ele[-1]
    b=ele[0]
    #print(a,b)
    #b=ele[0]
    player_lname.append(a)
    player_fname.append(b)
submission['lname'] = player_lname
submission['fname'] = player_fname
submission.head()
squad=squad.rename(columns={"Player_name": "player_name"})
squad['player_code'] = len(squad) - pd.Categorical(squad.player_name, ordered=True).codes
player_lname=[]
player_fname=[]
squad.head()
for index, row in squad.iterrows():
    line = (row['player_name'])
    #print(line)
    ele = re.split(' ',line)
    a=ele[-1]
    b=ele[0]
    #print(a,b)
    #b=ele[0]
    player_lname.append(a)
    player_fname.append(b)
squad['lname'] = player_lname
squad['fname'] = player_fname
squad.head()
player_name=[]
squad.head()
for index, row in squad.iterrows():
    line = (row['player_name'])
    a = re.split('�',line)
    player_name.append(a[0])
squad['player_name'] = player_name
    
submission= pd.merge(submission,data_match2020[['match_id','team1','team2']])
submission.head()
dataset.head()
squad=squad.rename(columns={"Player_name": "player_name"})
player_name=[]
for index, row in squad.iterrows():
    line = (row['player_name'])
    a = re.split('�',line)
    player_name.append(a[0])
squad['player_name'] = player_name
squad.head()
type(play)
"""
### Working with different sheets 
"""
"""
there are some issues in the name of player in squad and final_submission which makes it hard to merge files, we are merging files in 3 steps, first comaparing the player name, then if that fails comparing with last name only and even if that failes then doing manually
"""
player_name=[]
team=[]
failed_list=[]
player_id=[]
for i in submission['player_name']:
    filter1=squad[squad.player_name.isin([i])]
    if(filter1.empty):
        nname=i.split()
        fname=nname[0]
        if(len(nname)>=2):
            lname=nname[len(nname)-1]
        filter1=squad[squad.lname.isin([lname])]
        if(filter1.empty):
            continue
   # else:
    ipdf=filter1['Player_ipl_team'].values.tolist()
        #print(ipdf)
    if(len(ipdf)==1):
        team.append(ipdf[0])
        player_name.append(i)
        player_code=filter1['player_code'].values.tolist()
        player_id.append(player_code[0])
    else:
        filter2=filter1[filter1.fname.isin([fname])]
        ipdf=filter2['Player_ipl_team'].values.tolist()
            #print(ipdf)
        if(len(ipdf)==1):
            team.append(ipdf[0])
            player_name.append(i)
            player_code=filter1['player_code'].values.tolist()
            player_id.append(player_code[0])
        else:
            failed_list.append(i)
play = pd.DataFrame()                   
play['player_name'] = player_name
play['team'] = team
play['player_code']=player_id
#print(player_id)
play
play_drmvd = play.drop_duplicates()
failed_list=np.array(failed_list)
failed_list=np.unique(failed_list)
print(len(failed_list))
print(failed_list)

"""
Manually adding missing data
"""
squad['player_name'] = squad['player_name'].replace(['Axar Patel','Suryakumar Yadav','Ankit Sharma','Sheldon Cottrell','Deepak Chahar','Pavan Deshpande','Hardik Pandya','Harshal Patel','Harpreet Brar','Krunal Pandya','Karn Sharma','Josh Philippe','Prabhsimran Singh','Rinku Singh','Rahul Chahar','Rohit Sharma','Suryakumar Yadav','Sarfaraz Khan','Simran Singh','Umesh Yadav'],['AR Patel','AS Yadav','Ankit Sharma','Cottrell','DL Chahar','Deshpande',
 'HH Pandya','HV Patel','Harpreet Singh','KH Pandya','KV Sharma','Philippe','Prabhsimran Singh','R Singh','RD Chahar','RG Sharma','SA Yadav' ,'SN Khan' ,'Simran Singh', 'UT Yadav'])
submission1= pd.merge(submission,play_drmvd[['player_name','team','player_code']],on='player_name',how = 'left')
submission1.to_csv (r'D:\ka\\playerteamnew.csv', index = False, header=True)
submission1.head()
"""
to check whether all rows  is filled correctly
"""
"""
To find which team is playing against
"""
p_against=[]
for index, row in submission1.iterrows():
    p_team = (row['team'])
    team1= (row['team1'])
    team2=(row['team2'])
    if team1==p_team:
        p_against.append(team2)
    else:
        p_against.append(team1)
submission1['p_against'] = p_against
submission1.head()
submission1['p_against'] = submission1['p_against'].replace(['SRH'],'SH')
submission1['p_against'] = submission1['p_against'].replace(['KXIP'],'KXP')
dataset.head()
submission1.to_csv (r'D:\ka\\submissionv2.csv', index = False, header=True)
submission1=pd.read_csv('D:\ka\\submissionv2.csv')
submission1.head()
nds=dataset[['p_against','p_against_code']]
nds=nds.drop_duplicates()
submission1.head()
nds
submission1= pd.merge(submission1,nds[['p_against','p_against_code']],on='p_against',how = 'left')
submission1.head()
"""
submission1.to_csv (r'D:\ka\\submissionv3.csv', index = False, header=True)
"""
X_pred = submission1[['player_code','p_against_code']]
from sklearn import linear_model
X_train = dataset[['player_code','p_against_code']]
y_train = dataset['Total Points']
lm = linear_model.LinearRegression()
model = lm.fit(X_train,y_train) #running with entire dataset
y_pred = model.predict(X_pred)
"""
There are some missing values in the sheet so we are manually filling the data
"""
y_pred
len(play.player_name.unique())
play_drmvd.to_csv (r'D:\ka\\playersdd.csv', index = False, header=True)
df2=pd.read_csv('D:\ka\\csvplayer.csv')
p_names=df2['player_name']
p_names=p_names.drop_duplicates()
p_name=[]
p_team=[]
for i in p_names:
    n_f=df2[df2.player_name.isin([i])]
    ipdf=n_f['team1'].values.tolist()
    ipdf.extend(n_f['team2'].values.tolist())
    freq,res=most_common(ipdf)
    p_team.append(res)
    p_name.append(i)
n_p_d = pd.DataFrame()
n_p_d['player_name']  = p_name
n_p_d['team']  = p_team
n_p_d
dfextend= pd.merge(df2,n_p_d[['player_name','team']],on='player_name',how = 'left')
p_against=[]
for index, row in dfextend.iterrows():
    p_team = (row['team'])
    team1= (row['team1'])
    team2=(row['team2'])
    if team1==p_team:
        p_against.append(team2)
    else:
        p_against.append(team1)
dfextend['p_against'] = p_against
dfextend
#print(len(squad))
new_ids=len(squad)+len(n_p_d)
print(new_ids)
dfextend['player_code'] = new_ids - pd.Categorical(dfextend.player_name, ordered=True).codes
dfextend
dfextend['p_against'] = dfextend['p_against'].replace(['SRH'],'SH')
dfextend['p_against'] = dfextend['p_against'].replace(['KXIP'],'KXP')
nds=dataset[['p_against','p_against_code']]
nds=nds.drop_duplicates()
dfextend= pd.merge(dfextend,nds[['p_against','p_against_code']],on='p_against',how = 'left')
dfextend
dfextend.to_csv (r'D:\ka\\missingplayerdetails.csv', index = False, header=True)
dfextend_final= pd.merge(submission1,dfextend[['Id','player_code','p_against_code','team']],on='Id',how = 'left')
submission_final=submission1
for index, row in submission_final.iterrows():
    p_team = (row['player_code'])
    if(pd.isna(p_team)):
        id= (row['Id'])
        filter1=dfextend[dfextend.Id.isin([id])]
        team_val=filter1['team'].values.tolist()
        pcodeval=filter1['player_code'].values.tolist()
        t_against_val=filter1['p_against_code'].values.tolist()
        submission_final.at[index,'team'] = team_val[0]
        submission_final.at[index,'p_against_code'] = t_against_val[0]
        submission_final.at[index,'player_code'] = pcodeval[0]
submission_final.to_csv (r'D:\ka\\final.csv', index = False, header=True)
final_df=pd.read_csv('D:\ka\\final_submission.csv')
player_code=[]
p_against_code=[]
team=[]
for index, row in final_df.iterrows():
    id= (row['Id'])
    filter1=submission_final[submission_final.Id.isin([id])]
    team_val=filter1['team'].values.tolist()
    pcodeval=filter1['player_code'].values.tolist()
    t_against_val=filter1['p_against_code'].values.tolist()
    team.append(team_val[0])
    p_against_code.append(t_against_val[0])
    player_code.append(pcodeval[0])
final_df['team'] = team
final_df['player_code'] = player_code
final_df['p_against_code']=p_against_code
final_df.to_csv (r'D:\ka\\finalfinal.csv', index = False, header=True)
"""
## Final Prediction
"""
import pandas as pd
final_df=pd.read_csv('D:\ka\\finalfinal.csv')
X_pred = final_df[['p_against_code']]
from sklearn import linear_model
X_train = dataset[['p_against_code']]
y_train = dataset['Total Points']
lm = linear_model.LinearRegression()
model = lm.fit(X_train,y_train) #running with entire dataset
y_pred = model.predict(X_pred)
final_df['Total Points']=y_pred
final_df.to_csv (r'D:\ka\\finalsubmission2.csv', index = False, header=True)