Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
data.dtypes.value_counts()<set_options>
e_labels = ['matchId','groupId','killPlace', 'boosts', 'walkDistance', 'weaponsAcquired', 'damageDealt', 'heals',
'kills', 'longestKill', 'killStreaks', 'rideDistance', 'winPlacePerc', 'matchType']
variables = ['killPlace', 'boosts', 'walkDistance', 'weaponsAcquired', 'damageDealt', 'heals',
'kills', 'longestKill', 'killStreaks', 'rideDistance', 'winPlacePerc']
labels = [
'killPlace_g_norm', 'boosts_g_norm', 'walkDistance_g_norm',
'weaponsAcquired_g_norm', 'damageDealt_g_norm', 'heals_g_norm',
'kills_g_norm', 'longestKill_g_norm', 'killStreaks_g_norm',
'rideDistance_group', 'duo', 'duo-fpp', 'solo', 'solo-fpp', 'squad',
'squad-fpp']
def feature_engineering(pubg_data):

 pubg_data = pubg_data[e_labels]
pubg_group_means = pubg_data.groupby(['matchId','groupId'] ).mean().reset_index()
pubg_match_means = pubg_data.groupby(['matchId'] ).mean().reset_index()
pubg_engineered = pd.merge(pubg_data, pubg_group_means,
suffixes=["", "_group"], how = "left", on = ['matchId', 'groupId'])
 pubg_engineered = pd.merge(pubg_engineered, pubg_match_means,
suffixes=["_player", "_match"], how = "left", on = ['matchId'])
 for variable in variables:
pubg_engineered[variable+'_norm'] = pubg_engineered[variable+'_player']/(pubg_engineered[variable+'_match']+0.1)
 for variable in variables:
pubg_engineered[variable+'_g_norm'] = pubg_engineered[variable+'_group']/(pubg_engineered[variable+'_match']+0.1)
 one_hot = pd.get_dummies(pubg_engineered['matchType'])
 pubg_engineered = pubg_engineered.drop('matchType',axis = 1)
 pubg_engineered = pubg_engineered.join(one_hot)
 pubg_data = pubg_engineered.reset_index(drop=True)
 pubg_x = pubg_data[labels]
pubg_x = pubg_x.clip(lower=None, upper= pubg_x.quantile(0.99), axis = 1)
 pubg_y = pubg_data['winPlacePerc_player']
return pubg_x, pubg_y
pd.set_option('display.max_columns', 100)
 data[data.select_dtypes(['int64'] ).columns].head()<data_type_conversions>
pubg_data_all = pd.read_csv(".. /input/train_V2.csv")
 pubg_data = pubg_data_all.dropna()
data[['MSSubClass', 'MoSold','OverallQual','OverallCond','YrSold']] = data[['MSSubClass', 'MoSold','OverallQual','OverallCond','YrSold']].astype(str )<data_type_conversions>
pubg_x, pubg_y = feature_engineering(pubg_data )
data[['LotFrontage', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath',
'BsmtHalfBath', 'GarageYrBlt', 'GarageCars',
'GarageArea']] = data[['LotFrontage', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath',
'BsmtHalfBath', 'GarageYrBlt', 'GarageCars', 'GarageArea']].astype(int)
<predict_on_test>
scaler = skl.preprocessing.StandardScaler().fit(pubg_x)
 pubg_x = pd.DataFrame(scaler.transform(pubg_x), columns= labels)
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
model_rf = RandomForestRegressor(n_estimators=32, oob_score=False, random_state=0, n_jobs = -1, verbose = 2)
 model_rf.fit(pubg_x, pubg_y)
 predict_train_rf = model_rf.predict(pubg_x)
 print('Mean absolute error for the training set using random forest regressor model %.4f' %
mean_absolute_error(pubg_y, np.clip(predict_train_rf, 0, 1)) )
env_cols =['MSZoning','Street','Alley','Neighborhood','Condition1','Condition2']<categorify>
del(pubg_data)
 del(pubg_data_all)
 del(pubg_x)
 del(pubg_y)
 del(predict_train_rf )
mean_encoder(data,['MSZoning','Street','Alley','Neighborhood'])
<feature_engineering>
pubg_data_test = pd.read_csv('.. /input/test_V2.csv')
 pubg_data_test['winPlacePerc'] = 0
data['ConditionFeedr'] =(data.Condition2=="Feedr")*1+(data.Condition1=="Feedr")*1
data['ConditionArtery'] =(data.Condition2=="Artery")*1+(data.Condition1=="Artery")*1
data['ConditionPosA'] =(data.Condition2=="PosA")*1+(data.Condition1=="PosA")*1
data['ConditionPosN'] =(data.Condition2=="PosN")*1+(data.Condition1=="PosN")*1
data['ConditionRRAn'] =(data.Condition2=="RRAn")*1+(data.Condition1=="RRAn")*1
data['ConditionRRAe'] =(data.Condition2=="RRAe")*1+(data.Condition1=="RRAe")*1
data['ConditionRRNn'] =(data.Condition2=="RRNn")*1+(data.Condition1=="RRNn")*1
data['ConditionRRNe'] =(data.Condition2=="RRNe")*1+(data.Condition1=="RRNe")*1
data = data.drop(['Condition1','Condition2'],axis=1 )<predict_on_test>
pubg_x, pubg_y = feature_engineering(pubg_data_test )
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
pubg_x = pd.DataFrame(scaler.transform(pubg_x), columns= labels)
 predict_test_rf = model_rf.predict(pubg_x )
lot_cols =['LotFrontage','LotArea','LotShape','LandContour','LotConfig','LandSlope','Fence','Fireplaces','FireplaceQu','GarageType','GarageYrBlt','GarageFinish','GarageCars','GarageArea','GarageQual','GarageCond','PoolArea','PoolQC']<define_variables>
predict_test_rf_clip = np.clip(predict_test_rf, 0, 1 )
data['LotShape'].dtype=='object'<categorify>
predict_test_rf_df = pd.DataFrame(data= predict_test_rf_clip, columns=['winPlacePerc'])
 output_df = pd.merge(pubg_data_test["Id"].to_frame() ,predict_test_rf_df['winPlacePerc'].to_frame() , left_index=True, right_index=True)
 output_df.head()
output_df.to_csv("submission2.csv", index = False, index_label=False )