Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<train_on_grid>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
gamma = [i*0.1 for i in range(0,5)]
hyperparams = {'gamma': gamma}
gd=GridSearchCV(estimator = XGBClassifier(learning_rate=0.0001, n_estimators=10, max_depth=3,
min_child_weight=1), param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
train = pd.read_csv('.. /input/train_V2.csv')
 test = pd.read_csv('.. /input/test_V2.csv' )
subsample = [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1]
colsample_bytree = [0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1]
hyperparams = {'subsample': subsample, 'colsample_bytree': colsample_bytree}
gd=GridSearchCV(estimator = XGBClassifier(learning_rate=0.0001, n_estimators=10, max_depth=3,
min_child_weight=1, gamma=0), param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
train['totalDistance']=train['rideDistance']+train['walkDistance']+train['swimDistance']
train['killsWithoutMoving']=(( train['kills']>0)&(train['totalDistance']==0))
train['headshot_rate']=train['headshotKills']/train['kills']
train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count')
 print(train.shape)
 train.drop(train[train['winPlacePerc'].isnull() ].index,inplace=True)
 train.drop(train[train['killsWithoutMoving']==True].index,inplace=True)
 train.drop(train[train['roadKills']>10].index,inplace=True)
 train.drop(train[train['kills']>35].index,inplace=True)
 train.drop(train[(( train['headshot_rate']==1)&(train['kills']>9)) ].index,inplace=True)
 train.drop(train[train['longestKill']>=1000].index,inplace=True)
 train.drop(train[train['walkDistance']>=10000].index,inplace=True)
 train.drop(train[train['rideDistance']>=20000].index,inplace=True)
 train.drop(train[train['swimDistance']>=2000].index,inplace=True)
 train.drop(train[train['weaponsAcquired']>=80].index,inplace=True)
 train.drop(train[train['heals']>=40].index,inplace=True)
 train.drop(train[(( train['totalDistance']==0)&(train['weaponsAcquired']>3)) ].index,inplace=True)
 print(train.shape )
reg_alpha = [1e-5, 1e-2, 0.1, 1, 100]
hyperparams = {'reg_alpha': reg_alpha}
gd=GridSearchCV(estimator = XGBClassifier(learning_rate=0.0001, n_estimators=10, max_depth=3,
min_child_weight=1, gamma=0, subsample=0.6, colsample_bytree=0.9),
param_grid = hyperparams, verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['headshotKillsNorm'] = train['headshotKills']*(( 100-train['playersJoined'])/100 + 1)
 train['killPlaceNorm'] = train['killPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['killPointsNorm'] = train['killPoints']*(( 100-train['playersJoined'])/100 + 1)
 train['killStreaksNorm'] = train['killStreaks']*(( 100-train['playersJoined'])/100 + 1)
 train['longestKillNorm'] = train['longestKill']*(( 100-train['playersJoined'])/100 + 1)
 train['roadKillsNorm'] = train['roadKills']*(( 100-train['playersJoined'])/100 + 1)
 train['teamKillsNorm'] = train['teamKills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['DBNOsNorm'] = train['DBNOs']*(( 100-train['playersJoined'])/100 + 1)
 train['revivesNorm'] = train['revives']*(( 100-train['playersJoined'])/100 + 1)
 train = train.drop([ 'kills', 'headshotKills', 'killPlace', 'killPoints', 'killStreaks',
'longestKill', 'roadKills', 'teamKills', 'damageDealt', 'DBNOs', 'revives'],axis=1 )
n_restarts_optimizer = [0, 1, 2, 3]
max_iter_predict = [1, 2, 5, 10, 20, 35, 50, 100]
warm_start = [True, False]
hyperparams = {'n_restarts_optimizer': n_restarts_optimizer, 'max_iter_predict': max_iter_predict, 'warm_start': warm_start}
gd=GridSearchCV(estimator = GaussianProcessClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
target='winPlacePerc'
features=list(train.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 features.remove("killsWithoutMoving")
 features.remove("headshot_rate")
 features.remove(target)
 sample = 500000
df_sample = train.sample(sample)
 x_train=df_sample[features]
y_train=df_sample[target]
n_estimators = [10, 25, 50, 75, 100, 125, 150, 200]
learning_rate = [0.001, 0.01, 0.1, 0.5, 1, 1.5, 2]
hyperparams = {'n_estimators': n_estimators, 'learning_rate': learning_rate}
gd=GridSearchCV(estimator = AdaBoostClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
random_seed=1
sample = 500000
x_train,x_val,y_train,y_val=train_test_split(x_train,y_train,test_size=0.1,random_state=random_seed)
 model=RandomForestRegressor(n_estimators=70,min_samples_leaf=3,max_features=0.5,n_jobs=-1 )
n_neighbors = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20]
algorithm = ['auto']
weights = ['uniform', 'distance']
leaf_size = [1, 2, 3, 4, 5, 10, 15, 20, 25, 30]
hyperparams = {'algorithm': algorithm, 'weights': weights, 'leaf_size': leaf_size,
'n_neighbors': n_neighbors}
gd=GridSearchCV(estimator = KNeighborsClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
%%time
model.fit(x_train,y_train )
n_estimators = [10, 25, 50, 75, 100]
max_depth = [3, None]
max_features = [1, 3, 5, 7]
min_samples_split = [2, 4, 6, 8, 10]
min_samples_leaf = [2, 4, 6, 8, 10]
hyperparams = {'n_estimators': n_estimators, 'max_depth': max_depth, 'max_features': max_features,
'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf}
gd=GridSearchCV(estimator = RandomForestClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
print('mae train:',mean_absolute_error(model.predict(x_train),y_train))
print('mae train:',mean_absolute_error(model.predict(x_val),y_val))
n_estimators = [10, 25, 50, 75, 100]
max_depth = [3, None]
max_features = [1, 3, 5, 7]
min_samples_split = [2, 4, 6, 8, 10]
min_samples_leaf = [2, 4, 6, 8, 10]
hyperparams = {'n_estimators': n_estimators, 'max_depth': max_depth, 'max_features': max_features,
'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf}
gd=GridSearchCV(estimator = ExtraTreesClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<train_on_grid>
test['totalDistance']=test['rideDistance']+test['walkDistance']+test['swimDistance']
test['headshot_rate']=test['headshotKills']/test['kills']
test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['headshotKillsNorm'] = test['headshotKills']*(( 100-test['playersJoined'])/100 + 1)
 test['killPlaceNorm'] = test['killPlace']*(( 100-test['playersJoined'])/100 + 1)
 test['killPointsNorm'] = test['killPoints']*(( 100-test['playersJoined'])/100 + 1)
 test['killStreaksNorm'] = test['killStreaks']*(( 100-test['playersJoined'])/100 + 1)
 test['longestKillNorm'] = test['longestKill']*(( 100-test['playersJoined'])/100 + 1)
 test['roadKillsNorm'] = test['roadKills']*(( 100-test['playersJoined'])/100 + 1)
 test['teamKillsNorm'] = test['teamKills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['DBNOsNorm'] = test['DBNOs']*(( 100-test['playersJoined'])/100 + 1)
 test['revivesNorm'] = test['revives']*(( 100-test['playersJoined'])/100 + 1)
 test = test.drop([ 'kills', 'headshotKills', 'killPlace', 'killPoints', 'killStreaks',
'longestKill', 'roadKills', 'teamKills', 'damageDealt', 'DBNOs', 'revives'],axis=1 )
n_estimators = [10, 15, 20, 25, 50, 75, 100, 150]
max_samples = [1, 2, 3, 5, 7, 10, 15, 20, 25, 30, 50]
max_features = [1, 3, 5, 7]
hyperparams = {'n_estimators': n_estimators, 'max_samples': max_samples, 'max_features': max_features}
gd=GridSearchCV(estimator = BaggingClassifier() , param_grid = hyperparams,
verbose=True, cv=5, scoring = "accuracy")
 gd.fit(X_train, y_train)
 print(gd.best_score_)
 print(gd.best_estimator_ )<choose_model_class>
x_test=test[features]
pred=model.predict(x_test)
 test['winPlacePerc']=pred
submission=test[['Id','winPlacePerc']]
submission.head()
<prepare_output><EOS> 
submission.to_csv('submission.csv',index=False )