Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
from sklearn import metrics, tree
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, f1_score, mean_squared_error, r2_score
from xgboost import XGBRegressor<choose_model_class>
np.random.seed(7777 )
XGB = XGBRegressor(n_jobs=-1)
 param_grid = {
'colsample_bytree': np.linspace(0.3, 0.8, 1.0),
'n_estimators':[40 ,80, 150, 200,240, 300],
'max_depth': [1,2, 3,5, 8]
}
CV_XGB = GridSearchCV(estimator=XGB, param_grid=param_grid, cv= 10 )<train_on_grid>
train_df = pd.read_csv(".. /input/train_V2.csv")
 test_df = pd.read_csv(".. /input/test_V2.csv" )
%time CV_XGB.fit(X_train, y_train )<find_best_params>
matchTyp = ['squad-fpp', 'duo', 'solo-fpp', 'squad', 'duo-fpp', 'solo',
'normal-squad-fpp', 'crashfpp', 'flaretpp', 'normal-solo-fpp',
'flarefpp', 'normal-duo-fpp', 'normal-duo', 'normal-squad',
'crashtpp', 'normal-solo']
mapping = {}
for i, j in enumerate(matchTyp):
mapping[i] = j
train_df["matchTypeMap"] = train_df["matchType"].apply(lambda x: ''.join(str(i)for i, j in mapping.items() if x == j)).map(np.int64)
 test_df["matchTypeMap"] = test_df["matchType"].apply(lambda x: ''.join(str(i)for i, j in mapping.items() if x == j)).map(np.int64 )
best_xgb_model = CV_XGB.best_estimator_
print(CV_XGB.best_score_, CV_XGB.best_params_ )<predict_on_test>
train_df.drop(["matchType"], axis =1, inplace=True)
 test_df.drop(["matchType"], axis =1, inplace=True)
 train_df.dropna(inplace = True)
 train_df.isnull().any().any()
pred_train_xgb = best_xgb_model.predict(X_train)
 pred_test_xgb = best_xgb_model.predict(test_data )<compute_test_metric>
X = train_df.drop(["Id", "groupId", "matchId", "winPlacePerc"], axis = 1)
 y = train_df["winPlacePerc"]
test = test_df.drop(["Id", "groupId", "matchId"], axis = 1 )
print(metrics.mean_squared_log_error(y_train, pred_train_xgb ).round(5))<create_dataframe>
X_train_s, X_val_s, y_train_s, y_val_s = train_test_split(X, y, test_size = 0.5)
 del X
del y
y_pred_test_xgb = pd.DataFrame(pred_test_xgb, columns = ['SalePrice'])
 y_pred_test_xgb['Id'] = test['Id']<save_to_csv>
params = {
'learning_rate': 0.3,
'num_leaves': 20,
'feature_fraction': 0.9,
'min_data_in_leaf': 100,
'lambda_l2': 4,
'objective': 'regression_l2',
'metric': 'mae',
'seed': 123}
lgb_dataset = lightgbm.Dataset(X_train_s, y_train_s)
 lgb_valid = lightgbm.Dataset(X_val_s, y_val_s)
 lgb_model = lightgbm.train(params, lgb_dataset, num_boost_round=10000, valid_sets = lgb_valid, early_stopping_rounds=30, verbose_eval=100)
 pred1 = lgb_model.predict(X_val_s )
filename = 'House_Pricing_XGB.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )<set_options>
kfold = 15
skf = KFold(n_splits=kfold, random_state=42)
 pred2 = pd.DataFrame()
pred2['winPlacePerc'] = np.zeros(len(X_val_s))
%matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 GradientBoostingClassifier, ExtraTreesClassifier)
<load_from_csv>
cat_model = CatBoostRegressor(iterations=50, depth=3, learning_rate=0.1, loss_function='RMSE')
 cat_model.fit(X_train_s, y_train_s)
 pred3 = cat_model.predict(X_val_s )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 PassengerId = test['PassengerId']
