Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
pipeline = Pipeline([('imputer',SimpleImputer()),('model', XGBRegressor())])
 param_grid = {
"model__n_estimators": [500,600,800, 850, 1000, 1200],
"model__learning_rate": [0.01, 0.02, 0.05,0.75, 0.1, 0.2]
}
fit_params = {"model__eval_set": [(train.as_matrix() , y_log.as_matrix())],
"model__early_stopping_rounds": 10,
"model__verbose": False}
xgb_grid = GridSearchCV(pipeline, cv=5, param_grid=param_grid, fit_params = fit_params, n_jobs=32 )<train_model>

 gc.enable()
xgb_grid.fit(train, y_log )<find_best_params>
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = pd.read_csv(".. /input/train_V2.csv")
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv(".. /input/test_V2.csv")
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
print("get target")
 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 sum feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('sum')
 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=["_sum", "_sum_rank"], how='left', on=['matchId', 'groupId'])
 df_out=reduce_mem_usage(df_out)
 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'])
 df_out=reduce_mem_usage(df_out)
 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=reduce_mem_usage(df_out)
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out)
 feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names
xgb_grid.best_params_<choose_model_class>
x_train, y, feature_names = feature_engineering(True )
pipeline_list = [
Pipeline([('scaler', RobustScaler()),('model', ElasticNetCV(alphas = [4, 1, 0.1, 0.001, 0.0005, 5e-4], l1_ratio = [1, 0.1, 0.001, 0.005])) ]),
Pipeline([('scaler', RobustScaler()),('model', RidgeCV(alphas = [10, 1, 0.1, 0.001, 0.0005, 5e-4])) ]),
Pipeline([('scaler', RobustScaler()),('model', LassoCV(alphas = [1, 0.1, 0.001, 0.0005, 5e-4], max_iter=50000)) ]),
Pipeline([('model', XGBRegressor(learning_rate=0.05, n_estimators=800)) ]),
Pipeline([('model', GradientBoostingRegressor(max_depth=2, n_estimators=600, loss='huber')) ]),
Pipeline([('scaler', RobustScaler()),('model', KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5)) ]),
Pipeline([('model', LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=600,
max_bin = 50, bagging_fraction = 0.6,
bagging_freq = 5, feature_fraction = 0.25,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf = 6, min_sum_hessian_in_leaf = 11)) ])
 ]
model_names = ['elasticNet','ridge','lasso','XGB','GBR','KernelRidge','LGBM']<compute_test_metric>
X_train, X_val, y_train, y_val = train_test_split(x_train, y, test_size=0.33, random_state=42)
error_list =list()
for index, pipeline in enumerate(pipeline_list):
error_list.append(rms_error(pipeline ).mean() )<create_dataframe>
warnings.filterwarnings("ignore")
 color = sns.color_palette()
errors = pd.DataFrame({
'models': model_names,
'error':error_list
})
 errors.set_index('models', inplace=True)
 errors<prepare_output>
train_data=lgb.Dataset(X_train, label=y_train)
 val_data= lgb.Dataset(X_val, label=y_val)
 params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 1500,
'max_depth':12,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9,
'verbose':50,
'early_stopping_rounds':100
}
params['metric'] = 'auc'
light_reg= lgb.train(params, train_data, valid_sets=val_data, num_boost_round=5000, early_stopping_rounds=100)
 y_pred=light_reg.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))
multiple = pd.DataFrame()
for index, pipeline in enumerate(pipeline_list):
pipeline.fit(train, y_log)
 multiple[index] = pipeline.predict(test )<define_variables>
y_pred=light_reg.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))
del X_val
del y_val
averaged_predictions = np.expm1(multiple.mean(axis=1))<train_model>
x_test, y_test, feature_names = feature_engineering(False )
class StackingModel(BaseEstimator, TransformerMixin, RegressorMixin):
def __init__(self, base_models, meta_model):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = max(len(base_models), 2)
 def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 k_folds = KFold(n_splits = self.n_folds, shuffle=True, random_state = 46)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for ix, model in enumerate(self.base_models):
for train_ix, test_ix in k_folds.split(X, y):
m = clone(model)
 m.fit(X[train_ix], y[train_ix])
 self.base_models_[ix].append(m)
 y_predicted = m.predict(X[test_ix])
 out_of_fold_predictions[test_ix, ix] = y_predicted
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
return self.meta_model_.predict(
 np.column_stack(
 [
np.column_stack(model.predict(X)for model in models ).mean(axis=1)
 for models in self.base_models_
] 
)
 )<compute_train_metric>
y_test_pred=light_reg.predict(x_test )
rms_error(StackingModel(base_models=pipeline_list, meta_model=Lasso(alpha =0.0005, random_state=1)) ).mean()<train_model>
df=pd.read_csv(".. /input/test_V2.csv")
 var=pd.DataFrame(columns=['Id','winPlacePerc'])
 var['Id']= df['Id']
var['winPlacePerc'] = y_test_pred
submission = var[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )