Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<compute_train_metric>
print(os.listdir(".. /input"))
clf = SVC()
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_test)
 acc_clf = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_clf )<save_to_csv>
small_train = True
only_imp_cols = False
test = df_test[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 Submission['Survived']=clf.predict(test)
 Submission.set_index('PassengerId', inplace=True)
 Submission.to_csv('baselinemodel01.csv',sep=',')
 print('Submission Created' )<define_variables>
try:
train = pd.read_csv('.. /input/train_V2.csv')
 except:
train = pd.read_csv('~/.kaggle/competitions/pubg-finish-placement-prediction/train_V2.csv' )
REVISED_NUMERIC_COLUMNS=['Title_Master', 'Title_Millitary',
'Title_Miss', 'Title_Mr', 'Title_Mrs', 'Embarked_C', 'Embarked_Q',
'Embarked_S', 'FareBand_1', 'FareBand_2', 'FareBand_3', 'FareBand_4',
'Pclass_1', 'Pclass_2', 'Pclass_3', 'AgeGroup_Child',
'AgeGroup_Young Adult', 'AgeGroup_Adult', 'AgeGroup_Older Adult',
'AgeGroup_Senior', 'Sex_female', 'Sex_male', 'Alone', 'Family_Survival',
'HadCabin','SibSp_0', 'SibSp_1', 'SibSp_2',
'SibSp_3', 'SibSp_4', 'SibSp_5', 'SibSp_8', 'Parch_0', 'Parch_1',
'Parch_2', 'Parch_3', 'Parch_4', 'Parch_5', 'Parch_6', ]
predictors = df_train.drop(['Survived', 'PassengerId'], axis=1)
 data_to_train = df_train[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 X=data_to_train
y = df_train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(data_to_train, y, test_size = 0.3,random_state=21, stratify=y)
 print('Data Split' )<choose_model_class>
((train['maxPlace'] - 1)* train['winPlacePerc'])[:10]
param_dist = {"max_depth": np.arange(1, 6),
"max_features": np.arange(1, 10),
"min_samples_leaf": np.arange(1, 6),
"criterion": ["gini","entropy"]}
tree = DecisionTreeClassifier()
tree_cv = RandomizedSearchCV(tree, param_dist, cv=30)
 tree_cv.fit(X,y)
 y_pred = tree_cv.predict(x_val)
 print("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))
print("Best score is {}".format(tree_cv.best_score_))
acc_tree_cv = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_tree_cv )<predict_on_test>
if small_train:
train = train[train['matchId'].isin(train['matchId'].unique() [np.random.rand(len(train['matchId'].unique())) < 0.4])].reset_index(drop=True )
test = df_test[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 tree = DecisionTreeClassifier(max_depth=5,max_features=7,min_samples_leaf=1,criterion="entropy")
 tree.fit(X,y)
 Submission['Survived']=tree.predict(test)
 print(Submission.head(5))<save_to_csv>
def feature_engineering(df, train=True):
drop_labels = ['Id', 'groupId', 'matchId', 'maxPlace', 'numGroups', 'matchDuration', 'matchType']
if train:
df['wppBin'] = np.uint16(np.round(df['winPlacePerc']*(df['maxPlace']-1)))
 df['wppBin2'] = df['wppBin'] +(100 - df['maxPlace'])
 df['wppBin3'] = df['wppBin'] + np.uint16(np.round(( 100 - df['maxPlace'])*.5))
drop_labels += ['winPlacePerc', 'wppBin', 'wppBin2', 'wppBin3']
df['killrate'] = df['kills']/df['matchDuration']
df['totalDistance'] = df['walkDistance'] + df['swimDistance'] + df['rideDistance']
df['avgSpeed'] = df['totalDistance'] / df['matchDuration']
df['healsPlusBoosts'] = df['boosts'] + df['heals']
df['kar'] = df['kills'] + df['assists'] + df['revives']
le = LabelEncoder()
le.fit(['squad-fpp', 'duo', 'solo-fpp', 'squad', 'duo-fpp', 'solo',
'normal-squad-fpp', 'crashfpp', 'flaretpp', 'normal-solo-fpp',
'flarefpp', 'normal-duo-fpp', 'normal-duo', 'crashtpp',
'normal-squad', 'normal-solo'])
 df['matchType'] = le.transform(df['matchType'])
 agglabels = [ele for ele in df.columns if ele not in drop_labels]
groupByMatch = df.groupby(['matchId','groupId'])[agglabels]
groupByGroup = df.groupby(['groupId', 'Id'])[agglabels]
df = df.merge(groupByMatch.agg('min' ).reset_index() , suffixes=['','match_min'], on=['matchId','groupId'], how='left')
 print('Checkpoint 1/7')
 df = df.merge(groupByMatch.agg('max' ).reset_index() , suffixes=['','match_max'], on=['matchId','groupId'], how='left')
 print('Checkpoint 2/7')
 df = df.merge(groupByMatch.agg('mean' ).reset_index() , suffixes=['','match_mean'], on=['matchId','groupId'], how='left')
 print('Checkpoint 3/7')
 df = df.merge(groupByMatch.agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index() , suffixes=['','match_rank'], on=['matchId', 'groupId'], how='left')
 print('Checkpoint 4/7')
 df = df.merge(groupByGroup.agg('min' ).reset_index() , suffixes=['','group_min'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 5/7')
 df = df.merge(groupByGroup.agg('max' ).reset_index() , suffixes=['','group_max'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 6/7')
 df = df.merge(groupByGroup.agg('mean' ).reset_index() , suffixes=['','group_mean'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 7/7')
 df = df.merge(groupByGroup.agg('mean' ).groupby('groupId' ).rank(pct=True ).reset_index() , suffixes=['','group_rank'], on=['Id', 'groupId'], how='left')
 df = df.merge(groupByGroup.size().reset_index(name='group_size'), how='left', on=['Id', 'groupId'])
 df = df.merge(groupByMatch.size().reset_index(name='match_size'), how='left', on=['matchId','groupId'])
 return df
Submission.to_csv('Tunedtree1submission.csv',sep=',')
 print("Submission Submitted" )<define_variables>
train, _ = reduce_mem_usage(train )
REVISED_NUMERIC_COLUMNS=['Title_Master', 'Title_Millitary',
'Title_Miss', 'Title_Mr', 'Title_Mrs', 'Embarked_C', 'Embarked_Q',
'Embarked_S', 'FareBand_1', 'FareBand_2', 'FareBand_3', 'FareBand_4',
'Pclass_1', 'Pclass_2', 'Pclass_3', 'AgeGroup_Child',
'AgeGroup_Young Adult', 'AgeGroup_Adult', 'AgeGroup_Older Adult',
'AgeGroup_Senior', 'Sex_female', 'Sex_male', 'Alone', 'Family_Survival',
'HadCabin','SibSp_0', 'SibSp_1', 'SibSp_2',
'SibSp_3', 'SibSp_4', 'SibSp_5', 'SibSp_8', 'Parch_0', 'Parch_1',
'Parch_2', 'Parch_3', 'Parch_4', 'Parch_5', 'Parch_6', ]
predictors = df_train.drop(['Survived', 'PassengerId'], axis=1)
 data_to_train = df_train[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 data_to_predict=df_test[REVISED_NUMERIC_COLUMNS].fillna(-1000)
 y=df_train['Survived']
X=data_to_train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,random_state=21, stratify=y)
 print('Data split' )<import_modules>
msk = train['matchId'].unique() [np.random.rand(len(train['matchId'].unique())) < 0.8]
train_train = train[train['matchId'].isin(msk)].reset_index(drop=True)
 train_val = train[~train['matchId'].isin(msk)].reset_index(drop=True)
 train = None
msk = None
gc.collect()
train_train = feature_engineering(train_train)
 train_train, _ = reduce_mem_usage(train_train)
 train_val = feature_engineering(train_val)
 train_val, _ = reduce_mem_usage(train_val )
print('Modules imported' )<choose_model_class>
train_cols = [ele for ele in train_train.columns if ele not in ['Id', 'groupId', 'matchId', 'winPlacePerc', 'wppBin', 'wppBin2', 'wppBin3']]
target = 'winPlacePerc'
model = Sequential()
model.add(Dense(units=56, input_dim=X.shape[1], activation='selu'))
model.add(Dropout(0.5))
model.add(Dense(units=27, activation='selu'))
model.add(Dropout(0.5))
model.add(Dense(units=1, activation='tanh'))
model.compile(loss='mse', optimizer='sgd')
 print('Keras Model Created' )<train_model>
m1inputs = Input(shape=(len(train_cols),))
m1l1 = BatchNormalization()(m1inputs)
 m1l1 = Dense(128, activation='relu' )(m1l1)
 m1l1 = BatchNormalization()(m1l1)
 m1l2 = Dense(128, activation='relu' )(m1l1)
 m1l2 = BatchNormalization()(m1l2)
 m1l2 = Dropout(0.75 )(m1l2)
 m1l3 = Dense(128, activation='relu' )(m1l2)
 m1l3 = BatchNormalization()(m1l3)
 m1l4 = Dense(128, activation='relu' )(m1l3)
 m1l4 = BatchNormalization()(m1l4)
 m1l4 = Dropout(0.75 )(m1l4)
 m1output = Dense(100, activation='softmax' )(m1l4 )
model.fit(X.values, y.values, epochs=500, verbose=0)
 print('Keras model fitted' )<predict_on_test>
model1 = Model(inputs=[m1inputs], outputs=[m1output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model1.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'] )
df_test=df_test.set_index('PassengerId')
 p_survived = model.predict_classes(df_test)
 print('Prediction Completed' )<prepare_output>
enc = OneHotEncoder()
enc.fit(np.arange(100 ).reshape(-1,1))
submission = pd.DataFrame()
submission['PassengerId'] = df_test.index
submission['Survived'] = p_survived
print('predictions added to submission' )<save_to_csv>
model1.fit(train_train[train_cols], enc.transform(train_train['wppBin'].values.reshape(-1,1)) ,
epochs=20, batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin'].values.reshape(-1,1))),
verbose=2 )
submission.to_csv('DeepLearning03.csv', index=False)
 print('csv created' )<choose_model_class>
val_preds = model1.predict(train_val[train_cols], verbose=True )
seed=70
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
 cvscores = []
for train, test in kfold.split(X, y):
model = Sequential()
model.add(Dense(54, input_dim=X.shape[1], activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(54, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(54, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1, activation='tanh'))
model.compile(loss='binary_crossentropy', optimizer='Adagrad', metrics=['accuracy'])
 model.fit(X_train, y_train, epochs=150, batch_size=10, verbose=0)
 scores = model.evaluate(X_test, y_test, verbose=0)
 print("%s: %.2f%%" %(model.metrics_names[1], scores[1]*100))
cvscores.append(scores[1] * 100)
 print("%.2f%%(+/- %.2f%%)" %(np.mean(cvscores), np.std(cvscores)) )<prepare_output>
val_preds_float = np.argmax(val_preds, axis=1)/(train_val['maxPlace'] + 1 )
submission = pd.DataFrame()
submission['PassengerId'] = df_test.index
submission['Survived'] = p_survived
print('predictions added to submission' )<save_to_csv>
print('estimated score from verify set:')
 np.mean(np.abs(val_preds_float - train_val[target]))
submission.to_csv('OptimisedDeepLearning04.csv', index=False)
 print('csv created' )<load_from_csv>
m2inputs = Input(shape=(len(train_cols),))
m2l1 = BatchNormalization()(m2inputs)
 m2l1 = Dense(128, activation='relu' )(m2l1)
 m2l1 = BatchNormalization()(m2l1)
 m2l2 = Dense(128, activation='relu' )(m2l1)
 m2l2 = BatchNormalization()(m2l2)
 m2l2 = Dropout(0.75 )(m2l2)
 m2l3 = Dense(128, activation='relu' )(m2l2)
 m2l3 = BatchNormalization()(m2l3)
 m2l4 = Dense(128, activation='relu' )(m2l3)
 m2l4 = BatchNormalization()(m2l4)
 m2l4 = Dropout(0.75 )(m2l4)
 m2output = Dense(100, activation='softmax' )(m2l4 )
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv" )<drop_column>
model2 = Model(inputs=[m2inputs], outputs=[m2output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model2.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 model2.fit(train_train[train_cols], enc.transform(train_train['wppBin2'].values.reshape(-1,1)) ,
epochs=20,
batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin2'].values.reshape(-1,1))),
verbose=2 )
train = train.drop(columns= ['Name','Ticket','Cabin'])
 test = test.drop(columns= ['Name','Ticket','Cabin'] )<data_type_conversions>
m3inputs = Input(shape=(len(train_cols),))
m3l1 = BatchNormalization()(m3inputs)
 m3l1 = Dense(128, activation='relu' )(m3l1)
 m3l1 = BatchNormalization()(m3l1)
 m3l2 = Dense(128, activation='relu' )(m3l1)
 m3l2 = BatchNormalization()(m3l2)
 m3l2 = Dropout(0.75 )(m3l2)
 m3l3 = Dense(128, activation='relu' )(m3l2)
 m3l3 = BatchNormalization()(m3l3)
 m3l4 = Dense(128, activation='relu' )(m3l3)
 m3l4 = BatchNormalization()(m3l4)
 m3l4 = Dropout(0.75 )(m3l4)
 m3output = Dense(100, activation='softmax' )(m3l4 )
train['Embarked_S'] =(train['Embarked'] == 'S' ).astype(int)
 train['Embarked_C'] =(train['Embarked'] == 'C' ).astype(int)
 train['Embarked_Q'] =(train['Embarked'] == 'Q' ).astype(int)
 train['Gender'] =(train['Sex'] == 'male' ).astype(int )<data_type_conversions>
model3 = Model(inputs=[m3inputs], outputs=[m3output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model3.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 model3.fit(train_train[train_cols], enc.transform(train_train['wppBin3'].values.reshape(-1,1)) ,
epochs=20,
batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin3'].values.reshape(-1,1))),
verbose=2 )
test['Embarked_S'] =(test['Embarked'] == 'S' ).astype(int)
 test['Embarked_C'] =(test['Embarked'] == 'C' ).astype(int)
 test['Embarked_Q'] =(test['Embarked'] == 'Q' ).astype(int)
 test['Gender'] =(test['Sex'] == 'male' ).astype(int )<drop_column>
predsbin1 = model1.predict(train_val[train_cols], verbose=True)
 predsbin2 = model2.predict(train_val[train_cols], verbose=True)
 predsbin3 = model3.predict(train_val[train_cols], verbose=True )
train = train.drop(columns = ['Sex'])
 test = test.drop(columns = ['Sex'] )<drop_column>
tpredsbin1 = model1.predict(train_train[train_cols], verbose=True)
 tpredsbin2 = model2.predict(train_train[train_cols], verbose=True)
 tpredsbin3 = model3.predict(train_train[train_cols], verbose=True )
train = train.drop(columns = ['Embarked'])
 test = test.drop(columns = ['Embarked'] )<train_model>
trainMaxPlace = train_train['maxPlace']
valMaxPlace = train_val['maxPlace']
trainTar = train_train[target]
valTar = train_val[target]
train_train = None
train_verify = None
gc.collect()
train.fillna(0, inplace=True)
 test.fillna(0, inplace=True )<prepare_x_and_y>
preds1 = np.argmax(predsbin1,axis=1)/(valMaxPlace + 1)
 preds2 =(np.argmax(predsbin2,axis=1)-(100 - valMaxPlace)) /(valMaxPlace + 1)
 preds3 =(np.argmax(predsbin3,axis=1)- np.uint16(np.round(( 100 - valMaxPlace)*.5)))/(valMaxPlace + 1 )
X = train.drop(columns = ['Survived'])
 y = train['Survived']<train_model>
tpreds1 = np.argmax(tpredsbin1,axis=1)/(trainMaxPlace + 1)
 tpreds2 =(np.argmax(tpredsbin2,axis=1)-(100 - trainMaxPlace)) /(trainMaxPlace + 1)
 tpreds3 =(np.argmax(tpredsbin3,axis=1)- np.uint16(np.round(( 100 - trainMaxPlace)*.5)))/(trainMaxPlace + 1 )
model = XGBClassifier(learning_rate=0.01, n_estimators=1000)
 model.fit(X,y )<predict_on_test>
print('Training scores:')
 print(np.mean(np.abs(tpreds1 - trainTar)))
 print(np.mean(np.abs(tpreds2 - trainTar)))
 print(np.mean(np.abs(tpreds3 - trainTar)))
 print('Validation scores')
 print(np.mean(np.abs(preds1 - valTar)))
 print(np.mean(np.abs(preds2 - valTar)))
 print(np.mean(np.abs(preds3 - valTar)) )
pred = model.predict(test )<save_to_csv>
train_outs = pd.DataFrame({'p1': tpreds1,
'p2': tpreds2,
'p3': tpreds3,
'maxPlace': trainMaxPlace,
'winPlacePerc': trainTar})
 val_outs = pd.DataFrame({'p1': preds1,
'p2': preds2,
'p3': preds3,
'maxPlace': valMaxPlace,
'winPlacePerc': valTar})
 train_outs.to_csv('train_out.csv')
 val_outs.to_csv('validate_out.csv' )
result = pd.DataFrame({'PassengerId':test['PassengerId'], 'Survived':pred})
 result.to_csv("submission.csv", index=False )<load_from_csv>
clf = xgb.XGBRegressor(max_depth=4,
silent=False,
learning_rate=0.6,
n_estimators=100,
n_jobs=-1)
 boost_cols = ['p1', 'p2', 'p3', 'maxPlace']
target = 'winPlacePerc'
clf.fit(train_outs[boost_cols], train_outs[target],
eval_set=[(train_outs[boost_cols], train_outs[target]),(val_outs[boost_cols], val_outs[target])],
verbose=False, eval_metric='mae', early_stopping_rounds=5 )
train_df=pd.read_csv(".. /input/train.csv")
 test_df=pd.read_csv(".. /input/test.csv")
 train_df.head()<concatenate>
xgb_val_preds = clf.predict(val_outs[boost_cols])
 print('validation set mae: {}'.format(np.mean(np.abs(xgb_val_preds - val_outs[target]))))
combined_df=pd.concat([train_df,test_df] )<groupby>
try:
test = pd.read_csv('.. /input/test_V2.csv')
 except:
test = pd.read_csv('~/.kaggle/competitions/pubg-finish-placement-prediction/test_V2.csv')
 test, _ = reduce_mem_usage(test)
 test = feature_engineering(test, train=False)
 test_ids = test['Id']
test = test[train_cols]
combined_df.loc[combined_df["Age"].isnull() ].groupby(["Pclass","Sex"])["Sex"].agg(["count"] )<groupby>
test, _ = reduce_mem_usage(test )
combined_df.groupby(["Sex","SibSp","Parch"])["Age"].agg(["median"] )<filter>
preds1 = model1.predict(test, verbose=True)
 preds2 = model2.predict(test, verbose=True)
 preds3 = model3.predict(test, verbose=True )
combined_df.loc[combined_df["SibSp"]==8]<feature_engineering>
testMaxPlace = test['maxPlace']
test = None
gc.collect()
combined_df["Name_key"]=""
combined_df["Name_key"]=combined_df["Name"].str.split(',',expand=True)[1].str.split(' ',expand=True)[1]<groupby>
preds1 = np.argmax(preds1,axis=1)/(testMaxPlace + 1)
 preds2 =(np.argmax(preds2,axis=1)-(100 - testMaxPlace)) /(testMaxPlace + 1)
 preds3 =(np.argmax(preds3,axis=1)- np.uint16(np.round(( 100 - testMaxPlace)*.5)))/(testMaxPlace + 1 )
combined_df.groupby(["Sex","Name_key","Parch"])["Age"].agg(["median"] )<categorify>
test_outs = pd.DataFrame({'p1': preds1,
'p2': preds2,
'p3': preds3,
'maxPlace': testMaxPlace})
 test_outs.to_csv('test_out.csv' )
combined_df["Fare_Group"]=pd.cut(combined_df["Fare"],range(0,350,50),right=False)
 combined_df["Fare_Group"]=combined_df["Fare_Group"].astype("object")
 combined_df["Fare_Group"].fillna("[300,600)",inplace=True )<groupby>
test_preds = clf.predict(test_outs[boost_cols] )
<merge><EOS> 
submission = pd.DataFrame({'Id': test_ids, 'winPlacePerc': test_preds})
 submission.to_csv('submission.csv', index=False )