forked from kuharan/IPL-ML-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprediction.py
178 lines (159 loc) · 5.06 KB
/
prediction.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
import pandas as pd
from sklearn import model_selection
from sklearn.tree import DecisionTreeClassifier
def predict(home_team, away_team, city, toss_winner, toss_decision):
matches_cleaned_data = pd.read_csv('./Dataset/matches_cleaned.csv')
matches_df = matches_cleaned_data[['team1', 'team2', 'city', 'toss_winner', 'toss_decision', 'winner']]
# Split-out validation dataset
array = matches_df.values
x = array[:, 0:5]
y = array[:, 5]
validation_size = 0.10
seed = 7
x_train, x_validation, y_train, y_validation = model_selection.train_test_split(x, y, test_size=validation_size,
random_state=seed)
# Test options and evaluation metric
knn = DecisionTreeClassifier()
knn.fit(x_train, y_train)
results = convert_to_numerical_field(home_team, away_team, city, toss_winner, toss_decision)
predictions = knn.predict([results])
team = ''
if predictions[0] == '6':
team = 'KKR'
if predictions[0] == "5":
team = 'RCB'
if predictions[0] == "9":
team = 'CSK'
if predictions[0] == "10":
team = 'RR'
if predictions[0] == "7":
team = 'DD'
if predictions[0] == "8":
team = 'KXIP'
if predictions[0] == "1":
team = 'SRH'
if predictions[0] == "2":
team = 'MI'
print("model->" + team)
if int(predictions) != convert_again(home_team).__int__() and int(predictions) != convert_again(away_team).__int__():
print("Exception Case")
winner = convert_to_shortform(calculate_ef_score(home_team, away_team))
print("EF score data ->" + winner)
return winner
else:
return team.__str__()
def convert_to_shortform(winning_team):
if winning_team == 'Kolkata':
return 'KKR'
if winning_team == "Bangalore":
return 'RCB'
if winning_team == "Pune":
return 'CSK'
if winning_team == "Jaipur":
return 'RR'
if winning_team == "Delhi":
return 'DD'
if winning_team == "Dharamshala":
return 'KXIP'
if winning_team == "Hyderabad":
return 'SRH'
if winning_team == "Mumbai":
return 'MI'
def convert_again(home_team):
if home_team == 'Kolkata':
return 6
if home_team == "Bangalore":
return 5
if home_team == "Pune":
return 9
if home_team == "Jaipur":
return 10
if home_team == "Delhi":
return 7
if home_team == "Dharamshala":
return 8
if home_team == "Hyderabad":
return 1
if home_team == "Mumbai":
return 2
def convert_to_numerical_field(home_team, away_team, city, toss_winner, toss_decision):
list = []
if home_team == 'Kolkata':
list.append(6)
if home_team == "Bangalore":
list.append(5)
if home_team == "Pune":
list.append(9)
if home_team == "Jaipur":
list.append(10)
if home_team == "Delhi":
list.append(7)
if home_team == "Dharamshala":
list.append(8)
if home_team == "Hyderabad":
list.append(1)
if home_team == "Mumbai":
list.append(2)
if away_team == "Kolkata":
list.append(6)
if away_team == "Bangalore":
list.append(5)
if away_team == "Pune":
list.append(9)
if away_team == "Jaipur":
list.append(10)
if away_team == "Delhi":
list.append(7)
if away_team == "Dharamshala":
list.append(8)
if away_team == "Hyderabad":
list.append(1)
if away_team == "Mumbai":
list.append(2)
if city[6:] == "Kolkata":
list.append(7)
if city[6:] == "Bangalore":
list.append(5)
if city[6:] == "Pune":
list.append(2)
if city[6:] == "Jaipur":
list.append(11)
if city[6:] == "Delhi":
list.append(8)
if city[6:] == "Dharamshala":
list.append(24)
if city[6:] == "Hyderabad":
list.append(1)
if city[6:] == "Mumbai":
list.append(6)
if toss_winner == "KKR":
list.append(6)
if toss_winner == "RCB":
list.append(5)
if toss_winner == "CSK":
list.append(9)
if toss_winner == "RR":
list.append(10)
if toss_winner == "DD":
list.append(7)
if toss_winner == "KXIP":
list.append(8)
if toss_winner == "SRH":
list.append(1)
if toss_winner == "MI":
list.append(2)
if toss_decision == "Bat":
list.append(2)
if toss_decision == "Field":
list.append(1)
return list
# prediction from site scrape data
def calculate_ef_score(home, away):
data = pd.read_csv('./Dataset/_team_rank.csv')
home_score = list(data.loc[data['Team'] == home]['sum'])
away_score = list(data.loc[data['Team'] == away]['sum'])
if home_score > away_score:
return home
else:
return away
# predict('Jaipur', 'Hyderabad', 'City: Jaipur', 'RR', 'Bat')