Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
!pip install ultimate
gc.enable()
for col in ['BsmtFinType2','BsmtFinType1', 'BsmtExposure','BsmtCond','BsmtQual','Exterior2nd','Exterior1st','MasVnrType']:
all_data = fill_with_NA(all_data, col )<categorify>
INPUT_DIR = ".. /input/"
all_data = fill_with_mode(all_data, 'MSZoning' )<categorify>
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 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("get group max feature")
 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("get group min feature")
 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("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 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 = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y
all_data = fill_with_mode(all_data, 'Functional' )<categorify>
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )
all_data = fill_with_mode(all_data, 'Utilities' )<categorify>
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )
all_data = fill_with_mode(all_data, 'SaleType' )<categorify>
%%time
NN_model = Sequential()
NN_model.add(Dense(x_train.shape[1], input_dim = x_train.shape[1], activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(1, activation='linear'))
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
 NN_model.summary()
all_data = fill_with_mode(all_data, 'KitchenQual' )<categorify>
%%time
NN_model.fit(x=x_train, y=y, batch_size=1000,
epochs=30, verbose=1, callbacks=callbacks_list,
validation_split=0.15, validation_data=None, shuffle=True,
class_weight=None, sample_weight=None, initial_epoch=0,
steps_per_epoch=None, validation_steps=None)
 del x_train, y
gc.collect()
all_data = fill_with_mode(all_data, 'Electrical' )<sort_values>
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )
all_na =(all_data.isnull().sum() * 100)/ len(all_data)
 all_na = all_na.drop(all_na[all_na == 0].index ).sort_values(ascending=False)
 all_na<feature_engineering>
%%time
pred = NN_model.predict(x_test)
 del x_test
gc.collect()
all_data['ExterQual'] = all_data['ExterQual'].fillna("NA" )<feature_engineering>
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2
all_data['TotalAreaSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
all_data['AllSF'] = all_data['TotalAreaSF'] + all_data['WoodDeckSF'] + all_data['TotalBsmtSF'] + all_data['OpenPorchSF']<define_variables>
df_test = pd.read_csv(INPUT_DIR + 'test_V2.csv' )
y_log = np.log1p(y )<set_options>
%%time
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
skewed = set()
log_skewed = set()
for column in numerical_columns:
if abs(skew(all_data[column])) > 0.5:
skewed.add(column)
 if abs(skew(np.log1p(all_data[column])))> 0.5:
log_skewed.add(column)
 print("{} out of {} are skewed".format(len(skewed), len(numerical_columns)))
 print("{} are not log normal".format(len(log_skewed)))
 log_skewed<feature_engineering>
df_test['winPlacePerc'] = pred
<categorify><EOS> 
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )