-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipl prediction system.py
203 lines (128 loc) · 5.82 KB
/
ipl prediction system.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
#!/usr/bin/env python
# coding: utf-8
# In[302]:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
# In[303]:
train_data = pd.read_csv('E:/BACKUP/users-admin files/Downloads/Training Matches IPL 2008-2020.csv')
train_data.head()
# In[304]:
train_data.isnull().sum()
# In[305]:
train_data['city'].fillna('Abu Dhabi',inplace=True)
train_data['winner'].fillna('Draw', inplace = True)
# In[306]:
#Both Rising Pue Supergiant and Rising Pune Supergiants represents same team similarly Delhi Capitals and Delhi Daredevils,
#Deccan Chargers and Sunrisers Hyderabad
train_data.replace("Rising Pune Supergiant","Rising Pune Supergiants", inplace=True)
train_data.replace('Deccan Chargers', 'Sunrisers Hyderabad', inplace=True)
train_data.replace('Delhi Daredevils', 'Delhi Capitals', inplace=True)
# In[307]:
plt.subplots(figsize = (15,5))
sns.countplot(x = 'season' , data = train_data, palette='dark')
plt.title('Total number of matches played in each season')
plt.show()
# In[308]:
plt.subplots(figsize=(15,10))
ax = train_data['winner'].value_counts().sort_values(ascending=True).plot.barh(width=.9,color=sns.color_palette("husl", 9))
ax.set_xlabel('count')
ax.set_ylabel('team')
plt.show()
# In[309]:
#Extracting the records where a team won batting first
batting_first=train_data[train_data['win_by_runs']!=0]
#Making a pie chart
plt.figure(figsize=(7,7))
plt.pie(list(batting_first['winner'].value_counts()),labels=list(batting_first['winner'].value_counts().keys()),autopct='%0.1f%%')
plt.show()
# In[310]:
#extracting those records where a team has won after batting second
batting_second=train_data[train_data['win_by_wickets']!=0]
#Making a pie chart for distribution of most wins after batting second
plt.figure(figsize=(7,7))
plt.pie(list(batting_second['winner'].value_counts()),labels=list(batting_second['winner'].value_counts().keys()),autopct='%0.1f%%')
plt.show()
# In[311]:
sns.countplot(train_data['winner'])
plt.xticks(rotation = 90)
# In[312]:
train_data = train_data.drop(columns=['date','umpire1','umpire2','toss_winner','win_by_runs','win_by_wickets','toss_decision','toss_decision','result','dl_applied','player_of_match'])
print(train_data)
sns.heatmap(train_data.isnull())
plt.show()
# In[313]:
train_data.replace({"Mumbai Indians":"MI", "Delhi Capitals":"DC",
"Sunrisers Hyderabad":"SRH", "Rajasthan Royals":"RR",
"Kolkata Knight Riders":"KKR", "Kings XI Punjab":"KXIP",
"Chennai Super Kings":"CSK", "Royal Challengers Bangalore":"RCB",
"Kochi Tuskers Kerala":"KTK", "Rising Pune Supergiants":"RPS",
"Gujarat Lions":"GL", "Pune Warriors":"PW"}, inplace=True)
encode = {'team1': {'KKR':1,'CSK':2,'RR':3,'MI':4,'SRH':5,'KXIP':6,'RCB':7,'DC':8,'KTK':9,'RPS':10,'GL':11,'PW':12},
'team2': {'KKR':1,'CSK':2,'RR':3,'MI':4,'SRH':5,'KXIP':6,'RCB':7,'DC':8,'KTK':9,'RPS':10,'GL':11,'PW':12},
'winner': {'KKR':1,'CSK':2,'RR':3,'MI':4,'SRH':5,'KXIP':6,'RCB':7,'DC':8,'KTK':9,'RPS':10,'GL':11,'PW':12,'Draw':13}}
train_data.replace(encode, inplace=True)
train_data.head(5)
# In[314]:
dicVal = encode['winner']
train = train_data[['team1','team2','city','winner']]
train.head(6)
# In[315]:
df = pd.DataFrame(train)
var_mod = ['city']
le = preprocessing.LabelEncoder()
for i in var_mod:
df[i] = le.fit_transform(df[i])
df.dtypes
# In[316]:
X = df[['team1', 'team2','city']]
y = df[['winner']]
sc = preprocessing.StandardScaler()
X = sc.fit_transform(X)
# In[317]:
linear_model= LinearRegression()
linear_model.fit(X,y)
print("Linear Regression accuracy: ",(linear_model.score(X,y))*100)
logistic_model = LogisticRegression()
logistic_model.fit(X,y)
print("Logistic Regression accuracy: ",(logistic_model.score(X,y))*100)
Random_model = RandomForestClassifier()
Random_model.fit(X,y)
print("Random Forest accuracy: ", (Random_model.score(X,y))*100)
knn_model = KNeighborsClassifier()
knn_model.fit(X,y)
print("KNeighbor Classifier accuracy", (knn_model.score(X,y))*100)
# In[318]:
#test_data = pd.read_csv('E:/BACKUP/users-admin files/Downloads/Testset Matches IPL 2020.csv')
test_data = pd.read_csv('E:/BACKUP/users-admin files/OneDrive/Desktop/Testset Matches IPL 2021.csv')
encode = {'team1': {'KKR':1,'CSK':2,'RR':3,'MI':4,'SRH':5,'KXIP':6,'RCB':7,'DC':8,'KTK':9,'RPS':10,'GL':11,'PW':12},
'team2': {'KKR':1,'CSK':2,'RR':3,'MI':4,'SRH':5,'KXIP':6,'RCB':7,'DC':8,'KTK':9,'RPS':10,'GL':11,'PW':12}}
test_data.replace(encode,inplace=True)
var_mod = ['city']
le = preprocessing.LabelEncoder()
for i in var_mod:
test_data[i] = le.fit_transform(test_data[i].astype(str))
# In[319]:
test_X = test_data[['team1','team2','city']]
test_X = sc.fit_transform(test_X)
y_predict = Random_model.predict(test_X)
newlist = list()
for i in y_predict:
newlist.append(list(dicVal.keys())[list(dicVal.values()).index(i)])
test_data['winner'] = newlist
#Decoding Team Names
for i in range(27):
test_data['team1'][i]=(list(dicVal.keys())[list(dicVal.values()).index(test_data['team1'][i])])
test_data['team2'][i]=(list(dicVal.keys())[list(dicVal.values()).index(test_data['team2'][i])])
test_data.head()