Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<drop_column>
sns.set(style="darkgrid")
 py.init_notebook_mode(connected=True)
 cf.go_offline()
train_data.set_index(train_data.Id,drop=True, inplace=True)
 train_data = train_data.drop(['Id'],axis=1)
 test_data.set_index(test_data.Id,drop=True, inplace=True)
 test_data = test_data.drop(['Id'],axis=1 )<concatenate>
train = pd.read_csv('.. /input/train_V2.csv' ).dropna()
test = pd.read_csv('.. /input/test_V2.csv' )
data=pd.concat([train_data,test_data],keys=['train','test'], join='inner' )<data_type_conversions>
print("There are %s matches in the training set and %s in the test set" %(train['matchId'].nunique() , test['matchId'].nunique()))
data[['Alley','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2',
'FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond',
'PoolQC','Fence','MiscFeature']]=data[['Alley','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2',
'FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond',
'PoolQC','Fence','MiscFeature']].fillna(value="NA" )<count_missing_values>
def addNbPlayersFeature(df):
df_res = df
df_res = df_res.assign(nb_players_total=df_res.groupby('matchId')['Id'].transform('count'))
df_res = df_res.assign(nb_players_team=df_res.groupby(['matchId', 'groupId'])['Id'].transform('count'))
return df_res
train = addNbPlayersFeature(train )
data.isna().sum() [lambda x: x>0]<filter>
def addNbKillsFeature(df):
df_res = df.assign(nb_kills_team = df.groupby(['matchId', 'groupId'])['kills'].transform('sum'))
return df_res
train = addNbKillsFeature(train )
data[data.MSZoning.isna() ]<count_values>
def addKillPlaceQuantileInformation(df):
df_res = df
df_res['median_kill_place_team'] = df_res.groupby(['matchId', 'groupId'])['killPlace'].transform(np.median)
 df_res['median_kill_place_all'] = df_res.groupby('matchId')['killPlace'].transform(np.median)
 df_res['delta_median_kill_place_team_all'] = df_res.median_kill_place_all - df_res.median_kill_place_team
df_res['max_kill_place_team'] = df_res.groupby(['matchId', 'groupId'])['killPlace'].transform(np.max)
 df_res['min_kill_place_team'] = df_res.groupby(['matchId', 'groupId'])['killPlace'].transform(np.min)
 return df_res
train = addKillPlaceQuantileInformation(train )
data[data.Neighborhood=="Mitchel"].MSZoning.value_counts()<count_values>
def addRankingQuantileInformation(df):
df_res = df
df_res['median_ranking_team'] = df_res.groupby(['matchId', 'groupId'])['rankPoints'].transform(np.median)
 df_res['median_ranking_all'] = df_res.groupby('matchId')['rankPoints'].transform(np.median)
 df_res['delta_median_ranking_team_all'] = df_res.median_ranking_all - df_res.median_ranking_team
