-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel .py
279 lines (167 loc) · 6.86 KB
/
kernel .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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# coding: utf-8
# In[1]:
# 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 in
# ****** This code was run on kaggle notebook for PUBG Finish Placement Prediction ******
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 "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
import os
print(os.listdir("../input"))
df = pd.read_csv('../input/train_V2.csv')
# Any results you write to the current directory are saved as output.
# In[2]:
# Dropping ID columns. Not useful for our analysis
df = df.drop(columns = ['Id', 'groupId','matchId'])
# In[3]:
# Used to display all columns
pd.set_option('display.max_columns', 500)
df.head()
# In[4]:
# Certain modes have very low number of players
# We are only considering the 6 most popular modes. Dropping 'normal-squad-fpp','crashfpp','normal-duo-fpp','flaretpp','normal-solo-fpp','flarefpp','normal-squad','normal-solo','normal-duo','crashtpp'.
df = df.set_index('matchType')
df = df.drop(['normal-squad-fpp','crashfpp','normal-duo-fpp','flaretpp','normal-solo-fpp','flarefpp','normal-squad','normal-solo','normal-duo','crashtpp' ])
df = df.reset_index()
# In[5]:
# Checking correlation amongst all the features. Highly un-correlated features will be removed
import matplotlib.pyplot as plt
corr = df.corr()
corr.style.background_gradient()
# In[6]:
# We will now drop uncorrelated features 'killPoints','matchDuration','maxPlace','numGroups','rankPoints','roadKills','swimDistance','vehicleDestroys','killPoints','winPoints'.
df = df.drop(columns = ['killPoints','matchDuration','maxPlace','numGroups','rankPoints','roadKills','swimDistance','vehicleDestroys','killPoints','winPoints'])
# In[7]:
df.head()
# The matchType feature is the only one with 'object' datatype. We will use LabelEncoder() to label encode it.
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
df['matchType'] = le.fit_transform(df['matchType'])
# In[8]:
# We are trying to figure out if any of the data has skewness to it. Any skewed data has to be normalised.
# We are also trying to bring down are data between [0,1] so that the model is better able to understand it.
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
df.hist(bins = 50, figsize = (20,15))
plt.show()
# In[9]:
import numpy as np
df['damageDealt'] = df['damageDealt'].apply(np.cbrt)
df['walkDistance'] = df['walkDistance'].apply(np.cbrt)
df['rideDistance'] = df['rideDistance'].apply(np.cbrt)
df['longestKill'] = df['longestKill'].apply(np.cbrt)
# In[10]:
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
df.hist(bins = 50, figsize = (20,15))
plt.show()
# In[11]:
df['walkDistance'] = df['walkDistance']/np.max(df['walkDistance'])
df['rideDistance'] = df['rideDistance']/np.max(df['rideDistance'])
df['damageDealt'] = df['damageDealt']/np.max(df['damageDealt'])
df['weaponsAcquired'] = df['weaponsAcquired'].apply(np.cbrt)
df['kills'] = df['kills'].apply(np.cbrt)
# In[12]:
df.hist(bins = 50, figsize = (20,15))
plt.show()
# In[13]:
# The killPlace attribute shows what is the players position on the kills leaderboard.
# Since it's a form of ranking from 1-100
# We group them into 10 bands each of width 10
df['killPlaceBand'] = pd.cut(df['killPlace'],10)
df[['killPlaceBand', 'winPlacePerc']].groupby(['killPlaceBand'], as_index = False).mean().sort_values(by = 'killPlaceBand', ascending = True)
# In[14]:
df['killPlaceBand'] = le.fit_transform(df['killPlaceBand'])
# In[15]:
df = df.drop(columns = 'killPlace')
# In[16]:
# The dataset is too big. Any rows which contain missing values are less. So we just dropped them.
df = df.dropna()
# In[17]:
# Rather than using a separate test.csv, we split our train data into test and train.
# It's fairly big enough to not cause any issues.
from sklearn.model_selection import train_test_split
train, test = train_test_split(df,train_size = 0.66)
# In[18]:
x_train = train.drop(columns = 'winPlacePerc')
y_train = train['winPlacePerc']
x_test = test.drop(columns = 'winPlacePerc')
y_test = test['winPlacePerc']
# In[19]:
# Linear Regressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
l_r = LinearRegression()
l_r.fit(x_train,y_train)
y_pred_l_r = l_r.predict(x_test)
print(l_r.score(x_train,y_train))
print(l_r.score(x_test,y_test))
print(mean_absolute_error(y_test,y_pred_l_r))
# In[20]:
# XGBoostRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
xgb = XGBRegressor()
xgb.fit(x_train,y_train)
xgb.score(x_train,y_train)
xgb.score(x_test,y_test)
y_pred_xgb = xgb.predict(x_test)
print(mean_absolute_error(y_test,y_pred_xgb))
# In[21]:
# Cross Validation
from sklearn.model_selection import cross_val_score
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
xgb = XGBRegressor()
scores = cross_val_score(xgb,x_train,y_train,scoring = 'neg_mean_absolute_error',cv = 10)
print(scores)
print(scores.mean())
# In[22]:
# Decision tree
from sklearn.tree import DecisionTreeRegressor
dtr = DecisionTreeRegressor(max_depth = 6)
dtr.fit(x_train,y_train)
dtr.score(x_train,y_train)
dtr.score(x_test,y_test)
y_pred_dtr = dtr.predict(x_test)
print(mean_absolute_error(y_test,y_pred_dtr))
print(dtr.score(x_train,y_train))
print(dtr.score(x_test,y_test))
# In[23]:
# SGDRegressor
from sklearn import linear_model
sgd = linear_model.SGDRegressor(max_iter=1000)
sgd.fit(x_train,y_train)
print(sgd.score(x_train,y_train))
print(sgd.score(x_test,y_test))
y_pred_sgd = sgd.predict(x_test)
print(mean_absolute_error(y_test,y_pred_sgd))
# In[24]:
#Random Forest Regressor
from sklearn.ensemble import RandomForestRegressor
r_f_r = RandomForestRegressor(n_estimators = 10)
r_f_r.fit(x_train,y_train)
r_f_r.score(x_train,y_train)
y_pred = r_f_r.predict(x_test)
print(mean_absolute_error(y_test,y_pred))
print(r_f_r.score(x_test,y_test))
# In[25]:
mae_lr = mean_absolute_error(y_test,y_pred_l_r)
mae_xgb = mean_absolute_error(y_test,y_pred_xgb)
mae_sgd = mean_absolute_error(y_test,y_pred_sgd)
mae_dtr = mean_absolute_error(y_test,y_pred_dtr)
mae_rfr = mean_absolute_error(y_test,y_pred)
# In[26]:
models = pd.DataFrame({
'Model': ['Linear Regression', 'XGBoost Regressor', 'Decision Tree',
'Random Forest','Stochastic Gradient Decent'],
'MAE': [mae_lr,mae_xgb,mae_dtr,mae_rfr,mae_sgd]})
models.sort_values(by='MAE', ascending=True)
# In[28]:
# XGBoost Regressor gives us the best MAE
# Pickling
import pickle
filename = 'xgb_pickle.sav'
pickle.dump(xgb,open(filename,'wb'))