Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
import gc
import os
import sys
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error,mean_absolute_error
from sklearn.ensemble import RandomForestRegressor
from timeit import default_timer as timer
import lightgbm as lgb
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
df_train = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df_test = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv')
 df_train=reduce_mem_usage(df_train)
 df_test=reduce_mem_usage(df_test)
 df_train.head()
df_train.describe()
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<set_options>
%matplotlib inline
warnings.filterwarnings('ignore')
 print(os.listdir(".. /input"))
df_train=pd.read_csv('.. /input/train.csv',sep=',')
 df_test=pd.read_csv('.. /input/test.csv',sep=',')
 df_data = df_train.append(df_test)
 PassengerId = df_test['PassengerId']
Submission=pd.DataFrame()
Submission['PassengerId'] = df_test['PassengerId']<train_model>
def feature_engineering(df,is_train=True):
if is_train:
df = df[df['maxPlace'] > 1]
state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train:
df_out = agg.reset_index() [['matchId','groupId']]
else:
df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 df_out = reduce_mem_usage(df_out)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y
NUMERIC_COLUMNS=['Pclass','Age','SibSp','Parch','Fare']
test = df_test[NUMERIC_COLUMNS].fillna(-1000)
 data_to_train = df_train[NUMERIC_COLUMNS].fillna(-1000)
 y=df_train['Survived']
X_train, X_test, y_train, y_test = train_test_split(data_to_train, y, test_size=0.3,random_state=21, stratify=y)
 clf = SVC()
clf.fit(X_train, y_train)
 print("Accuracy: {}".format(clf.score(X_test, y_test)) )<predict_on_test>
x_train, y_train = feature_engineering(df_train,True)
 x_test,_ = feature_engineering(df_test,False )
Submission['Survived']=clf.predict(test)
 print(Submission.head())
 print('predictions generated' )<save_to_csv>
random_seed=1
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.05, random_state=random_seed )
print('file created' )<feature_engineering>
RF = RandomForestRegressor(n_estimators=10, min_samples_leaf=3, max_features=0.5, n_jobs=-1 )
embarked = ['S', 'C', 'Q']
for port in embarked:
fare_to_impute = df_data.groupby('Embarked')['Fare'].median() [embarked.index(port)]
df_data.loc[(df_data['Fare'].isnull())&(df_data['Embarked'] == port), 'Fare'] = fare_to_impute
df_train["Fare"] = df_data['Fare'][:891]
df_test["Fare"] = df_data['Fare'][891:]
print('Missing Fares Estimated' )<feature_engineering>
%%time
RF.fit(x_train, y_train )
for x in range(len(df_train["Fare"])) :
if pd.isnull(df_train["Fare"][x]):
pclass = df_train["Pclass"][x]
df_train["Fare"][x] = round(df_train[df_train["Pclass"] == pclass]["Fare"].mean() , 8)
 for x in range(len(df_test["Fare"])) :
if pd.isnull(df_test["Fare"][x]):
pclass = df_test["Pclass"][x]
df_test["Fare"][x] = round(df_test[df_test["Pclass"] == pclass]["Fare"].mean() , 8)
 df_data["FareBand"] = pd.qcut(df_data['Fare'], 8, labels = [1, 2, 3, 4,5,6,7,8] ).astype('int')
 df_train["FareBand"] = pd.qcut(df_train['Fare'], 8, labels = [1, 2, 3, 4,5,6,7,8] ).astype('int')
 df_test["FareBand"] = pd.qcut(df_test['Fare'], 8, labels = [1, 2, 3, 4,5,6,7,8] ).astype('int')
 df_train[["FareBand", "Survived"]].groupby(["FareBand"], as_index=False ).mean()
print('FareBand feature created' )<categorify>
mae_train_RF = mean_absolute_error(RF.predict(x_train), y_train)
 mae_val_RF = mean_absolute_error(RF.predict(x_val), y_val)
 print('mae train RF: ', mae_train_RF)
 print('mae val RF: ', mae_val_RF )
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
df_data["Embarked"] = df_data["Embarked"].map(embarked_mapping)
 df_train["Embarked"] = df_data["Embarked"][:891]
