Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<predict_on_test>
pd.set_option('display.width', 1000)
 pd.set_option('display.max_rows', 200)
 pd.set_option('display.max_columns', 200)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1 )<compute_train_metric>
df_train['Id'].nunique()
averaged_models = AveragingModels(models =(ENet, GBoost, KRR, lasso))
score = rmsle_cv(averaged_models)
 print(" Averaged base models score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<train_model>
df_train['groupId'].nunique()
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features )<compute_train_metric>
df_train['matchId'].nunique()
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))<compute_test_metric>
df_train['matchId'].nunique()
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred)) 
<predict_on_test>
print(df_train['revives'].value_counts() )
stacked_averaged_models.fit(train.values, y_train)
 stacked_train_pred = stacked_averaged_models.predict(train.values)
 stacked_pred = np.expm1(stacked_averaged_models.predict(test.values))
print(rmsle(y_train, stacked_train_pred))<predict_on_test>
print(df_train['teamKills'].value_counts())
model_xgb.fit(train, y_train)
 xgb_train_pred = model_xgb.predict(train)
 xgb_pred = np.expm1(model_xgb.predict(test))
print(rmsle(y_train, xgb_train_pred))<predict_on_test>
print(df_train['vehicleDestroys'].value_counts() )
model_lgb.fit(train, y_train)
 lgb_train_pred = model_lgb.predict(train)
 lgb_pred = np.expm1(model_lgb.predict(test.values))
print(rmsle(y_train, lgb_train_pred))<compute_test_metric>
print(df_train['weaponsAcquired'].value_counts() )
print('RMSLE score on train data:')
 print(rmsle(y_train,stacked_train_pred*0.70 +
xgb_train_pred*0.15 + lgb_train_pred*0.15))<define_variables>
show_count_sum(train_, 'assists' )
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15 
<save_to_csv>
show_count_sum(train_, 'boosts' )
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble
sub.to_csv('submission.csv',index=False )<load_from_csv>
show_count_sum(train_, 'DBNOs' )
warnings.filterwarnings('ignore')
 main_file_path = '.. /input/train.csv'
data = pd.read_csv(main_file_path )<load_from_csv>
show_count_sum(train_, 'headshotKills' )
test_data = pd.read_csv('.. /input/test.csv')
 X_test = test_data.copy(deep=True )<prepare_x_and_y>
show_count_sum(train_, 'heals' )
data.drop(data[(data["GrLivArea"]>4000)&(data["SalePrice"]<300000)].index,inplace=True)
 X = data.drop('SalePrice', axis=1 ).drop('Id', axis=1)
 ID = data['Id']
y = data['SalePrice']<drop_column>
show_count_sum(train_, 'killPlace' )
test_ID = test_data['Id']
test_data.drop('Id', axis=1, inplace=True )<concatenate>
show_count_sum(train_, 'killPoints' )
train_size = X.shape[0]
all_data = pd.concat(( X, test_data)).reset_index(drop=True )<data_type_conversions>
show_count_sum(train_, 'kills' )
ordinals = ['YearBuilt', 'YearRemodAdd', 'GarageYrBlt', 'MoSold', 'YrSold']
for ordinal in ordinals:
all_data[ordinal] = all_data[ordinal].astype(str )<feature_engineering>
show_count_sum(train_, 'killStreaks' )
qual_dict = {None: 0, "Po": 1, "Fa": 2, "TA": 3, "Gd": 4, "Ex": 5, np.NaN:0}
all_data["ExterQual"] = all_data["ExterQual"].apply(lambda x: qual_dict[x])
 all_data["ExterCond"] = all_data["ExterCond"].apply(lambda x: qual_dict[x])
 all_data["BsmtQual"] = all_data["BsmtQual"].apply(lambda x: qual_dict[x])
 all_data["BsmtCond"] = all_data["BsmtCond"].apply(lambda x: qual_dict[x])
 all_data["HeatingQC"] = all_data["HeatingQC"].apply(lambda x: qual_dict[x])
 all_data["KitchenQual"] = all_data["KitchenQual"].apply(lambda x: qual_dict[x])
 all_data["FireplaceQu"] = all_data["FireplaceQu"].apply(lambda x: qual_dict[x])
 all_data["GarageQual"] = all_data["GarageQual"].apply(lambda x: qual_dict[x])
 all_data["GarageCond"] = all_data["GarageCond"].apply(lambda x: qual_dict[x])
 all_data["BsmtExposure"] = all_data["BsmtExposure"].apply(lambda x:
{None: 0, "No": 1, "Mn": 2, "Av": 3, "Gd": 4, np.NaN: 0}[x])
 bsmt_fin_dict = {None: 0, "Unf": 1, "LwQ": 2, "Rec": 3, "BLQ": 4, "ALQ": 5, "GLQ": 6, np.NaN:0}
