Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
stack = StackingModel(base_models=pipeline_list, meta_model=Lasso(alpha =0.0005, random_state=1))
stack.fit(train.values, y_log.values )<choose_model_class>
%load_ext autoreload
%autoreload 2
%matplotlib inline
m_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)
 m_xgb.fit(train.values, y_log.values )<choose_model_class>
from fastai.imports import *
from fastai.structured import *
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
m_lgbm = 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)
 m_lgbm.fit(train.values, y_log.values )<predict_on_test>
df_raw = pd.read_csv('.. /input/train_V2.csv' )
answer = np.expm1(0.6 * stack.predict(test.values)+ 0.2 * m_xgb.predict(test.values)+ 0.2 * m_lgbm.predict(test.values))<create_dataframe>
df_raw.isnull().sum()
submission = pd.DataFrame({'Id': test_ID, 'SalePrice':answer} )<save_to_csv>
df_raw = df_raw.dropna()
df_raw.shape
submission.to_csv('submission_stacked.csv', index=False )<set_options>
df_raw['totalDistance'] = df_raw['rideDistance'] + df_raw['walkDistance'] + df_raw['swimDistance']
sns.set()<load_from_csv>
ranking_cats = ['killPlace', 'damageDealt', 'kills', 'walkDistance', 'rankPoints', 'weaponsAcquired', 'totalDistance']
for c in ranking_cats: df_raw[c+'_ranking'] = df_raw.groupby('matchId')[c].rank(ascending=False)
 df_raw.head()
test = pd.read_csv(".. /input/titanic/test.csv")
 train = pd.read_csv(".. /input/titanic/train.csv")
 data_cleaner = [train, test]<count_missing_values>
