Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
train_df = pd.read_csv('.. /input/train_V2.csv')
 test_df = pd.read_csv('.. /input/test_V2.csv' )
all_data = pd.get_dummies(all_data )<split>
%matplotlib inline
warnings.filterwarnings("ignore" )
train = all_data[:train_size]
test = all_data[train_size:]<compute_train_metric>
train_matchType = pd.get_dummies(train_df['matchType'])
 test_matchType = pd.get_dummies(test_df['matchType'])
 train_df2 = train_df.drop(columns=['matchType'])
 test_df2 = test_df.drop(columns=['matchType'])
 train_df2 = pd.concat([train_matchType, train_df2], axis = 1)
 test_df2 = pd.concat([test_matchType, test_df2], axis = 1 )
def rms_error(model):
return np.sqrt(-1 * cross_val_score(model, train.values, y_log.values, scoring="neg_mean_squared_error", cv = 5))<import_modules>
train_df3 = train_df2[train_df2['damageDealt'] - train_df2['damageDealt'].mean() <=(3 * train_df2['damageDealt'].std())]
train_df3 = train_df2[train_df2['walkDistance'] - train_df2['walkDistance'].mean() <=(3 * train_df2['walkDistance'].std())]
train_df3 = train_df2[train_df2['rideDistance'] - train_df2['rideDistance'].mean() <=(3 * train_df2['rideDistance'].std())]
train_df3 = train_df2[train_df2['swimDistance'] - train_df2['swimDistance'].mean() <=(3 * train_df2['swimDistance'].std())]
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.preprocessing import StandardScaler, Normalizer, MinMaxScaler, RobustScaler
from sklearn.linear_model import ElasticNet, ElasticNetCV, SGDRegressor, HuberRegressor, LassoCV, RidgeCV, Lasso
from sklearn.svm import SVR
from sklearn.kernel_ridge import KernelRidge
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor<define_search_space>
train_df3 = train_df3.dropna()
pipeline = Pipeline([('model', RandomForestRegressor())])
 random_forest_param_grid = {
"model__n_estimators": [100,200, 300, 400, 500, 600, 700],
"model__max_leaf_nodes": [ 100, 200, 300, 400, 500, 600, 650, 700, 800]
}
random_forest_grid = GridSearchCV(pipeline, cv=5, param_grid=random_forest_param_grid, n_jobs=-1 )<train_model>
id_cols = train_df3.loc[:, ["Id", "groupId", "matchId"]]
train_df4 = train_df3.drop(columns = id_cols)
 train_df4.dropna(inplace=True)
 X = train_df4.iloc[:, 0:-1]; y = train_df4.iloc[:, -1]
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=2015195017 )
random_forest_grid.fit(train, y_log )<find_best_params>
lgb_train = lgb.Dataset(X_train, label=y_train)
 lgb_val = lgb.Dataset(X_val, label=y_val)
 params = {'objective': 'regression',
'metric': 'mae',
'n_estimators': 20000,
'early_stopping_rounds': 5,
'num_leaves': 31,
'learning_rate': 0.05,
'bagging_fraction': 0.7,
'bagging_seed': 20181219,
'num_threads': 5,
'colsample_bytree': 0.7}
lgr = lgb.train(params, lgb_train, valid_sets=[lgb_train, lgb_val], early_stopping_rounds=200, verbose_eval=1000 )
random_forest_grid.best_params_<define_search_space>
test_df2 = test_df2.drop(columns=['Id', 'groupId', 'matchId'] )
pipeline = Pipeline([('imputer',SimpleImputer()),('model', GradientBoostingRegressor())])
 gradient_boosting_param_grid = {
"model__n_estimators": [ 500, 600,700,800,1000],
"model__max_depth": [2, 3, 4]
}
gradient_boosting_grid = GridSearchCV(pipeline, cv=5, param_grid=gradient_boosting_param_grid, n_jobs=-1 )<train_model>
y_pred_lgb = lgr.predict(test_df2, num_iteration=lgr.best_iteration )
gradient_boosting_grid.fit(train, y_log )<find_best_params>
submit_lgb = pd.DataFrame({'Id': test_df['Id'], "winPlacePerc": y_pred_lgb} , columns=['Id', 'winPlacePerc'])
 submit_lgb.head()
gradient_boosting_grid.best_params_<train_on_grid>
submit_lgb.to_csv("submission_lgb.csv", index=False )