all_data["BsmtFinType1"] = all_data["BsmtFinType1"].apply(lambda x: bsmt_fin_dict[x])
 all_data["BsmtFinType2"] = all_data["BsmtFinType2"].apply(lambda x: bsmt_fin_dict[x])
 all_data["Functional"] = all_data["Functional"].apply(lambda x:
{None: 0, "Sal": 1, "Sev": 2, "Maj2": 3, "Maj1": 4,
"Mod": 5, "Min2": 6, "Min1": 7, "Typ": 8, np.NaN: 0}[x])
 all_data["GarageFinish"] = all_data["GarageFinish"].apply(lambda x:
{None: 0, "Unf": 1, "RFn": 2, "Fin": 3, np.NaN: 0}[x])
 all_data["Fence"] = all_data["Fence"].apply(
 lambda x: {None: 0, "MnWw": 1, "GdWo": 2, "MnPrv": 3, "GdPrv": 4, np.NaN: 0}[x])
 all_data["YearBuilt"] = all_data["YearBuilt"]
all_data["YearRemodAdd"] = all_data["YearRemodAdd"]
all_data["GarageYrBlt"] = all_data["GarageYrBlt"]
all_data["GarageYrBlt"].fillna(0.0, inplace=True)
 all_data["MoSold"] = all_data["MoSold"]
all_data["YrSold"] = all_data["YrSold"]
all_data["LowQualFinSF"] = all_data["LowQualFinSF"]
all_data["MiscVal"] = all_data["MiscVal"]
all_data["PoolQC"] = all_data["PoolQC"].apply(lambda x: qual_dict[x])
 all_data["PoolArea"] = all_data["PoolArea"]
all_data["PoolArea"].fillna(0, inplace=True )<data_type_conversions>
show_count_sum(train_, 'maxPlace' )
for ordinal in ['MSSubClass']:
all_data[ordinal] = all_data[ordinal].astype(str )<sort_values>
show_count_sum(train_, 'numGroups' )
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<categorify>
show_count_sum(train_, 'revives' )
def fill_with_NA(df, column):
df[column] = df[column].fillna("NA")
 return df<categorify>
show_count_sum(train_, 'vehicleDestroys' )
def fill_with_mode(df, column):
df[column] = df[column].fillna(df[column].mode() [0])
 return df<categorify>
show_count_sum(train_, 'weaponsAcquired' )
all_data = fill_with_NA(all_data, 'PoolQC' )<correct_missing_values>
show_count_sum(train_, 'winPoints' )
all_data = fill_with_NA(all_data, 'MiscFeature' )<categorify>
Y =(df_train['winPlacePerc'].astype(float)).values
sum_id = df_test["Id"].values
df_train = df_train.drop(['Id','groupId','matchId','winPlacePerc'], axis = 1)
 df_test= df_test.drop(['Id','groupId','matchId'], axis = 1 )
all_data = fill_with_NA(all_data, 'Alley' )<categorify>
all_data = fill_with_NA(all_data, 'Fence' )<categorify>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
 model_lgb.fit(df_train, Y)
 lgb_pred = model_lgb.predict(df_test)
 lgb_pred[lgb_pred > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = lgb_pred
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("LGB.csv", index=False )
all_data = fill_with_NA(all_data, 'FireplaceQu' )<data_type_conversions>
dtrain = xgb.DMatrix(df_train, label=Y)
 dtest = xgb.DMatrix(df_test)
 params = {'max_depth':7,
'eta':1,
'silent':1,
'objective':'reg:linear',
'eval_metric':'rmse',
'learning_rate':0.05
}
num_rounds = 50
xb = xgb.train(params, dtrain, num_rounds)
 y_pred_xgb = xb.predict(dtest)
 y_pred_xgb[y_pred_xgb > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = y_pred_xgb
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("XGB1.csv", index=False )
all_data["LotFrontage"] = all_data['LotFrontage'].fillna(all_data['LotFrontage'].median() )<categorify>
dtrain = xgb.DMatrix(df_train, label = Y)
 dtest = xgb.DMatrix(df_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=100)
 model_xgb = xgb.XGBRegressor(n_estimators=360, max_depth=2, learning_rate=0.1)
 model_xgb.fit(df_train, Y)
 xgb_preds = model_xgb.predict(df_test)
 xgb_preds[xgb_preds > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = xgb_preds
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("XGB2.csv", index=False )
for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
all_data = fill_with_NA(all_data, col )<data_type_conversions>
sub = xgb_preds * 0.1 + y_pred_xgb * 0.65 + lgb_pred * 0.25
sub[sub > 1] = 1
test = pd.read_csv('.. /input/test.csv')
 test['winPlacePercPred'] = sub
aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test = test.merge(aux, how='left', on=['matchId','groupId'])
 subm = test[['Id','winPlacePerc']]
subm.to_csv("Stacked_1.csv", index=False )
<data_type_conversions><EOS> 
LR = LinearRegression()
LR.fit(df_train, Y)
 X_train, X_val, y_train,y_val = train_test_split(df_train,Y,test_size=0.3, random_state=42)
 print('Accuracy on training：
',LR.score(X_train, y_train))
print('Accuracy on validation：
',LR.score(X_val, y_val))
print('LinearRegression Accuracy：
',LR.score(df_train, Y))
pred = LR.predict(df_test)
 pred = pd.DataFrame({'Id':sum_id, 'winPlacePerc':pred})
 pred.to_csv('pred_Linear.csv',index=None )