Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
score['Sacrificed'] = 0
score['SN'] = 0
score['FS'] = 0
for i in score:
score.loc[(( score.Name.values == 1)) ,'SN']=-5
score.loc[(( score.Family.values == 5)) ,'FS']=-1
score.loc[(( score.Family.values == 6)) ,'FS']=-3
score.loc[(( score.Family.values == 8)) ,'FS']=-5
score.loc[(( score.Family.values >= 9)) ,'FS']=-5
score['Sacrificed'] = score['SN'] + score['FS']
score.head(3 )<feature_engineering>
print('-' * 80)
 print('train')
 train = import_data('.. /input/train.csv')
 print('-' * 80)
 print('test')
 test = import_data('.. /input/test.csv')
 print('-' * 80)
 print('sample_submission')
 submission = import_data('.. /input/sample_submission.csv' )
score['Score'] = score['Class'] + score['Wealth'] + score['Priority'] + score['Situation'] + score['Sacrificed']<create_dataframe>
train.isnull().sum()
df_new = pd.DataFrame()<categorify>
train.isnull().sum()
df_enc = df_new.apply(LabelEncoder().fit_transform)
 df_enc.head()<split>
y = train['winPlacePerc']
columns_todrop = ['winPlacePerc']
train_sel = train.drop(columns_todrop,axis=1 )
train = df_enc[:ntrain]
test = df_enc[ntrain:]<prepare_x_and_y>
train_mean = train_sel.groupby(['matchId','groupId'] ).mean()
test_mean = test.groupby(['matchId','groupId'] ).mean()
train_median = train_sel.groupby(['matchId','groupId'] ).median()
test_median = test.groupby(['matchId','groupId'] ).median()
train_rank = train_mean.groupby('matchId' ).rank(pct=True)
 test_rank = test_mean.groupby('matchId' ).rank(pct=True)
X_test = test
X_train = train<normalization>
train_new = pd.merge(train_sel,train_mean,suffixes=['',"_mean"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test,test_mean,suffixes=['',"_mean"],how="left",on=['matchId','groupId'])
 train_new = pd.merge(train_new,train_rank,suffixes=['',"_rank"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test_new,test_rank,suffixes=['',"_rank"],how="left",on=['matchId','groupId'])
 train_new = pd.merge(train_new,train_median,suffixes=['',"_median"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test_new,test_median,suffixes=['',"_median"],how="left",on=['matchId','groupId'])
 del train_mean
del test_mean
del train_rank
del test_rank
del train_median
del test_median
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
 X_test = scaler.transform(X_test )<train_on_grid>
selected_columns=[]
for each in train_new.columns:
if "_" in each:
selected_columns.append(each)
 train_selected = train_new[selected_columns]
test_selected = test_new[selected_columns]
train_selected['matchId'] = train['matchId']
test_selected['matchId'] = test['matchId']
ran = RandomForestClassifier(random_state=1)
 knn = KNeighborsClassifier()
log = LogisticRegression()
xgb = XGBClassifier()
gbc = GradientBoostingClassifier()
svc = SVC(probability=True)
 ext = ExtraTreesClassifier()
ada = AdaBoostClassifier()
gnb = GaussianNB()
gpc = GaussianProcessClassifier()
bag = BaggingClassifier()
models = [ran, knn, log, xgb, gbc, svc, ext, ada, gnb, gpc, bag]
scores = []
for mod in models:
mod.fit(X_train, y_train)
 acc = cross_val_score(mod, X_train, y_train, scoring = "accuracy", cv = 10)
 scores.append(acc.mean() )<prepare_output>
cols_toDrop = ['Id_rank','Id_mean','Id_median','vehicleDestroys_mean','vehicleDestroys_median','vehicleDestroys_mean']
train_selected.drop(cols_toDrop,axis=1,inplace=True)
 test_selected.drop(cols_toDrop,axis=1,inplace=True )
results = pd.DataFrame({
'Model': ['Random Forest', 'K Nearest Neighbour', 'Logistic Regression', 'XGBoost', 'Gradient Boosting', 'SVC', 'Extra Trees', 'AdaBoost', 'Gaussian Naive Bayes', 'Gaussian Process', 'Bagging Classifier'],
'Score': scores})
 result_df = results.sort_values(by='Score', ascending=False ).reset_index(drop=True)
 result_df.head(11 )<create_dataframe>
train_selected['winPlacePerc'] = y
matchId = train_selected['matchId'].unique()
matchIdTrain = np.random.choice(matchId, int(0.80*len(matchId)))
 df_train2 = df_train[train_selected['matchId'].isin(matchIdTrain)]
df_test = df_train[~train_selected['matchId'].isin(matchIdTrain)]
y_train = df_train2['winPlacePerc']
X_train = df_train2.drop(columns=['winPlacePerc'])
 y_test = df_test['winPlacePerc']
X_test = df_test.drop(columns=['winPlacePerc'])
fi = {'Features':train.columns.tolist() , 'Importance':xgb.feature_importances_}
importance = pd.DataFrame(fi, index=None ).sort_values('Importance', ascending=False )<create_dataframe>
scaler = MinMaxScaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 model = LinearRegression()
regr = make_pipeline(model)
 model.fit(X_train,y_train)
fi = {'Features':train.columns.tolist() , 'Importance':np.transpose(log.coef_[0])}
importance = pd.DataFrame(fi, index=None ).sort_values('Importance', ascending=False )<create_dataframe>
y_pred = model.predict(X_test)
 print(mean_absolute_error(y_pred,y_test))
gbc_imp = pd.DataFrame({'Feature':train.columns, 'gbc importance':gbc.feature_importances_})
 xgb_imp = pd.DataFrame({'Feature':train.columns, 'xgb importance':xgb.feature_importances_})
 ran_imp = pd.DataFrame({'Feature':train.columns, 'ran importance':ran.feature_importances_})
 ext_imp = pd.DataFrame({'Feature':train.columns, 'ext importance':ext.feature_importances_})
 ada_imp = pd.DataFrame({'Feature':train.columns, 'ada importance':ada.feature_importances_})
 importances = gbc_imp.merge(xgb_imp, on='Feature' ).merge(ran_imp, on='Feature' ).merge(ext_imp, on='Feature' ).merge(ada_imp, on='Feature')
 importances['Average'] = importances.mean(axis=1)
 importances = importances.sort_values(by='Average', ascending=False ).reset_index(drop=True)
 importances<create_dataframe>
test_scaled = scaler.transform(test_selected)
 final_pred = model.predict(test_scaled )
<normalization><EOS> 
finalPred_series = pd.Series(final_pred)
 submission = pd.concat([test['Id'],finalPred_series],axis=1)
 columns=['Id','winPlacePerc']
submission.columns = columns
print(submission)
 submission.to_csv('submission.csv', index=False )