Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import scipy.stats as sc
import gc
import warnings
X_train_df['Age'] = X_train_df['Age'].apply(Age_cat)
 X_test_df['Age'] = X_test_df['Age'].apply(Age_cat )<feature_engineering>
plt.rcParams['figure.figsize'] = 15,8
sns.set(rc={'figure.figsize':(15,8)})
 pd.options.display.float_format = '{:.2f}'.format
warnings.filterwarnings('ignore')
 gc.enable()
X_train_df['With_someone'] = X_train_df['SibSp'] | X_train_df['Parch']
X_test_df['With_someone'] = X_test_df['SibSp'] | X_test_df['Parch']
X_train_df['Family'] = X_train_df['SibSp'] + X_train_df['Parch']+1
X_test_df['Family'] = X_test_df['SibSp'] + X_test_df['Parch']+1<feature_engineering>
train = pd.read_csv('.. /input/train_V2.csv')
 test = pd.read_csv('.. /input/test_V2.csv')
 train.head()
X_train_df['With_someone'] =X_train_df['With_someone'].apply(lambda x:1 if x >=1 else 0)
 X_test_df['With_someone'] =X_test_df['With_someone'].apply(lambda x:1 if x >=1 else 0 )<count_values>
display(train[train.isnull().any(1)])
 display(test[test.isnull().any(1)] )
mod = X_train_df.Embarked.value_counts().argmax()
X_train_df.Embarked.fillna(mod, inplace=True )<prepare_x_and_y>
train.drop(2744604, inplace=True )
fare_med = train_df.Fare.median()
X_test_df.Fare.fillna(fare_med, inplace=True )<count_missing_values>
print(sc.normaltest(train['winPlacePerc']))
print('Skew: ', sc.skew(train['winPlacePerc']))
X_train_df.isnull().sum()<count_missing_values>
print("Number of match in train dataset:",train['matchId'].nunique() )
X_test_df.isnull().sum()<categorify>
def _min(x):
return x.value_counts().values.min()
def _max(x):
return x.value_counts().values.max()
def _avg(x):
return x.value_counts().values.mean()
def _med(x):
return np.median(x.value_counts().values)
 def _num(x):
return x.nunique()
infostat = train.groupby('matchType' ).agg({
"matchId": [np.size, _num, _min,_med,_max],
"groupId": [_min,_med,_max],
"matchDuration": [min,np.median, max],
"maxPlace": [min,np.median,max],
"numGroups":[min,np.median,max]
} ).sort_values(by=('matchId','size'),ascending=False)
 display(infostat )
X_train_df.replace({"male": 0, "female": 1}, inplace=True)
 X_test_df.replace({"male": 0, "female": 1}, inplace=True)
 X_train_df.replace({"S": 0, "C": 1, "Q": 2}, inplace=True)
 X_test_df.replace({"S": 0, "C": 1, "Q": 2}, inplace=True )<categorify>
import lightgbm as lgb
X_train_df = pd.get_dummies(X_train_df, columns=['Pclass', 'Embarked','Age','Title'], drop_first=True)
 X_test_df = pd.get_dummies(X_test_df, columns=['Pclass', 'Embarked','Age','Title'], drop_first=True)
 X_train_df.head()<drop_column>
def initial_preparing(df, Debug):
if Debug:
df = df[df['matchId'].isin(df['matchId'].unique() [:2000])]
df.drop(columns=['killPoints','rankPoints','winPoints','matchType','maxPlace','Id'],inplace=True)
 X = df.groupby(['matchId','groupId'] ).agg(np.mean)
 X = reduce_mem_usage(X)
 y = X['winPlacePerc']
X.drop(columns=['winPlacePerc'],inplace=True)
 X_ranked = X.groupby('matchId' ).rank(pct=True)
 X = X.reset_index() [['matchId','groupId']].merge(X_ranked, how='left', on=['matchId', 'groupId'])
 X.drop(['matchId','groupId'],axis=1, inplace=True)
 X = reduce_mem_usage(X)
 return X, y
X_train_df = X_train_df.drop(columns=['SibSp','Parch'])
 X_test_df = X_test_df.drop(columns=['SibSp','Parch'] )<normalization>
X_train, y = initial_preparing(train.copy() ,False )
sc_X = MinMaxScaler()
X_train_df[['Fare','Family']] = sc_X.fit_transform(X_train_df[['Fare','Family']])
 X_test_df[['Fare','Family']] = sc_X.transform(X_test_df[['Fare','Family']] )<import_modules>
X_train, X_holdout, y_train, y_holdout = train_test_split(X_train, y, test_size=0.2, random_state=666 )
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import RandomForestClassifier<define_search_space>
from sklearn.model_selection import cross_val_score
import sklearn.metrics
from sklearn.model_selection import GridSearchCV
logi_clf = LogisticRegression(random_state=0)
 logi_parm = {"penalty": ['l1', 'l2'], "C": [0.1, 0.5, 1, 5, 10, 50]}