train.head(3 )<feature_engineering>
xgb_model = xgboost.XGBRegressor(max_depth=11)
 for i,(train_index, test_index)in enumerate(skf.split(X_train_s, y_train_s)) :
X_train, X_valid = X_train_s.iloc[train_index], X_train_s.iloc[test_index]
y_train, y_valid = y_train_s.iloc[train_index], y_train_s.iloc[test_index]
xgb_model.fit(X_train, y_train, eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='mae', early_stopping_rounds=100)
 vtest = xgb_model.predict(X_val_s)
 pred2['winPlacePerc'] += vtest/kfold
full_data = [train, test]
train['Name_length'] = train['Name'].apply(len)
 test['Name_length'] = test['Name'].apply(len)
 train['Has_Cabin'] = train["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 test['Has_Cabin'] = test["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 for dataset in full_data:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for dataset in full_data:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
for dataset in full_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S')
 for dataset in full_data:
dataset['Fare'] = dataset['Fare'].fillna(train['Fare'].median())
 train['CategoricalFare'] = pd.qcut(train['Fare'], 4)
 for dataset in full_data:
age_avg = dataset['Age'].mean()
age_std = dataset['Age'].std()
age_null_count = dataset['Age'].isnull().sum()
age_null_random_list = np.random.randint(age_avg - age_std, age_avg + age_std, size=age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 train['CategoricalAge'] = pd.cut(train['Age'], 5)
 def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in full_data:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in full_data:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 for dataset in full_data:
dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] 						 = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] 							 = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 dataset.loc[ dataset['Age'] <= 16, 'Age'] 					 = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4 ;<drop_column>
test_pred_lgm = lgb_model.predict(test)
 test_pred_xgb = xgb_model.predict(test)
 test_pred_cat = cat_model.predict(test )
drop_elements = ['PassengerId', 'Name', 'Ticket', 'Cabin', 'SibSp']
train = train.drop(drop_elements, axis = 1)
 train = train.drop(['CategoricalAge', 'CategoricalFare'], axis = 1)
 test = test.drop(drop_elements, axis = 1 )<train_model>
stack_valid = np.column_stack(( pred1, pred2['winPlacePerc'], pred3))
test_pred = np.column_stack(( test_pred_lgm, test_pred_xgb, test_pred_cat))
ntrain = train.shape[0]
ntest = test.shape[0]
SEED = 0
NFOLDS = 5
kf = KFold(ntrain, n_folds= NFOLDS, random_state=SEED)
 class SklearnHelper(object):
def __init__(self, clf, seed=0, params=None):
params['random_state'] = seed
self.clf = clf(**params)
 def train(self, x_train, y_train):
self.clf.fit(x_train, y_train)
 def predict(self, x):
return self.clf.predict(x)
 def fit(self,x,y):
return self.clf.fit(x,y)
 def feature_importances(self,x,y):
print(self.clf.fit(x,y ).feature_importances_)
<predict_on_test>
stack_model = LinearRegression()
stack_model.fit(stack_valid, y_val_s)
 test_stack_model = stack_model.predict(test_pred )
def get_oof(clf, x_train, y_train, x_test):
oof_train = np.zeros(( ntrain,))
oof_test = np.zeros(( ntest,))
oof_test_skf = np.empty(( NFOLDS, ntest))
for i,(train_index, test_index)in enumerate(kf):
x_tr = x_train[train_index]
y_tr = y_train[train_index]
x_te = x_train[test_index]
clf.train(x_tr, y_tr)
 oof_train[test_index] = clf.predict(x_te)
 oof_test_skf[i, :] = clf.predict(x_test)
 oof_test[:] = oof_test_skf.mean(axis=0)
 return oof_train.reshape(-1, 1), oof_test.reshape(-1, 1 )<init_hyperparams>
test_id = test_df["Id"].map(str)
 submit_xg = pd.DataFrame({'Id': test_id, "winPlacePerc": test_stack_model} , columns=['Id', 'winPlacePerc'] )
<choose_model_class><EOS> 
submit_xg.to_csv("submission.csv", index = False )