df_raw = df_raw.sort_values(['matchId'] )
for data in data_cleaner:
print(data.isnull().sum())
 print('
' )<create_dataframe>
train_cats(df_raw)
 df, y, nas = proc_df(df_raw, 'winPlacePerc', max_n_cat=5 )
age_ref = pd.DataFrame(data=[train.groupby('Pclass')['Age'].mean() ],columns=train['Pclass'].unique())
 age_ref<feature_engineering>
def split_vals(a,n): return a[:n].copy() , a[n:].copy()
n_valid = 444647
n_trn = len(df)-n_valid
def fill_age(pclass,age):
if pd.isnull(age):
return float(age_ref[pclass])
 else:
return age
for data in data_cleaner:
data['Age'] = train.apply(lambda x: fill_age(x['Pclass'],x['Age']), axis=1 )<categorify>
X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train.shape
def fill_fare(fare):
if pd.isnull(fare):
return train['Fare'].mean()
else:
return fare
def fill_embark(embark):
if pd.isnull(embark):
return train['Embarked'].mode().iloc[0]
else:
return embark
for data in data_cleaner:
data['Fare'] = train.apply(lambda x: fill_fare(x['Fare']), axis=1)
 data['Embarked'] = train.apply(lambda x: fill_embark(x['Embarked']), axis=1 )<drop_column>
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m):
res = [rmse(m.predict(X_train), y_train), rmse(m.predict(X_valid), y_valid),
m.score(X_train, y_train), m.score(X_valid, y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
for data in data_cleaner:
data.drop(['Cabin'],axis=1,inplace=True )<count_missing_values>
set_rf_samples(100000 )
for data in data_cleaner:
print(data.isnull().sum())
 print('
' )<feature_engineering>
m = RandomForestRegressor(n_estimators=100, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 %time m.fit(X_train, y_train)
 print_score(m )
title_list = list()
for data in data_cleaner:
for title in data['Name']:
title = title.split('.')[0].split(',')[1]
title_list.append(title)
 data['Title'] = title_list
title_list = list()<count_values>
fi = rf_feat_importance(m, df); fi[:]
for data in data_cleaner:
print(data['Title'].value_counts())
 print('
' )<count_values>
to_keep = fi[fi.imp>0.0025].cols; len(to_keep )
train['Title'] = train['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer'], 'Others')
 train['Title'].value_counts()<count_values>
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn )
test['Title'] = test['Title'].replace([ ' Don', ' Rev', ' Dr', ' Mme',' Ms', ' Major', ' Lady', ' Sir', ' Mlle', ' Col', ' Capt',
' the Countess', ' Jonkheer',' Dona'], 'Others')
 test['Title'].value_counts()<feature_engineering>
m = RandomForestRegressor(n_estimators=100, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 %time m.fit(X_train, y_train)
 print_score(m )
def get_size(df):
if df['SibSp'] + df['Parch'] + 1 == 1:
return 'Single'
if df['SibSp'] + df['Parch'] + 1 > 1:
return 'Small'
if df['SibSp'] + df['Parch'] + 1 > 4:
return 'Big'
for data in data_cleaner:
data['FamilySize'] = data.apply(get_size,axis=1)
 for data in data_cleaner:
data['IsAlone'] = 1
data['IsAlone'].loc[data['FamilySize'] != 'Single'] = 0<categorify>
drop_cols = ['totalDistance', 'walkDistance', 'groupId', 'matchId', 'Id']
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True)
 title = pd.get_dummies(train['Title'],drop_first=True)
 Pclass = pd.get_dummies(train['Pclass'],drop_first=True)
 FamilySize = pd.get_dummies(train['FamilySize'],drop_first=True)
 sex2 = pd.get_dummies(test['Sex'],drop_first=True)
 embark2 = pd.get_dummies(test['Embarked'],drop_first=True)
 title2 = pd.get_dummies(test['Title'],drop_first=True)
 Pclass2 = pd.get_dummies(test['Pclass'],drop_first=True)
 FamilySize2 = pd.get_dummies(test['FamilySize'],drop_first=True)
 for data in data_cleaner:
data.drop(['Sex','Embarked','Name','Ticket','PassengerId','Title','FamilySize'],axis=1,inplace=True)
 train = pd.concat([sex,embark,train,title,FamilySize],axis=1)
 test = pd.concat([sex2,embark2,test,title2,FamilySize2],axis=1 )<split>
df_final = df_keep.drop(drop_cols, axis=1 )
X = train.drop('Survived',axis=1)
 y = train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=101 )<normalization>
np.save('keep_cols.npy', np.array(df_final.columns))
scaler = MinMaxScaler()
scaler.fit(X_train)
 scaler.transform(X_train)
 scaler.transform(X_test)
 scaler.transform(test )<choose_model_class>
keep_cols = np.load('keep_cols.npy')
 df_keep = df[keep_cols]
logistic_model = LogisticRegression()
logistic_model.fit(X_train, y_train)
 y_pred = logistic_model.predict(X_test )<compute_test_metric>
X_train, X_valid = split_vals(df_keep, n_trn )
print(classification_report(y_test,y_pred))
print('
')
 print(confusion_matrix(y_test,y_pred))<save_to_csv>
m = RandomForestRegressor(n_estimators=50, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )
predictions = logistic_model.predict(test)
 pred_list = [int(x)for x in predictions]
test2 = pd.read_csv(".. /input/titanic/test.csv")
 output = pd.DataFrame({'PassengerId': test2['PassengerId'], 'Survived': pred_list})
 output.to_csv('Titanic_with_logistic.csv', index=False )<import_modules>
df_test = pd.read_csv('.. /input/test_V2.csv' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier 
<load_from_csv>
df_test['totalDistance'] = df_test['rideDistance'] + df_test['walkDistance'] + df_test['swimDistance']
ranking_cats = ['killPlace', 'damageDealt', 'kills', 'walkDistance', 'rankPoints', 'weaponsAcquired', 'totalDistance']
for c in ranking_cats: df_test[c+'_ranking'] = df_test.groupby('matchId')[c].rank(ascending=False)
 df_test.head()
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )<create_dataframe>
train_cats(df_test)
 df_test, y, nas = proc_df(df=df_test, y_fld=None )
train_len = len(train)
 test_copy = test.copy()<count_missing_values>
df_submit = df_test[keep_cols]
a = m.predict(df_submit)
 a = pd.Series(a)
 submission = pd.Series(( pd.read_csv('.. /input/test_V2.csv', low_memory=False)) ['Id'])
 submission = pd.concat([submission, a], axis=1)
 submission = submission.rename(columns={0:'winPlacePerc'} )
total = train.append(test)
 total.isnull().sum()<filter>
submission.to_csv('submission.csv', index=False )
total[total.Fare.isnull() ]<data_type_conversions>
submission.to_csv('submission.csv', index=False )