Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
cats = ["PoolQC", "Fence", "MiscFeature", "Alley", "FireplaceQu", "MasVnrType", 'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
for col in cats:
df_total[col] = df_total[col].fillna('None' )<categorify>
nums = ['MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath', 'GarageCars', 'GarageArea']
for col in nums:
df_total[col] = df_total[col].fillna(0)
 print('Finished imputing' )<create_dataframe>
train = pd.get_dummies(train,columns=['matchType'] )
df_total_na =(df_total.isnull().sum() / len(df_total)) * 100
df_total_na = df_total_na.drop(df_total_na[df_total_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Percentage missing' :df_total_na})
 missing_data.head()<data_type_conversions>
train =train.dropna()
df_total['MSSubClass'] = df_total['MSSubClass'].apply(str)
 df_total['YrSold'] = df_total['YrSold'].astype(str)
 df_total['MoSold'] = df_total['MoSold'].astype(str )<categorify>
test = pd.get_dummies(test,columns=['matchType'] )
df_total = df_total.replace({"BsmtCond" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"BsmtExposure" : {"None" : 0, "No" : 1, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"None" : 0, "Unf" : 1, "LwQ": 1, "Rec" : 2, "BLQ" : 3,
"ALQ" : 4, "GLQ" : 5},
"BsmtFinType2" : {"None" : 0, "Unf" : 1, "LwQ": 1, "Rec" : 2, "BLQ" : 3,
"ALQ" : 4, "GLQ" : 5},
"BsmtQual" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA": 3, "Gd" : 4, "Ex" : 5},
"ExterCond" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"ExterQual" : {"Po" : 1, "Fa" : 2, "TA": 3, "Gd": 4, "Ex" : 5},
"FireplaceQu" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageCond" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"GarageQual" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
'GarageFinish' : {"None" : 0, "Unf" : 1, "RFn" : 2, "Fin" : 3},
"HeatingQC" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"KitchenQual" : {"Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"PoolQC" : {"None" : 0, "Po" : 1, "Fa" : 2, "TA" : 3, "Gd" : 4, "Ex" : 5},
"CentralAir" : {"N" : 0, "Y" : 1}} )<categorify>
y_train =train['winPlacePerc']
x_train =train.drop(['Id','groupId','matchId','winPlacePerc'],axis=1 )
df_total = df_total.replace({"Alley" : {"None" :0, "Grvl" : 1, "Pave" : 2}, "PavedDrive" : {"N" : 1, "P" : 2, "Y" : 3},
"Street" : {"Grvl" : 1, "Pave" : 2}} )<categorify>
X_train = x_train.values
Y_train = y_train.values
df_total = df_total.replace({"LandSlope" : {"Sev" : 1, "Mod" : 2, "Gtl" : 3} ,
"LotShape" : {"IR3" : 1, "IR2" : 2, "IR1" : 3, "Reg" : 4}} )<categorify>
def build_model() :
model = Sequential()
model.add(Dense(80,input_dim=X_train.shape[1],activation='relu'))
model.add(Dense(160,activation='relu'))
model.add(Dense(320,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(160,activation='relu'))
model.add(Dense(80,activation='relu'))
model.add(Dense(40,activation='relu'))
model.add(Dense(20,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
 return model
df_total = df_total.replace({"Functional" : {"Sal" : 0, "Sev" : 1, "Maj2" : 2, "Maj1" : 2, "Mod":3,
"Min2" : 4, "Min1" : 4, "Typ" : 5}} )<categorify>
k = 4
num_val_samples = len(X_train)// k
df_total = df_total.replace({"Electrical" : {"FuseP" : 1, "FuseF" : 2, "FuseA" : 3, "Mix" : 4, "SBrkr" : 5},
'Utilities': {'NoSeWa':0, "AllPub": 1}} )<categorify>
K.clear_session()
df_total['OverallQual_simple'] = df_total.OverallQual.replace({1:1, 2:1, 3:1,
4:2, 5:2, 6:2,
7:3, 8:3, 9:3, 10:3
})
 df_total['OverallCond_simple'] = df_total.OverallCond.replace({1:1, 2:1, 3:1,
4:2, 5:2, 6:2,
7:3, 8:3, 9:3, 10:3
})
 df_total['ExterQual_simple'] = df_total.ExterQual.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['ExterCond_simple'] = df_total.ExterCond.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['BsmtQual_simple'] = df_total.BsmtQual.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['BsmtCond_simple'] = df_total.BsmtCond.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['BsmtFinType1_simple'] = df_total.BsmtFinType1.replace({6:3, 5:3, 4:2, 3:2, 2:1, 1:1})
 df_total['BsmtFinType2_simple'] = df_total.BsmtFinType2.replace({6:3, 5:3, 4:2, 3:2, 2:1, 1:1})
 df_total['HeatingQC_simple'] = df_total.HeatingQC.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['KitchenQual_simple'] = df_total.KitchenQual.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['Functional_simple'] = df_total.Functional.replace({8:4, 7:3, 7:3, 6:3, 5:2,4:2, 3:1, 2:1})
 df_total['GarageQual_simple'] = df_total.GarageQual.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['GarageCond_simple'] = df_total.GarageCond.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['PoolQC_simple'] = df_total.PoolQC.replace({5:3, 4:3, 3:2, 2:2, 1:1})
 df_total['FireplaceQu_simple'] = df_total.FireplaceQu.replace({5:3, 4:3, 3:2, 2:2, 1:1} )<data_type_conversions>
num_epochs = 500
all_mae_histories = []
for i in tqdm(range(k)) :
print('processing fold
val_data = X_train[i * num_val_samples:(i + 1)* num_val_samples]
val_targets = Y_train[i * num_val_samples:(i + 1)* num_val_samples]
partial_train_data = np.concatenate([X_train[:i * num_val_samples],X_train[(i + 1)* num_val_samples:]],axis=0)
 partial_train_targets = np.concatenate([Y_train[:i * num_val_samples],Y_train[(i + 1)* num_val_samples:]],axis=0)
 model = build_model()
history = model.fit(partial_train_data, partial_train_targets,validation_data=(val_data, val_targets),epochs=num_epochs, batch_size=10000, verbose=2)
 mae_history = history.history['val_mean_absolute_error']
all_mae_histories.append(mae_history )
year = df_total[df_total['GarageYrBlt']!='None'][['YearBuilt','YearRemodAdd', 'GarageYrBlt']]
year['GarageYrBlt'] = year['GarageYrBlt'].astype(int)
 year.corr()<define_variables>
average_mae_history = [np.mean([x[i] for x in all_mae_histories])for i in range(num_epochs)]
categorical_features = df_total.select_dtypes(include = ["object"] ).columns
numerical_features = df_total.select_dtypes(exclude = ["object"] ).columns
numerical_features = numerical_features.drop("SalePrice")
 price = df_total['SalePrice']
print("Numerical features : " + str(len(numerical_features)))
 print("Categorical features : " + str(len(categorical_features)))
 df_total_num = df_total[numerical_features]
df_total_cat = df_total[categorical_features]<feature_engineering>
x_test = test.drop(['Id','groupId','matchId'],axis=1 )
skewness = df_total_num.apply(lambda x: skew(x))
skewness = skewness[abs(skewness)> 0.5]
print(str(skewness.shape[0])+ " skewed numerical features need to be transform")
 skewed_features = skewness.index
df_total_num[skewed_features] = boxcox1p(df_total_num[skewed_features], 0.15 )<concatenate>
X_test = x_test.values
df_total = pd.concat([df_total_num, df_total_cat, price], axis = 1 )<feature_engineering>
model = build_model()
model.fit(train_data, train_targets,epochs=70, batch_size=16, verbose=1)
 test_mse_score, test_mae_score = model.evaluate(test_data, test_targets )
df_total['HouseSF'] = df_total["GrLivArea"] + df_total["TotalBsmtSF"]<feature_engineering>
prediction = model.predict(X_test )
df_total["AllPorchSF"] = df_total["OpenPorchSF"] + df_total["EnclosedPorch"] + df_total["ScreenPorch"]<feature_engineering>
sample['winPlacePerc'] = prediction
<feature_engineering><EOS> 
sample.to_csv('sample_submission_v1.csv', index=False )