Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
all_data.drop(all_data['TotalSF'].iloc[:ntrain].nlargest().index[:2], axis=0, inplace=True)
 ntrain = train.shape[0]-2<data_type_conversions>
def BuildFeature(is_train=True):
y = None
test_idx = None
if is_train:
print("Reading train.csv")
 df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/train_V2.csv")
 df = df[df['maxPlace'] > 1]
else:
print("Reading test.csv")
 df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/test_V2.csv")
 test_idx = df.Id
df = reduce_mem_usage(df)
 print("Delete Unuseful Columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 if is_train:
print("Read Labels")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("Read Group mean features")
 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("Read Group max features")
 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("Read Group min features")
 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("Read Group size features")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("Read Match mean features")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("Read Match size features")
 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)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
return X, y, feature_names, test_idx
num_to_str_columns = ['MSSubClass', 'OverallQual', 'OverallCond', 'MoSold', 'YrSold']
for col in num_to_str_columns:
all_data[col] = all_data[col].astype('str' )<categorify>
X_Train, Y_Train, train_columns, _ = BuildFeature(is_train=True)
 X_test, _, _ , test_idx = BuildFeature(is_train=False )
cat_cols = ['OverallQual', 'OverallCond', 'ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2', 'BsmtFinSF2', 'HeatingQC', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath', 'BedroomAbvGr',
'KitchenAbvGr', 'KitchenQual', 'TotRmsAbvGrd', 'Fireplaces', 'FireplaceQu', 'GarageFinish', 'GarageCars',
'GarageQual', 'GarageCond', 'PoolQC', 'Fence', 'YearBuilt', 'YearRemodAdd', 'GarageYrBlt', 'MoSold', 'YrSold']
for col in cat_cols:
label = LabelEncoder()
label.fit(list(all_data[col].values))
all_data[col] = label.transform(list(all_data[col].values))
print('Shape all_data: {}'.format(all_data.shape))
all_data.head()<drop_column>
total = X_Train.isnull().sum().sort_values(ascending= False)
 print(total )
numeric_feats = ['LotFrontage', 'LotArea', 'BsmtFinSF1', 'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal', 'TotalSF']
skewed_feats = all_data[numeric_feats].apply(lambda x: st.skew(x.dropna())).sort_values(ascending=False)
 skewness = pd.DataFrame({'Skew Before Transformation' :skewed_feats})
 skewness = skewness[abs(skewness)> 1].dropna(axis=0)
 skewed_features = skewness.index
for feat in skewed_features:
all_data[feat] = np.log1p(all_data[feat]+1)
 skewed_feats = all_data[skewed_features].apply(lambda x: st.skew(x.dropna())).sort_values(ascending=False)
 skewness['Skew After Transformation'] = skewed_feats
skewness<categorify>
total = X_test.isnull().sum().sort_values(ascending= False)
 print(total )
all_data = pd.get_dummies(all_data)
 print(all_data.shape)
 all_data.head(3 )<prepare_x_and_y>
X_Train['traveldistance']=X_Train['walkDistance_min']+X_Train['rideDistance_min']+X_Train['swimDistance_min']
X_Train['killingwithoutmoving']=(X_Train['kills_min']>0)&(X_Train['traveldistance']== 0)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['killingwithoutmoving']==True].index,axis = 0)
 X_Train.drop(X_Train[X_Train['killingwithoutmoving']==True].index,inplace=True)
 X_Train.drop(['killingwithoutmoving'],axis=1,inplace=True)
 X_Train.drop(['traveldistance'],axis=1,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['walkDistance_max']>10000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['walkDistance_max']>10000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['swimDistance_max']>2000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['swimDistance_max']>2000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['rideDistance_max']>20000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['rideDistance_max']>20000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['longestKill_max']>1000].index,axis = 0)
 X_Train.drop(X_Train[X_Train['longestKill_max']>1000].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['kills_max']>30].index,axis = 0)
 X_Train.drop(X_Train[X_Train['kills_max']>30].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['roadKills_max']>12].index,axis = 0)
 X_Train.drop(X_Train[X_Train['roadKills_max']>12].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['weaponsAcquired_max']>70].index,axis = 0)
 X_Train.drop(X_Train[X_Train['weaponsAcquired_max']>70].index,inplace=True)
 Y_Train = np.delete(Y_Train,X_Train[X_Train['heals_max']>35].index,axis = 0)
 X_Train.drop(X_Train[X_Train['heals_max']>35].index,inplace=True)
 X_Train.info()
final_y_train = all_data['SalePrice'][~all_data['SalePrice'].isnull() ]
final_X_train = all_data[all_data['Dataset_Train'] == 1].drop(['Dataset_Train', 'Dataset_Test', 'SalePrice'], axis=1)
 final_X_test = all_data[all_data['Dataset_Test'] == 1].drop(['Dataset_Train', 'Dataset_Test', 'SalePrice'], axis=1 )<compute_train_metric>
model = Sequential()
model.add(Dense(256, input_dim=X_Train.shape[1] ,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(128,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(64,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(32,kernel_initializer='normal', activation = 'tanh'))
model.add(Dense(1,kernel_initializer='normal', activation = 'relu'))
model.compile(optimizer= 'adam', loss= 'mae',metrics= ['mse'])
 model.fit(X_Train, Y_Train, batch_size= 2000, epochs = 15, verbose = 1)
 y_pred = model.predict(X_test, batch_size= 10)
 result = test_idx.reset_index(level=None, drop=False, name=None, inplace=False)
 result['index'] = y_pred
result.set_index(["Id"], inplace=True)
 result.rename(columns={'index': 'winPlacePerc'}, inplace=True)
 result.to_csv('submission.csv', index= True )
<choose_model_class><EOS>