df_test["Embarked"] = df_data["Embarked"][891:]
print('Embarked feature created')
 df_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean()<feature_engineering>
def run_lgb(train_X, train_y, val_X, val_y, x_test):
params = {"objective" : "regression",
"metric" : "mae",
'n_estimators':20000,
'early_stopping_rounds':200,
"num_leaves" : 31,
"learning_rate" : 0.05,
"bagging_fraction" : 0.7,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.7
}
lgtrain = lgb.Dataset(train_X, label=train_y)
 lgval = lgb.Dataset(val_X, label=val_y)
 model = lgb.train(params, lgtrain, valid_sets=[lgtrain, lgval], early_stopping_rounds=200, verbose_eval=1000)
 pred_test_y = model.predict(x_test, num_iteration=model.best_iteration)
 return pred_test_y, model
fareband = [1,2,3,4]
for fare in fareband:
embark_to_impute = df_data.groupby('FareBand')['Embarked'].median() [fare]
df_data.loc[(df_data['Embarked'].isnull())&(df_data['FareBand'] == fare), 'Embarked'] = embark_to_impute
df_train["Embarked"] = df_data['Embarked'][:891]
df_test["Embarked"] = df_data['Embarked'][891:]
print('Missing Embarkation Estimated' )<categorify>
%%time
pred_test_lgb, model = run_lgb(x_train, y_train, x_val, y_val, x_test )
dummies=pd.get_dummies(df_train[['Sex']], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 testdummies=pd.get_dummies(df_test[['Sex']], prefix_sep='_')
 df_test = pd.concat([df_test, testdummies], axis=1)
 print('Gender Feature added ' )<categorify>
mae_train_lgb = mean_absolute_error(model.predict(x_train, num_iteration=model.best_iteration), y_train)
 mae_val_lgb = mean_absolute_error(model.predict(x_val, num_iteration=model.best_iteration), y_val)
 print('mae train lgb: ', mae_train_lgb)
 print('mae val lgb: ', mae_val_lgb )
gender_mapping = {"female": 0, "male": 1}
df_data["Sex"] = df_data['Sex'].map(gender_mapping)
 df_data["Sex"]=df_data["Sex"].astype('int')
 df_train["Sex"] = df_data["Sex"][:891]
df_test["Sex"] = df_data["Sex"][891:]
print('Gender Category created' )<categorify>
def run_DNN(x_train, y_train, x_val, y_val, x_test):
NN_model = Sequential()
NN_model.add(Dense(x_train.shape[1], input_dim = x_train.shape[1], activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(1, activation='linear'))
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
 NN_model.summary()
checkpoint_name = 'Weights-{epoch:03d}--{val_loss:.5f}.hdf5'
checkpoint = ModelCheckpoint(checkpoint_name, monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
 callbacks_list = [checkpoint]
NN_model.fit(x=x_train,
y=y_train,
batch_size=1000,
epochs=30,
verbose=1,
callbacks=callbacks_list,
validation_split=0.15,
validation_data=None,
shuffle=True,
class_weight=None,
sample_weight=None,
initial_epoch=0,
steps_per_epoch=None,
validation_steps=None)
 pred_test_y = NN_model.predict(x_test)
 pred_test_y = pred_test_y.reshape(-1)
 return pred_test_y, NN_model
df_data['NameLen'] = df_data['Name'].apply(lambda x: len(x))
print('Name Length calculated')
 df_train["NameLen"] = df_data["NameLen"][:891]
df_test["NameLen"] = df_data["NameLen"][891:]
df_train["NameBand"] = pd.cut(df_train["NameLen"], bins=5, labels = [1,2,3,4,5])
 df_test["NameBand"] = pd.cut(df_test["NameLen"], bins=5, labels = [1,2,3,4,5])
 dummies=pd.get_dummies(df_train[["NameBand"]].astype('category'), prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["NameBand"]].astype('category'), prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print("Name Length categories created")
 pd.qcut(df_train['NameLen'],5 ).value_counts()<feature_engineering>
%%time
pred_test_DNN, model = run_DNN(x_train, y_train, x_val, y_val, x_test )
df_data["Title"] = df_data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 df_data["Title"] = df_data["Title"].replace('Mlle', 'Miss')
 df_data["Title"] = df_data["Title"].replace('Master', 'Master')
 df_data["Title"] = df_data["Title"].replace(['Mme', 'Dona', 'Ms'], 'Mrs')
 df_data["Title"] = df_data["Title"].replace(['Jonkheer','Don'],'Mr')
 df_data["Title"] = df_data["Title"].replace(['Capt','Major', 'Col','Rev','Dr'], 'Millitary')
 df_data["Title"] = df_data["Title"].replace(['Lady', 'Countess','Sir'], 'Honor')
 df_train["Title"] = df_data['Title'][:891]
df_test["Title"] = df_data['Title'][891:]
titledummies=pd.get_dummies(df_train[['Title']], prefix_sep='_')
 df_train = pd.concat([df_train, titledummies], axis=1)
 ttitledummies=pd.get_dummies(df_test[['Title']], prefix_sep='_')
 df_test = pd.concat([df_test, ttitledummies], axis=1)
 print('Title categories added' )<data_type_conversions>
mae_train_DNN = mean_absolute_error(model.predict(x_train), y_train)
 mae_val_DNN = mean_absolute_error(model.predict(x_val), y_val)
 print('mae train dnn: ', mae_train_DNN)
 print('mae val dnn: ', mae_val_DNN )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Millitary": 5, "Honor": 6}
df_data["TitleCat"] = df_data['Title'].map(title_mapping)
 df_data["TitleCat"] = df_data["TitleCat"].astype(int)
 df_train["TitleCat"] = df_data["TitleCat"][:891]
df_test["TitleCat"] = df_data["TitleCat"][891:]
print('Title Category created' )<feature_engineering>
pred_test_RF = RF.predict(x_test)
 df_test['winPlacePerc_RF'] = pred_test_RF
submission = df_test[['Id', 'winPlacePerc_RF']]
submission.to_csv('submission_RF.csv', index=False )
titles = ['Master', 'Miss', 'Mr', 'Mrs', 'Millitary','Honor']
for title in titles:
age_to_impute = df_data.groupby('Title')['Age'].median() [title]
df_data.loc[(df_data['Age'].isnull())&(df_data['Title'] == title), 'Age'] = age_to_impute
df_train["Age"] = df_data['Age'][:891]
df_test["Age"] = df_data['Age'][891:]
print('Missing Ages Estimated' )<categorify>
df_test['winPlacePerc_lgb'] = pred_test_lgb
submission = df_test[['Id', 'winPlacePerc_lgb']]
submission.to_csv('submission_lgb.csv', index=False )
bins = [0,12,24,45,60,np.inf]
labels = ['Child', 'Young Adult', 'Adult','Older Adult','Senior']
df_train["AgeBand"] = pd.cut(df_train["Age"], bins, labels = labels)
 df_test["AgeBand"] = pd.cut(df_test["Age"], bins, labels = labels)
 print('Age Feature created')
 dummies=pd.get_dummies(df_train[["AgeBand"]], prefix_sep='_')
 df_train = pd.concat([df_train, dummies], axis=1)
 dummies=pd.get_dummies(df_test[["AgeBand"]], prefix_sep='_')
 df_test = pd.concat([df_test, dummies], axis=1)
 print('AgeBand feature created' )<feature_engineering>
df_test['winPlacePerc_DNN'] = pred_test_DNN
submission = df_test[['Id', 'winPlacePerc_DNN']]
submission.to_csv('submission_DNN.csv', index=False )
<data_type_conversions><EOS> 
weight_DNN =(1 - mae_val_DNN)/(3 - mae_val_DNN - mae_val_RF - mae_val_lgb)
 weight_RF =(1 - mae_val_RF)/(3 - mae_val_DNN - mae_val_RF - mae_val_lgb)
 weight_lgb =(1 - mae_val_lgb)/(3 - mae_val_DNN - mae_val_RF - mae_val_lgb)
 df_test['winPlacePerc'] = df_test.apply(lambda x: x['winPlacePerc_RF'] * weight_RF + x['winPlacePerc_DNN'] * weight_DNN + x['winPlacePerc_lgb'] * weight_lgb, axis=1)
 submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )