Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
models_sort = models_sort[models_sort.Model != 'VotingClassifier']
models_best = models_sort[(models_sort.acc_diff < 5)&(models_sort.acc_train > 90)]
models_best[['Model', ms + '_train', ms + '_test', 'acc_diff']].sort_values(by=['acc_test'], ascending=False )<create_dataframe>
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape
models_pred = pd.DataFrame(models_best.Model, columns = ['Model'])
 N_best_models = len(models_best.Model )<train_model>
df_train = reduce_mem_usage(df_train )
def model_fit(name_model,train,target):
if name_model == 'LGBMClassifier':
Xtrain, Xval, Ztrain, Zval = train_test_split(train, target, test_size=test_train_split_part, random_state=random_state)
 model = lgb.LGBMClassifier(n_estimators=1000)
 model.fit(Xtrain, Ztrain, eval_set=[(Xval, Zval)], early_stopping_rounds=50, verbose=False)
 else:
param_grid={}
if name_model == 'Linear Regression':
model_clf = LinearRegression()
elif name_model == 'Support Vector Machines':
model_clf = SVC()
param_grid = {'kernel': ['linear', 'poly', 'rbf', 'sigmoid'], 'tol': [1e-4]}
elif name_model == 'Linear SVC':
model_clf = LinearSVC()
param_grid = {'dual':[False],
'C': np.linspace(1, 15, 15)}
elif name_model == 'MLPClassifier':
model_clf = MLPClassifier()
param_grid = {'hidden_layer_sizes': [i for i in range(2,5)],
'solver': ['sgd'],
'learning_rate': ['adaptive'],
'max_iter': [1000]
}
elif name_model == 'Stochastic Gradient Decent':
model_clf = SGDClassifier(early_stopping=True)
 param_grid = {'alpha': [0.0001, 0.001, 0.01, 0.1, 1]}
elif name_model == 'Decision Tree Classifier':
model_clf = DecisionTreeClassifier()
param_grid = {'min_samples_leaf': [i for i in range(2,10)]}
elif name_model == 'Random Forest':
model_clf = RandomForestClassifier()
param_grid = {'n_estimators': [300, 400, 500, 600], 'min_samples_split': [60], 'min_samples_leaf': [20, 25, 30, 35, 40],
'max_features': ['auto'], 'max_depth': [5, 6, 7, 8, 9, 10], 'criterion': ['gini'], 'bootstrap': [False]}
elif name_model == 'XGBClassifier':
model_clf = xgb.XGBClassifier(objective='reg:squarederror')
 param_grid = {'n_estimators': [200, 300, 400],
'learning_rate': [0.001, 0.003, 0.005, 0.006, 0.01],
'max_depth': [4, 5, 6]}
elif name_model == 'GradientBoostingClassifier':
model_clf = GradientBoostingClassifier()
param_grid = {'learning_rate' : [0.001, 0.01, 0.1],
'max_depth': [i for i in range(2,5)],
'min_samples_leaf': [i for i in range(2,5)]}
elif name_model == 'RidgeClassifier':
model_clf = RidgeClassifier()
param_grid={'alpha': np.linspace (.1, 1.5, 15)}
elif name_model == 'BaggingClassifier':
model_base_estimator = GridSearchCV(LinearSVC() , param_grid={'dual':[False], 'C': np.linspace(1, 15, 15)},
cv=cv_train, verbose=False)
 model_base_estimator.fit(train, target)
 model_clf = BaggingClassifier(base_estimator=model_base_estimator)
 param_grid={'max_features': [0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
'n_estimators': [3, 5, 10],
'warm_start' : [True],
'random_state': [random_state]}
elif name_model == 'ExtraTreesClassifier':
model_clf = ExtraTreesClassifier()
param_grid={'min_samples_leaf' : [10, 20, 30, 40, 50]}
elif name_model == 'AdaBoostClassifier':
model_clf = AdaBoostClassifier()
param_grid={'learning_rate' : [.01,.1,.5, 1]}
elif name_model == 'Logistic Regression':
model_clf = LogisticRegression()
param_grid={'C' : [.1,.3,.5,.7, 1]}
elif name_model == 'KNN':
model_clf = KNeighborsClassifier()
param_grid={'n_neighbors': range(2, 7)}
elif name_model == 'Naive Bayes':
model_clf = GaussianNB()
param_grid={'var_smoothing': [1e-8, 1e-9, 1e-10]}
elif name_model == 'Perceptron':
model_clf = Perceptron()
param_grid = {'penalty': [None, 'l2', 'l1', 'elasticnet']}
elif name_model == 'Gaussian Process Classification':
model_clf = GaussianProcessClassifier()
param_grid = {'max_iter_predict': [100, 200],
'warm_start': [True, False],
'n_restarts_optimizer': range(3)}
model = GridSearchCV(model_clf, param_grid=param_grid, cv=cv_train, verbose=False)
 model.fit(train, target)
 return model<train_on_grid>
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape
for i in range(N_best_models):
name_model = models_best.iloc[i]['Model']
if(name_model == 'LGBMClassifier')or(name_model == 'XGBClassifier'):
model = model_fit(name_model,train0b,target0)
 acc_metrics_calc_pred(i,model,name_model,train0b,test0b,target0)
 else:
model = model_fit(name_model,train0,target0)
 acc_metrics_calc_pred(i,model,name_model,train0,test0,target0 )<sort_values>
df_test = reduce_mem_usage(df_test )
for x in metrics_now:
xs = metrics_all[x]
models_pred[xs + '_train'] = acc_all_pred[(x-1)]
models_pred[['Model', 'acc_train']].sort_values(by=['acc_train'], ascending=False )<import_modules>
df_test_id = pd.DataFrame(index=df_test.index )
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier, Pool, cv
import hyperopt<load_from_csv>
part_train = 0.05
part_valid = 0.05
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train_size = train.shape[0]
test_size = test.shape[0]
data = pd.concat([train, test] )<feature_engineering>
match_ids = df_train['matchId'].unique()
match_ids_train = np.random.choice(match_ids, int(part_train * len(match_ids)))
 len(match_ids_train )
data['Title'] = data['Name'].str.extract('([A-Za-z]+)\.', expand=False )<feature_engineering>
df_train_train = df_train[df_train['matchId'].isin(match_ids_train)]
df_train_train.shape[0]
age_ref = data.groupby('Title' ).Age.mean()
data['Age'] = data.apply(lambda r: r.Age if pd.notnull(r.Age)else age_ref[r.Title] , axis=1)
 del age_ref<feature_engineering>
match_ids_valid = np.random.choice(np.setdiff1d(match_ids, match_ids_train), int(part_valid * len(match_ids)))
 len(match_ids_valid )
data.loc[(data.PassengerId==1044, 'Fare')] = 14.43<data_type_conversions>
del df_train
del match_ids
del match_ids_train
data['Embarked'] = data['Embarked'].fillna('S')
 data['Cabin'] = data['Cabin'].fillna('Undefined' )<prepare_x_and_y>
def feature_engineering(df, is_train=True):
df['rankPoints'] = np.where(df['rankPoints'] <= 0, 0, df['rankPoints'])
 features = list(df.columns)
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 if 'winPlacePerc' in features:
features.remove('winPlacePerc')
 y = None
if is_train:
y = df.groupby(['matchId','groupId'])['winPlacePerc'].agg('mean')
 elif 'winPlacePerc' in df.columns:
y = df['winPlacePerc']
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'])
 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'])
 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'])
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 del agg, agg_rank
return df_out, y
cols = [
'Pclass',
'Name',
'Sex',
'Age',
'SibSp',
'Parch',
'Ticket',
'Fare',
'Cabin',
'Embarked'
]
X_train = data[:train_size][cols]
Y_train = data[:train_size]['Survived'].astype(int)
 X_test = data[train_size:][cols]
categorical_features_indices = [0,1,2,6,8,9]
X_train.head()<create_dataframe>
%%time
fe_x_train, fe_y_train = feature_engineering(df_train_train, is_train=True )
train_pool = Pool(X_train, Y_train, cat_features=categorical_features_indices )<choose_model_class>
del df_train_train
<train_on_grid>
%%time
fe_x_test, t = feature_engineering(df_test, is_train=False )
model = CatBoostClassifier(
 depth=3,
iterations=300,
eval_metric='Accuracy',
random_seed=42,
logging_level='Silent',
allow_writing_files=False 
)
 cv_data = cv(
 train_pool,
model.get_params() ,
fold_count=5 
)
 print('Best validation accuracy score: {:.2f}±{:.2f} on step {}'.format(
 np.max(cv_data['test-Accuracy-mean']),
cv_data['test-Accuracy-std'][cv_data['test-Accuracy-mean'].idxmax(axis=0)],
cv_data['test-Accuracy-mean'].idxmax(axis=0)
))
print('Precise validation accuracy score: {}'.format(np.max(cv_data['test-Accuracy-mean'])))
 model.fit(train_pool);
model.score(X_train, Y_train )<features_selection>
del df_test
feature_importances = model.get_feature_importance(train_pool)
 feature_names = X_train.columns
for score, name in sorted(zip(feature_importances, feature_names), reverse=True):
print('{}: {}'.format(name, score))<save_to_csv>
%%time
rf = RandomForestRegressor(n_estimators=30, criterion='mae', n_jobs=-1)
 rf.fit(fe_x_train, fe_y_train)
 rf_y_pred = rf.predict(fe_x_test )
Y_pred = model.predict(X_test)
 submission = pd.DataFrame({
"PassengerId": data[train_size:]["PassengerId"],
"Survived": Y_pred.astype(int)
 })
 submission.to_csv('submission.csv', index=False )<set_options>
def fix_pred(x, pred):
updated_pred = []
for i in range(len(x)) :
winPlacePerc = pred[i]
maxPlace = int(x.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
updated_pred.append(winPlacePerc)
 return updated_pred
warnings.filterwarnings('ignore' )<load_from_csv>
%%time
fixed_rf_y_pred = fix_pred(fe_x_test, rf_y_pred )
train_set = pd.read_csv('.. /input/train.csv')
 test_set = pd.read_csv('.. /input/test.csv')
 train_set.shape, test_set.shape<concatenate>
def make_submission(x, y, filename):
submission = pd.DataFrame(index=x.index)
 submission['winPlacePerc'] = np.clip(y, a_min=0, a_max=1)
 submission.to_csv(filename, index_label='Id' )
<data_type_conversions><EOS> 
make_submission(df_test_id, fixed_rf_y_pred, 'random_forest_baseline.csv' )