svm_clf = SVC(random_state=0)
 svm_parm = {'kernel': ['rbf', 'poly'], 'C': [0.1, 0.5, 1, 5, 10, 50], 'degree': [3, 5, 7],
'gamma': ['auto', 'scale']}
dt_clf = DecisionTreeClassifier(random_state=0)
 dt_parm = {'criterion':['gini', 'entropy']}
knn_clf = KNeighborsClassifier()
knn_parm = {'n_neighbors':[5, 10, 15, 20], 'weights':['uniform', 'distance'], 'p': [1,2]}
gnb_clf = GaussianNB()
gnb_parm = {'priors':['None']}
clfs = [logi_clf, svm_clf, dt_clf, knn_clf]
params = [logi_parm, svm_parm, dt_parm, knn_parm]<train_on_grid>
%%time
lgtrain = lgb.Dataset(X_train, label=y_train.reset_index(drop=True))
res = lgb.cv({'metric': 'mae'},lgtrain, nfold=5,stratified=False,seed=666)
 print("Mean score:",res['l1-mean'][-1])
 gc.collect()
clf1 = RandomForestClassifier()
clf1.fit(X_train_df,y_train_df)
 rf_rand = GridSearchCV(clf1,{'n_estimators':[50,100,200,300,500],'max_depth':[i for i in range(2,11)]},cv=10)
 rf_rand.fit(X_train_df,y_train_df)
 print(rf_rand.best_score_)
 print(rf_rand.best_params_ )<train_on_grid>
team_features = {
'assists': [sum, np.mean, np.size],
'boosts' : [sum, np.var, np.mean],
'heals': [sum, np.var, np.mean],
'damageDealt': [np.var,min,max,np.mean],
'DBNOs': [np.var,max,np.mean],
'headshotKills': [max,np.mean],
'killPlaceScall':[sum, min,max, np.var, np.mean],
'kills': [ sum, max, np.var,np.mean],
'killStreaks': [max,np.var,np.mean],
'longestKill': [max, np.mean, np.var],
'revives': sum,
'rideDistance': [sum, np.mean,np.var],
'swimDistance': [np.var],
'teamKills': sum,
'vehicleDestroys': sum,
'walkDistance': [np.var,np.mean],
'weaponsAcquired': [np.mean],
'damageRate': [np.var,min,max,np.mean],
'headshotRate': [np.var,max,np.mean],
'killStreakRate': [np.var,np.max, np.mean],
'healthItems': [np.var, np.mean],
'healsOnKill': [ np.var, np.mean],
'sniper': [ np.var, np.mean],
'totalDistance': [sum, np.var, np.mean],
'totalDistancePen': [ sum ,np.var, np.mean],
'killsPerRoadDist': [np.mean],
'killsPerWalkDist': [np.mean],
'killsPerDist': [np.mean],
'distance_over_weapons': [np.mean],
'walkDistance_over_heals': [np.mean],
'skill': [np.var,np.mean]
}
clf2 = GradientBoostingClassifier()
clf2.fit(X_train_df,y_train_df)
 gb_rand = GridSearchCV(clf2,{'n_estimators':[50,100,200,300,500],'learning_rate':[0.01,0.1,1],'max_depth':[i for i in range(2,11)]},cv=10)
 gb_rand.fit(X_train_df,y_train_df)
 print(gb_rand.best_score_)
 print(gb_rand.best_params_ )<train_on_grid>
def featuring(df, isTrain, Debug):
y=None
if Debug:
df = df[df['matchId'].isin(df['matchId'].unique() [:2000])]
nplayers = df.groupby('matchId')['matchId'].transform('count')
 df['killPlaceScall'] = df['killPlace'] / nplayers
df['damageRate'] = df['kills']/(0.01*df['damageDealt'])
 df['headshotRate'] = df['headshotKills']/df['kills']
df['killStreakRate'] = df['killStreaks']/df['kills']
df['healthItems'] = df['heals'] + df['boosts']
df['healsOnKill'] = df['healthItems']/df['kills']
df['sniper'] = df['longestKill']/100*df['weaponsAcquired']
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
df['totalDistancePen'] = df['rideDistance']/5 + df["walkDistance"] + df["swimDistance"]*10
df['killsPerRoadDist'] = df['roadKills'] /(df['rideDistance']+1)
 df['killsPerWalkDist'] =(df['kills']-df['roadKills'])/(df['walkDistance']+1)
 df['killsPerDist'] = df['kills']/(df['totalDistance']+1)
 df['distance_over_weapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistance_over_heals'] = df['walkDistance']/100/df['heals']