return df_res
train = addRankingQuantileInformation(train )
data[data.Neighborhood=="IDOTRR"].MSZoning.value_counts()<categorify>
def addDamageDealtInformation(df):
df_res = df
df_res['mean_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.mean)
 df_res['max_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.max)
 df_res['min_damage_dealt_team'] = df_res.groupby(['matchId', 'groupId'])['damageDealt'].transform(np.min)
 df_res['mean_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.mean)
 df_res['max_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.max)
 df_res['min_damage_dealt_all'] = df_res.groupby('matchId')['damageDealt'].transform(np.min)
 return df_res
train = addDamageDealtInformation(train )
data.loc[(data.MSZoning.isna())&(data.Neighborhood=="Mitchel"),"MSZoning"]="RL"
data.loc[(data.MSZoning.isna())&(data.Neighborhood=="IDOTRR"),"MSZoning"]="RM"<feature_engineering>
def addDistanceWalked(df):
df_res = df
df_res['mean_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.mean)
 df_res['max_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.max)
 df_res['min_distance_walked_team'] = df_res.groupby(['matchId', 'groupId'])['walkDistance'].transform(np.min)
 df_res['mean_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.mean)
 df_res['max_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.max)
 df_res['min_distance_walked_all'] = df_res.groupby(['matchId'])['walkDistance'].transform(np.min)
 df_res['delta_mean_distance_walked_team_all'] = df_res.mean_distance_walked_team - df_res.mean_distance_walked_all
return df_res
train = addDistanceWalked(train )
data['LotAreaSqr']=np.sqrt(data.LotArea )<count_values>
def addWeaponsAcquired(df):
df_res = df
df_res['mean_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.mean)
 df_res['max_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.max)
 df_res['min_weapons_acquired_team'] = df_res.groupby(['matchId', 'groupId'])['weaponsAcquired'].transform(np.min)
 df_res['mean_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.mean)
 df_res['max_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.max)
 df_res['min_weapons_acquired_all'] = df_res.groupby(['matchId'])['weaponsAcquired'].transform(np.min)
 df_res['delta_mean_weapons_acquired_team_all'] = df_res.mean_weapons_acquired_team - df_res.mean_weapons_acquired_all
return df_res
train = addWeaponsAcquired(train )
data.LotShape.value_counts()<data_type_conversions>
other_features = ['assists', 'boosts', 'DBNOs', 'headshotKills', 'heals',
'revives', 'vehicleDestroys', 'killStreaks', 'roadKills']
def addOtherDeltaFeatures(df):
df_res = df
for f in other_features:
df_res['mean_%s_team' % f] = df_res.groupby(['matchId', 'groupId'])[f].transform(np.mean)
 df_res['mean_%s_all' % f] = df_res.groupby('matchId')[f].transform(np.mean)
 df_res['delta_mean_%s_team_all' % f] = df_res['mean_%s_team' % f] - df_res['mean_%s_all' % f]
return df_res
train = addOtherDeltaFeatures(train)
data['LotFrontage']=data.LotFrontage.fillna(data.LotAreaSqr )<drop_column>
corr = train[['winPlacePerc',
'delta_median_ranking_team_all',
'delta_median_kill_place_team_all',
'delta_mean_distance_walked_team_all',
'delta_mean_weapons_acquired_team_all',
'delta_mean_assists_team_all',
'delta_mean_boosts_team_all', 'delta_mean_DBNOs_team_all',
'delta_mean_headshotKills_team_all', 'delta_mean_heals_team_all',
'delta_mean_revives_team_all', 'delta_mean_vehicleDestroys_team_all',
'delta_mean_killStreaks_team_all', 'delta_mean_roadKills_team_all']].corr()
data = data.drop(['LotAreaSqr'],axis=1 )<drop_column>
train.to_pickle('train.pkl')
 test = addNbPlayersFeature(test)
 test = addNbKillsFeature(test)
 test = addRankingQuantileInformation(test)
 test = addKillPlaceQuantileInformation(test)
 test = addDistanceWalked(test)
 test = addWeaponsAcquired(test)
 test = addDamageDealtInformation(test)
 test = addOtherDeltaFeatures(test)
 test.to_pickle('test.pkl' )
data = data.drop(['Utilities'],axis=1 )<filter>
from sklearn.linear_model import LinearRegression, Ridge
data[data.Exterior1st.isna() ]<count_values>
features_selected = [
'delta_median_kill_place_team_all',
'delta_mean_distance_walked_team_all',
'delta_mean_weapons_acquired_team_all',
'delta_mean_boosts_team_all',
'delta_mean_heals_team_all',
'delta_mean_killStreaks_team_all',
'winPlacePerc'
]
data[(data.YearBuilt>1935)&(data.YearBuilt<1945)&(data.Neighborhood=='Edwards')].Exterior1st.value_counts()<data_type_conversions>
reg = LinearRegression()
data['Exterior1st']=data.Exterior1st.fillna('MetalSd')
 data['Exterior2nd']=data.Exterior2nd.fillna('MetalSd' )<filter>
train_linear = train[features_selected].dropna()
test_linear = test[[f for f in features_selected if f!='winPlacePerc'] + ['Id']]
data[data.BsmtFinSF1.isna() ]<feature_engineering>
Y_train = train_linear.winPlacePerc
X_train = train_linear.drop(columns=['winPlacePerc'], axis=1)
 reg = reg.fit(X_train, Y_train)
 first_pred = reg.predict(X_train )
data.loc[data.BsmtFinSF1.isna() ,'BsmtFinSF2' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtUnfSF' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtFullBath' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'TotalBsmtSF' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtHalfBath' ]=0.0
data.loc[data.BsmtFinSF1.isna() ,'BsmtFinSF1' ]=0.0
data.loc[data.BsmtQual=="NA",'BsmtExposure' ]="NA"
data.loc[data.BsmtQual=="NA",'BsmtFullBath' ]=0.0
data.loc[data.BsmtQual=="NA",'BsmtHalfBath' ]=0.0<filter>
np.mean(abs(first_pred-Y_train))
data[data.Electrical.isna() ]<count_values>
train = pd.read_pickle('train.pkl')
 test = pd.read_pickle('test.pkl')
data.Electrical.value_counts()<data_type_conversions>
features_selected = [
'assists',
'boosts',
'heals',
'killPlace',
'killPoints',
'kills',
'killStreaks',
'matchDuration',
'numGroups',
'rankPoints',
'teamKills',
'winPoints',
'winPlacePerc',
'nb_players_total',
'nb_players_team',
'nb_kills_team',
'median_ranking_team',
'median_ranking_all',
'median_kill_place_team',
'delta_median_ranking_team_all',
'delta_median_kill_place_team_all',
'mean_damage_dealt_team',
'max_damage_dealt_team',
'min_damage_dealt_team',
'mean_damage_dealt_all',
'max_damage_dealt_all',
'min_damage_dealt_all',
'mean_distance_walked_team',
'max_distance_walked_team',
'min_distance_walked_team',
'mean_distance_walked_all',
'max_distance_walked_all',
'min_distance_walked_all',
'max_weapons_acquired_team',
'min_weapons_acquired_team',
'mean_weapons_acquired_team',
'max_weapons_acquired_all',
'min_weapons_acquired_all',
'mean_weapons_acquired_all',
'mean_assists_all',
'mean_assists_team',
'mean_boosts_all',
'mean_boosts_team',
'mean_DBNOs_all',
'mean_DBNOs_team',
'delta_mean_heals_team_all',
'delta_mean_killStreaks_team_all',
'delta_mean_roadKills_team_all',
]
data[['Electrical']]=data[['Electrical']].fillna(value="SBrkr" )<feature_engineering>
train = train[features_selected].dropna()
test = test[[f for f in features_selected if f!='winPlacePerc'] + ['Id']]
train, valid = train_test_split(train, test_size=0.2 )
data['KitchenQual']=data['KitchenQual'].fillna(value="TA" )<feature_engineering>
Y_train = train.winPlacePerc
X_train = train.drop(columns=['winPlacePerc'], axis=1)
 Y_valid = valid.winPlacePerc
X_valid = valid.drop(columns=['winPlacePerc'], axis=1 )
data['GarageYrBlt']=data['GarageYrBlt'].fillna(data.YearBuilt )<filter>
ts = time.time()
model = XGBRegressor(
 max_depth=6,
n_estimators=800,
min_child_weight=100,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="mae",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 time.time() - ts
data[data.GarageCars.isna() ]<filter>
Y_pred = model.predict(test ).clip(0, 1 )
<feature_engineering><EOS> 
submission = pd.DataFrame({
"ID": test.Id,
"winPlacePerc": Y_pred
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))