# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns 
%matplotlib inline 
train_data = pd.read_csv("../input/ipl-2020-player-performance/Training.csv")
train_data
train_data['batting_points'] = train_data['Runs'] + train_data['Boundaries'] + 2 * train_data['Six'] + 8 * train_data['Fifty'] + 16 * train_data['Hundred'] - 2 * train_data['Duck']
train_data['bowling_points'] = 25 * train_data['Wickets'] + 8 * train_data['4W_Haul'] + 16 * train_data['5W_Haul'] + 8 * train_data['Maidens']
train_data['total'] = train_data['batting_points'] + train_data['bowling_points']
train_data
train_data['Player_name'] = train_data['Id']
train_data['match_number'] =  train_data['Id']
for p in range(0, len( train_data)):
    train_data['Player_name'][p] =  train_data['Id'][p].split("_")[-1]
    train_data['match_number'][p] = int( train_data['Id'][p].split('_')[:1][0])
train_data.head()
train_data.isnull().sum()
plt.figure(figsize=(12,6),dpi=200)
sns.heatmap(train_data.corr(),annot =True)
plt.figure(figsize = (12,10))
sns.scatterplot(x ='Batting_Points', y = 'Bowling_Points', data=train_data)
plt.figure(figsize=(10,5),dpi=150)
sns.scatterplot(data=train_data,y='Total Points',x='Player_name')
plt.xticks(rotation=45);
data = pd.DataFrame()
data['total_score'] = train_data['Total Points']
data['match_number']    = train_data['match_number']
data['Player_name']= train_data['Player_name']
data
##2020 matches
IPL_2020 =pd.read_csv('../input/ipl-2020-player-performance/Matches IPL 2020.csv')
 IPL_2020.head()
IPL_2020.isnull().sum()
IPL_2020.columns
sns.countplot(x='city',data =IPL_2020)
IPL_2008_2019 = pd.read_csv('../input/ipl-2020-player-performance/Matches IPL 2008-2019.csv')
IPL_2008_2019
IPL_2008_2019.isnull().sum()
IPL_2008_2019["city"].fillna("0", inplace = True) 

IPL_2008_2019["city"]
IPL_2008_2019['umpire3'].fillna('0',inplace=True)
IPL_2008_2019['umpire3']
IPL_2008_2019.isnull().sum()
IPL_2008_2019['umpire1'].fillna('0',inplace=True)
IPL_2008_2019['umpire2'].fillna('0',inplace=True)
IPL_2008_2019['winner'].fillna('0',inplace=True)
IPL_2008_2019['player_of_match'].fillna('0',inplace=True)
IPL_2008_2019
IPL_2008_2019.isnull().sum()
plt.figure(figsize=(10,5),dpi = 200)
plt.xticks(rotation=90)
sns.countplot(IPL_2008_2019['winner'])
plt.figure(figsize=(10,5),dpi = 200)
plt.xticks(rotation=90)
sns.countplot(IPL_2008_2019['toss_winner'])
IPL_2008_2019 = IPL_2008_2019[['match_id','city','date','winner']]
IPL_2008_2019.head()
"""
2020 ipl squads
"""
IPL_squad20 = pd.read_csv('../input/ipl-2020-player-performance/IPL 2020 Squads.csv',encoding= 'unicode_escape')
IPL_squad20
IPL_squad20['Player_name'].value_counts()
"""
sample_submission data
"""
sub_data=pd.read_csv('../input/ipl-2020-player-performance/sample_submission.csv')
sub_data
"""
1. 1. WE are getting 1283 rows 2 columns



PREPROCESS DATA
"""
data.head()
data.player_name.value_counts()
data.shape
data1 = data.drop(['Player_name'],axis=1)
data1
"""
PREDICTION
"""
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
Pred_ = train_data.groupby(['Id','Player_name'])['total'].sum().reset_index() 

Pred_ =Pred_.sort_values(by = 'total',ascending = False)
Pred_['Id'] = Pred_['Id'].astype('str')
Pred_['Id'] = Pred_['Id'] +'_'+ Pred_['Player_name']
Pred_
pred_l = train_data[['Id','Player_name','Total Points']]
pred_l
pred_l.to_csv('predict10_11.csv',index=False)
from math import sqrt
from sklearn.metrics import mean_squared_error

#calculate RMSE
rmse = sqrt(mean_squared_error(pred_l['Total Points'], Pred_['total'] )) 
rmse
pred_l.to_csv('predict10_11.csv',index=False)

pred_l