Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam)
<categorify>
df_train['Id'].nunique()
all_data = pd.get_dummies(all_data)
 print(all_data.shape )<split>
df_train['groupId'].nunique()
train = all_data[:ntrain]
test = all_data[ntrain:]<import_modules>
df_train['matchId'].nunique()
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb<compute_train_metric>
df_train[df_train['groupId']==24]
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<choose_model_class>
show_count_sum(train_, 'assists')
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))<choose_model_class>
show_count_sum(train_, 'boosts' )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))<choose_model_class>
show_count_sum(train_, 'DBNOs' )
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5 )<choose_model_class>
show_count_sum(train_, 'headshotKills' )
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )<choose_model_class>
show_count_sum(train_, 'heals' )
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )<choose_model_class>
show_count_sum(train_, 'weaponsAcquired' )
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 )<compute_test_metric>
show_count_sum(train_, 'winPoints' )
score = rmsle_cv(lasso)
 print("
Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
show_count_sum(train_, 'revives' )
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
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)
score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
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 )
score = rmsle_cv(GBoost)
 print("Gradient Boosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
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 )
<compute_test_metric><EOS> 
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 )