df["skill"] = df["headshotKills"] + df["roadKills"] - df['teamKills']
df.fillna(0,inplace=True)
 df.replace(np.inf, 0, inplace=True)
 ids = df[['matchId','groupId','Id']]
df.drop(columns=['killPlace','killPoints','rankPoints','winPoints','matchType','maxPlace','Id'],inplace=True)
 tfeatures = team_features.copy()
if isTrain:
tfeatures['winPlacePerc'] = max
X = df.groupby(['matchId','groupId'] ).agg(tfeatures)
 X.fillna(0,inplace=True)
 X.replace(np.inf, 1000000, inplace=True)
 X = reduce_mem_usage(X)
 if isTrain:
y = X[('winPlacePerc','max')]
X.drop(columns=[('winPlacePerc','max')],inplace=True)
 X_ranked = X.groupby('matchId' ).rank(pct=True)
 X = X.reset_index() [['matchId','groupId']].merge(X_ranked, suffixes=["", "_rank"], how='left', on=['matchId', 'groupId'])
 ids_after = X[['matchId','groupId']]
ids_after.columns = ['matchId','groupId']
X = X.drop(['matchId','groupId'],axis=1)
 X.columns = [a+"_"+b for a,b in X.columns]
X = reduce_mem_usage(X)
 return X, y, ids,ids_after
clf3 = SVC(gamma='auto')
 clf3.fit(X_train_df,y_train_df)
 svc_rand = GridSearchCV(clf3,{'C':[5,10,15,20],'degree':[i for i in range(1,11)]},cv=10)
 svc_rand.fit(X_train_df,y_train_df)
 print(svc_rand.best_score_)
 print(svc_rand.best_params_ )<train_model>
%%time
X_train, y, _,_ = featuring(train,True,False)
 X_test, _,ids_init,ids_after = featuring(test,False,False )
clf1 = RandomForestClassifier(max_depth=6,n_estimators=200)
 clf1.fit(X_train_df,y_train_df)
 clf2 = GradientBoostingClassifier(n_estimators=300,learning_rate=0.01,max_depth=4,random_state=0)
 clf2.fit(X_train_df,y_train_df)
 clf3 = SVC(C=5,degree=1,gamma='auto',probability=True)
 clf3.fit(X_train_df,y_train_df )<choose_model_class>
X_train, X_holdout, y_train, y_holdout = train_test_split(X_train, y, test_size=0.2, random_state=666 )
eclf = VotingClassifier(estimators=[('rf',clf1),('gb',clf2),('svc',clf3)],voting='soft',weights=[2.5,2.5,2] )<train_model>
%%time
lgtrain = lgb.Dataset(X_train, label=y_train.reset_index(drop=True))
res = lgb.cv({'metric': 'mae'},lgtrain, nfold=5,stratified=False,seed=666)
 print("Mean score:",res['l1-mean'][-1] )
eclf.fit(X_train_df,y_train_df )<predict_on_test>
gridParams = {
'num_leaves': [30,50,100], 'max_depth': [-1,8,15],
'min_data_in_leaf': [100,300,500], 'max_bin': [250,500],
'lambda_l1': [0.01], 'num_iterations': [5],
'nthread': [4], 'seed': [666],
'learning_rate': [0.05], 'metric': ['mae'],
"bagging_fraction" : [0.7], "bagging_seed" : [0], "colsample_bytree" : [0.7]
}
model = lgb.LGBMRegressor()
grid = GridSearchCV(model, gridParams,
verbose=1,
cv=5 )
pred = eclf.predict(X_test_df )<create_dataframe>
%%time
grid.fit(X_train.iloc[:500000,:], y_train.iloc[:500000] )
cols = ['PassengerId', 'Survived']
submit_df = pd.DataFrame(np.hstack(( y_test_df.values.reshape(-1,1),pred.reshape(-1,1))),
columns=cols )<save_to_csv>
print("Best params:", grid.best_params_)
 print("
Best score:", grid.best_score_)
 params = grid.best_params_
submit_df.to_csv('submission.csv', index=False )<install_modules>
%%time
lgtrain = lgb.Dataset(X_train, label=y_train)
 lgval = lgb.Dataset(X_holdout, label=y_holdout)
 params['num_iterations'] = 5000
model = lgb.train(params, lgtrain, valid_sets=[lgtrain, lgval], early_stopping_rounds=200, verbose_eval=1000 )
<import_modules><EOS> 
pred_test = model.predict(X_test, num_iteration=model.best_iteration)
 ids_after['winPlacePerc'] = pred_test
predict = ids_init.merge(ids_after, how='left', on=['groupId',"matchId"])['winPlacePerc']
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 df_sub['winPlacePerc'] = predict
df_sub[["Id", "winPlacePerc"]].to_csv("submission.csv", index=False )