Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
def process_fares() :
global combined
combined['Fare'] = combined.groupby(
 ['Pclass', 'Sex'])['Fare'].apply(lambda x: x.fillna(x.median()))
status('fare')
 return combined<categorify>
def take_part_of_data(df, part):
match_ids = df['matchId'].unique()
match_ids_part = np.random.choice(match_ids, int(part * len(match_ids)))
 df = df[df['matchId'].isin(match_ids_part)]
del match_ids
del match_ids_part
def process_fare_bin(onehot='None'):
global combined
bins = [-1, 7.91, 14.454, 31, 99, 250, np.inf]
names = [0, 1, 2, 3, 4, 5]
combined['FareBin'] = pd.cut(combined['Fare'], bins,
labels=names ).astype('int')
 if onehot == 'yes':
farebin_dummies = pd.get_dummies(combined['FareBin'], prefix='FareBin')
 combined = pd.concat([combined, farebin_dummies], axis=1)
 combined.drop('FareBin', inplace=True, axis=1)
 combined.drop('Fare', inplace=True, axis=1)
 elif onehot == 'both':
farebin_dummies = pd.get_dummies(combined['FareBin'], prefix='FareBin')
 combined = pd.concat([combined, farebin_dummies], axis=1)
 combined.drop('FareBin', inplace=True, axis=1)
 else:
combined.drop('Fare', inplace=True, axis=1)
 status('FareBin')
 return combined<normalization>
def add_new_features_1(df):
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['headshotKillsOverKills'].fillna(0, inplace=True)
 df['killStreaksOverKills'] = df['killStreaks'] / df['kills']
df['killStreaksOverKills'].fillna(0, inplace=True)
 df['killsAndAssists'] = df['kills'] + df['assists']
df['assistsAndRevives'] = df['assists'] + df['revives']
def scale_fare() :
global combined
combined['Fare'] = boxcox1p(combined['Fare'],
boxcox_normmax(combined['Fare'] + 1))
status('NFareBin')
 return combined<categorify>
def add_new_features_2(df):
df['playersJoined'] = df.groupby('matchId')['matchId'].transform('count')
 df['killsAndAssistsOverPlayersJoined'] = df['killsAndAssists'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['matchDurationOverPlayersJoined'] = df['matchDuration'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['damageDealtOverPlayersJoined'] = df['damageDealt'] *(( 100 - df['playersJoined'])/ 100 + 1 )
def process_embarked() :
global combined
combined.Embarked.fillna(combined.Embarked.mode() [0], inplace=True)
 embarked_dummies = pd.get_dummies(combined['Embarked'], prefix='Embarked')
 combined = pd.concat([combined, embarked_dummies], axis=1)
 combined.drop('Embarked', axis=1, inplace=True)
 status('Embarked')
 return combined<categorify>
def add_new_features_3(df):
df['totalDistanceOverKillsAndAssists'] = df['totalDistance'] / df['killsAndAssists']
df['totalDistanceOverKillsAndAssists'].fillna(0, inplace=True)
 df['totalDistanceOverKillsAndAssists'].replace(np.inf, 0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'] = df['totalDistance'] / df['healsAndBoosts']
df['totalDistanceOverHealsAndBoosts'].fillna(0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'].replace(np.inf, 0, inplace=True )
def process_cabin() :
global combined
combined['Cabin_Informed'] = [
1 if pd.notnull(cab)else 0 for cab in combined['Cabin']
]
combined.Cabin.fillna('M', inplace=True)
 combined['Deck'] = combined['Cabin'].map(lambda c: c[0])
 combined['Deck'].replace('T', 'A', inplace=True)
 cabin_dummies = pd.get_dummies(combined['Deck'], prefix='Deck')
 combined = pd.concat([combined, cabin_dummies], axis=1)
 combined.drop('Cabin', axis=1, inplace=True)
 combined.drop('Deck', axis=1, inplace=True)
 status('Cabin')
 return combined<categorify>
def add_new_features_4(df):
df['headshotRate'] = df['kills'] / df['headshotKills']
df['killStreakRate'] = df['killStreaks'] / df['kills']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['killPlaceOverMaxPlace'] = df['killPlace'] / df['maxPlace']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['distanceOverWeapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistanceOverHeals'] = df['walkDistance'] / df['heals']
df['walkDistanceOverKills'] = df['walkDistance'] / df['kills']
df['killsPerWalkDistance'] = df['kills'] / df['walkDistance']
df["skill"] = df['headshotKills'] + df['roadKills']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
df.fillna(0, inplace=True )
def process_sex() :
global combined
combined['Sex'] = combined['Sex'].map({'male': 1, 'female': 0})
 status('Sex')
 return combined<categorify>
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
def process_pclass() :
global combined
pclass_dummies = pd.get_dummies(combined['Pclass'], prefix='Pclass')
 combined = pd.concat([combined, pclass_dummies], axis=1)
 combined.drop('Pclass', axis=1, inplace=True)
 status('Pclass')
 return combined<feature_engineering>
class Estimator(object):
def fit(self, x_train, y_train, x_valid, y_valid):
raise NotImplementedException
def predict(self, x):
raise NotImplementedException
def process_ticket() :
global combined

 def cleanTicket(ticket):
ticket = ticket.replace('.', '')
 ticket = ticket.replace('/', '')
 ticket = ticket.split()
ticket = map(lambda t: t.strip() , ticket)
 ticket = list(filter(lambda t: not t.isdigit() , ticket))
if len(ticket)> 0:
return ticket[0]
else:
return 'Unknown'
combined['Ticket'] = combined['Ticket'].map(cleanTicket)
 tickets_dummies = pd.get_dummies(combined['Ticket'], prefix='Ticket')
 combined = pd.concat([combined, tickets_dummies], axis=1)
 combined.drop('Ticket', inplace=True, axis=1)
 status('Ticket')
 return combined<drop_column>
class ScikitLearnEstimator(Estimator):
def __init__(self, estimator):
self.estimator = estimator
def fit(self, x_train, y_train, x_valid, y_valid):
self.estimator.fit(x_train, y_train)
 def predict(self, x):
return self.estimator.predict(x )
def dropper() :
global combined
combined.drop('Cabin', axis=1, inplace=True)
 combined.drop('PassengerId', inplace=True, axis=1)
 combined.drop('Last_Name', inplace=True, axis=1)
 combined.drop('Survived', inplace=True, axis=1)
 combined.drop('Ticket', inplace=True, axis=1)
 return combined<categorify>
def fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 print('MAE:', mean_absolute_error(y_train_valid, oof_part))
oof[valid_idx] = oof_part
y_part = estimator.predict(x_test)
 return y_part
combined = get_combined_data()
combined = family_survival()
combined = process_family()
combined = get_titles()
combined = process_names()
combined = process_age()
combined = age_binner()
combined = process_fares()
combined = process_fare_bin(onehot='no')
 combined = process_embarked()
combined = process_sex()
combined = dropper()
print(
 f'Processed everything.Missing values left: {combined.isna().sum().sum() }'
)<prepare_output>
def fit_predict(estimator, x_train, y_train, x_test):
oof = np.zeros(x_train.shape[0])
 y = np.zeros(x_test.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 for train_idx, valid_idx in kf.split(x_train):
y_part = fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof)
 y += y_part / kf.n_splits
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, y
v_merged = combined.copy()
v_merged['Survived'] = v_train['Survived']
v_merged.head()<import_modules>
def fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 mae = mean_absolute_error(y_train_valid, oof_part)
 print('MAE:', mae)
 oof[valid_idx] = oof_part
return estimator, mae
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, cross_val_score, learning_curve, cross_validate, train_test_split, KFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve, plot_roc_curve, auc
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import RFE
import xgboost as xgb
import lightgbm as lgb
from mlxtend.plotting import plot_decision_regions<load_from_csv>
def fit(estimator, x_train, y_train):
oof = np.zeros(x_train.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 trained_estimators = []
for train_idx, valid_idx in kf.split(x_train):
e, mae = fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof)
 trained_estimators.append(deepcopy(e))
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, trained_estimators
def recover_train_test_target() :
global combined
y = pd.read_csv('/kaggle/input/titanic/train.csv', usecols=['Survived'])['Survived']
X = combined.iloc[:idx]
X_test = combined.iloc[idx:]
return X, X_test, y
X, X_test, y = recover_train_test_target()<choose_model_class>
def predict(trained_estimators, x_test):
y = np.zeros(x_test.shape[0])
 for estimator in trained_estimators:
y_part = estimator.predict(x_test)
 y += y_part / len(trained_estimators)
 return y
cv = StratifiedKFold(10, shuffle=True, random_state=42)
 rf = RandomForestClassifier(criterion='gini',
n_estimators=1750,
max_depth=7,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=42,
n_jobs=-1,
verbose=0)
 lg = lgb.LGBMClassifier(max_bin=4,
num_iterations=550,
learning_rate=0.0114,
max_depth=3,
num_leaves=7,
colsample_bytree=0.35,
random_state=42,
n_jobs=-1)
 xg = xgb.XGBClassifier(
 n_estimators=2800,
min_child_weight=0.1,
learning_rate=0.002,
max_depth=2,
subsample=0.47,
colsample_bytree=0.35,
gamma=0.4,
reg_lambda=0.4,
random_state=42,
n_jobs=-1, 
)
 sv = SVC(probability=True)
 logreg = LogisticRegression(n_jobs=-1, solver='newton-cg')
 gb = GradientBoostingClassifier(random_state=42)
 gnb = GaussianNB()
mlp = MLPClassifier(random_state=42 )<define_variables>
def pipeline_fit(estimator, df_train, scaler=None):
add_new_features_4(df_train)
 x_train, y_train = feature_engineering(df_train, is_train=True)
 x_train = reduce_mem_usage(x_train)
 gc.collect()
if not(scaler is None):
scaler.fit(x_train)
 scaled_x_train = scaler.transform(x_train)
 else:
scaled_x_train = x_train.values
oof, trained_estimators = fit(estimator, scaled_x_train, y_train.values)
 del x_train
del scaled_x_train
del y_train
gc.collect()
return oof, trained_estimators
estimators = [rf, lg, xg, gb, sv, logreg, gnb, mlp]<compute_train_metric>
def pipeline_predict(trained_estimators, df_test, scaler=None):
add_new_features_4(df_test)
 x_test, _ = feature_engineering(df_test, is_train=False)
 x_test = reduce_mem_usage(x_test)
 gc.collect()
if not(scaler is None):
scaled_x_test = scaler.transform(x_test)
 else:
scaled_x_test = x_test.values
y = predict(trained_estimators, scaled_x_test)
 del x_test
del scaled_x_test
gc.collect()
return y
def model_check(X, y, estimators, cv):
model_table = pd.DataFrame()
row_index = 0
for est in estimators:
MLA_name = est.__class__.__name__
model_table.loc[row_index, 'Model Name'] = MLA_name
cv_results = cross_validate(
 est,
X,
y,
cv=cv,
scoring='accuracy',
return_train_score=True,
n_jobs=-1 
)
 model_table.loc[row_index, 'Train Accuracy Mean'] = cv_results[
'train_score'].mean()
model_table.loc[row_index, 'Test Accuracy Mean'] = cv_results[
'test_score'].mean()
model_table.loc[row_index, 'Test Std'] = cv_results['test_score'].std()
model_table.loc[row_index, 'Time'] = cv_results['fit_time'].mean()
row_index += 1
model_table.sort_values(by=['Test Accuracy Mean'],
ascending=False,
inplace=True)
 return model_table<compute_train_metric>
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape
raw_models = model_check(X, y, estimators, cv)
 display(raw_models.style.background_gradient(cmap='summer_r'))<train_on_grid>
df_train = reduce_mem_usage(df_train )
def m_roc(estimators, cv, X, y):
fig, axes = plt.subplots(math.ceil(len(estimators)/ 2),
2,
figsize=(25, 50))
axes = axes.flatten()
for ax, estimator in zip(axes, estimators):
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
 for i,(train, test)in enumerate(cv.split(X, y)) :
estimator.fit(X.loc[train], y.loc[train])
 viz = plot_roc_curve(estimator,
X.loc[test],
y.loc[test],
name='ROC fold {}'.format(i),
alpha=0.3,
lw=1,
ax=ax)
 interp_tpr = interp(mean_fpr, viz.fpr, viz.tpr)
 interp_tpr[0] = 0.0
tprs.append(interp_tpr)
 aucs.append(viz.roc_auc)
 ax.plot([0, 1], [0, 1],
linestyle='--',
lw=2,
color='r',
label='Chance',
alpha=.8)
 mean_tpr = np.mean(tprs, axis=0)
 mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
 std_auc = np.std(aucs)
 ax.plot(mean_fpr,
mean_tpr,
color='b',
label=r'Mean ROC(AUC = %0.2f $\pm$ %0.2f)' % 
(mean_auc, std_auc),
lw=2,
alpha=.8)
 std_tpr = np.std(tprs, axis=0)
 tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
 tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
 ax.fill_between(mean_fpr,
tprs_lower,
tprs_upper,
color='grey',
alpha=.2,
label=r'$\pm$ 1 std.dev.')
 ax.set(xlim=[-0.02, 1.02],
ylim=[-0.02, 1.02],
title=f'{estimator.__class__.__name__} ROC')
 ax.legend(loc='lower right', prop={'size': 18})
 plt.show()<compute_test_metric>
gc.collect()
m_roc(estimators, cv, X, y )<compute_test_metric>
df_train.drop(df_train[df_train['winPlacePerc'].isnull() ].index, inplace=True )
f_imp(estimators, X, y, 14 )<train_model>
import torch
from torch.utils.data import DataLoader, TensorDataset
import torch.nn as nn
from torch.nn.utils.weight_norm import weight_norm
rf.fit(X, y)
 estimator = rf.estimators_[0]
export_graphviz(estimator, out_file='tree.dot',
feature_names = X.columns,
class_names = ['Not Survived','Survived'],
rounded = True, proportion = False,
precision = 2, filled = True)
 call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
 plt.figure(figsize =(40, 20))
plt.imshow(plt.imread('tree.png'))
plt.axis('off');
plt.show() ;<split>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device
def f_selector(X, y, est, features):
X_train, X_valid, y_train, y_valid = train_test_split(X,
y,
test_size=0.4,
random_state=42)
 rfe = RFE(estimator=est, n_features_to_select=features, verbose=1)
 rfe.fit(X_train, y_train)
 print(dict(zip(X.columns, rfe.ranking_)))
 print(X.columns[rfe.support_])
 acc = accuracy_score(y_valid, rfe.predict(X_valid))
print("{0:.1%} accuracy on test set.".format(acc))
X_red = X[X_train.columns[rfe.support_].to_list() ]
X_te_red = X_test[X_train.columns[rfe.support_].to_list() ]
return X_red, X_te_red<compute_test_metric>
class PyTorch(Estimator):
def fit(self, x_train, y_train, x_valid, y_valid):
train_tensor = TensorDataset(
 torch.from_numpy(x_train.astype('float32')) ,
torch.from_numpy(y_train.astype('float32')))
 train_loader = DataLoader(train_tensor, batch_size=256, shuffle=True)
 self.model = nn.Sequential(
 weight_norm(nn.Linear(x_train.shape[1], 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 1)) ).to(device)
 for m in self.model:
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight_v)
 nn.init.kaiming_normal_(m.weight_g)
 nn.init.constant_(m.bias, 0)
 criterion = nn.L1Loss()
optimizer = torch.optim.Adam(self.model.parameters() , betas=(0.9, 0.999), lr=1e-3)
 self.model.train()
n_epochs = 50
for epoch in range(n_epochs):
epoch_loss = 0.0
for train_part, y_part in train_loader:
optimizer.zero_grad()
y_pred_part = self.model(train_part.to(device))
loss = criterion(y_pred_part.reshape(-1), y_part.to(device))
loss.backward()
optimizer.step()
epoch_loss += y_pred_part.shape[0] * loss.item()
print('Epoch %3d / %3d.Loss = %.5f' %(epoch + 1, n_epochs, epoch_loss / x_train.shape[0]))
def predict(self, x):
self.model.eval()
x_tensor = torch.from_numpy(x.astype('float32'))
x_loader = DataLoader(x_tensor, batch_size=256, shuffle=False)
 y_pred = np.empty(0)
 with torch.no_grad() :
for x_part in x_loader:
y_pred_part = self.model(x_part.to(device)).data.cpu().numpy().reshape(-1)
 y_pred = np.append(y_pred, y_pred_part)
 return y_pred
X_sel, X_test_sel = f_selector(X, y, rf, 11 )<normalization>
%%time
scaler = StandardScaler()
oof, trained_estimators = pipeline_fit(PyTorch() , df_train, scaler )
pipe = Pipeline([ 
('scaler', StandardScaler()), 
('reducer', PCA(n_components=2)) ,
])
 X_sel_red = pipe.fit_transform(X_sel)
 X_test_sel_red = pipe.transform(X_test_sel )<split>
del df_train
gc.collect()
def prob_reg(X, y):
figure = plt.figure(figsize=(20, 40))
h =.02
i = 1
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=.4, random_state=42)
 x_min, x_max = X_sel_red[:, 0].min() -.5, X_sel_red[:, 0].max() +.5
y_min, y_max = X_sel_red[:, 1].min() -.5, X_sel_red[:, 1].max() +.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
cm = plt.cm.RdYlGn
cm_bright = ListedColormap(['
ax = plt.subplot(5, 2, i)
 for clf in estimators:
ax = plt.subplot(math.ceil(len(estimators)/ 2), 2, i)
 clf.fit(X_train, y_train)
 score = clf.score(X_test, y_test)
 if hasattr(clf, "decision_function"):
Z = clf.decision_function(np.c_[xx.ravel() , yy.ravel() ])
 else:
Z = clf.predict_proba(np.c_[xx.ravel() , yy.ravel() ])[:, 1]
Z = Z.reshape(xx.shape)
 ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
 g = ax.scatter(X_train[:, 0],
X_train[:, 1],
c=y_train,
cmap=cm_bright,
edgecolors='k')
 ax.scatter(X_test[:, 0],
X_test[:, 1],
c=y_test,
cmap=cm_bright,
edgecolors='k',
alpha=0.6)
 ax.set_xlim(xx.min() , xx.max())
 ax.set_ylim(yy.min() , yy.max())
 ax.set_title(clf.__class__.__name__)
 ax.set_xlabel('PCA 1')
 ax.set_ylabel('PCA 2')
 plt.legend(handles=g.legend_elements() [0],
labels=['Not Survived', 'Survived'],
framealpha=0.3,
scatterpoints=1)
 i += 1
plt.tight_layout()
plt.show()<compute_test_metric>
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape
dec_regs(X_sel_red, y, estimators )<compute_test_metric>
df_test = reduce_mem_usage(df_test )
prob_reg(X_sel_red, y )<compute_train_metric>
df_test_id = pd.DataFrame(index=df_test.index )
pca_models = model_check(X_sel_red, y, estimators, cv)
 display(pca_models.style.background_gradient(cmap='summer_r'))<train_model>
gc.collect()
rand_model_full_data = rf.fit(X, y)
 print(accuracy_score(y, rand_model_full_data.predict(X)))
 y_pred = rand_model_full_data.predict(X_test )<save_to_csv>
y = pipeline_predict(trained_estimators, df_test, scaler )
test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 submission_df = pd.DataFrame(columns=['PassengerId', 'Survived'])
 submission_df['PassengerId'] = test_df['PassengerId']
submission_df['Survived'] = y_pred
submission_df.to_csv('submission.csv', header=True, index=False)
 submission_df.head(10 )<categorify>
del df_test
gc.collect()
class KaggleMember() :
kaggle_member_count=0
def __init__(self, name, surname, level=None):
self.name=name.capitalize()
self.surname=surname.upper()
self._set_level(level)
 KaggleMember.kaggle_member_count+=1
self.kaggle_id=KaggleMember.kaggle_member_count
def display_number_of_member(self):
print("There are {} members in Kaggle community".format(KaggleMember.kaggle_member_count))
def display_member_info(self):
print("Kaggle Member Full Name:{} {}".format(self.name, self.surname))
print("Level:{:15}".format(self.level))
print("Kaggle ID:",self.kaggle_id)
 def _set_level(self, level):
if level is None:
self.level='Novice'
else:
self.level=level.title()
myclass_name='Kaggle Member' 
<set_options>
df_oof = pd.DataFrame()
df_oof['pytorch_oof'] = oof
df_oof.to_csv('pytorch_oof.csv', index_label='id' )
warnings.filterwarnings('ignore')
 print("Warnings were ignored" )<count_missing_values>
df_submission = pd.DataFrame(index=df_test_id.index)
 df_submission['winPlacePerc'] = y
df_submission.to_csv('pytorch_raw.csv', index_label='Id' )
class Information() :
def __init__(self):

 print("Information object created")
 def _get_missing_values(self,data):

 missing_values = data.isnull().sum()
missing_values.sort_values(ascending=False, inplace=True)
 return missing_values
def info(self,data):

 feature_dtypes=data.dtypes
self.missing_values=self._get_missing_values(data)
 print("=" * 50)
 print("{:16} {:16} {:25} {:16}".format("Feature Name".upper() ,
"Data Format".upper() ,
"
"Samples".upper()))
for feature_name, dtype, missing_value in zip(self.missing_values.index.values,
feature_dtypes[self.missing_values.index.values],
self.missing_values.values):
print("{:18} {:19} {:19} ".format(feature_name, str(dtype), str(missing_value)) , end="")
 for v in data[feature_name].values[:10]:
print(v, end=",")
 print()
print("="*50)
<categorify>
df_test = pd.read_csv('.. /input/test_V2.csv')
 df_test.shape
class Preprocess() :
def __init__(self):
print("Preprocess object created")
 def fillna(self, data, fill_strategies):
for column, strategy in fill_strategies.items() :
if strategy == 'None':
data[column] = data[column].fillna('None')
 elif strategy == 'Zero':
data[column] = data[column].fillna(0)
 elif strategy == 'Mode':
data[column] = data[column].fillna(data[column].mode() [0])
 elif strategy == 'Mean':
data[column] = data[column].fillna(data[column].mean())
 elif strategy == 'Median':
data[column] = data[column].fillna(data[column].median())
 else:
print("{}: There is no such thing as preprocess strategy".format(strategy))
return data
def drop(self, data, drop_strategies):
for column, strategy in drop_strategies.items() :
data=data.drop(labels=[column], axis=strategy)
 return data
def feature_engineering(self, data, engineering_strategies=1):
if engineering_strategies==1:
return self._feature_engineering1(data)
 return data
def _feature_engineering1(self,data):
data=self._base_feature_engineering(data)
 data['FareBin'] = pd.qcut(data['Fare'], 4)
 data['AgeBin'] = pd.cut(data['Age'].astype(int), 5)
 drop_strategy = {'Age': 1,
'Name': 1,
'Fare': 1}
data = self.drop(data, drop_strategy)
 return data
def _base_feature_engineering(self,data):
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = 1
data.loc[(data['FamilySize'] > 1), 'IsAlone'] = 0
data['Title'] = data['Name'].str.split(", ", expand=True)[1].str.split('.', expand=True)[0]
min_lengtht = 10
title_names =(data['Title'].value_counts() < min_lengtht)
 data['Title'] = data['Title'].apply(lambda x: 'Misc' if title_names.loc[x] == True else x)
 return data
def _label_encoder(self,data):
labelEncoder=LabelEncoder()
for column in data.columns.values:
if 'int64'==data[column].dtype or 'float64'==data[column].dtype or 'int64'==data[column].dtype:
continue
labelEncoder.fit(data[column])
 data[column]=labelEncoder.transform(data[column])
 return data
def _get_dummies(self, data, prefered_columns=None):
if prefered_columns is None:
columns=data.columns.values
non_dummies=None
else:
non_dummies=[col for col in data.columns.values if col not in prefered_columns ]
columns=prefered_columns
dummies_data=[pd.get_dummies(data[col],prefix=col)for col in columns]
if non_dummies is not None:
for non_dummy in non_dummies:
dummies_data.append(data[non_dummy])
 return pd.concat(dummies_data, axis=1 )<categorify>
df_submission = df_submission.merge(df_test[['Id', 'matchId', 'groupId', 'maxPlace', 'numGroups']], on='Id', how='left')
 df_submission.head()
class PreprocessStrategy() :

 def __init__(self):
self.data=None
self._preprocessor=Preprocess()
def strategy(self, data, strategy_type="strategy1"):
self.data=data
if strategy_type=='strategy1':
self._strategy1()
elif strategy_type=='strategy2':
self._strategy2()
return self.data
def _base_strategy(self):
drop_strategy = {'PassengerId': 1,
'Cabin': 1,
'Ticket': 1}
self.data = self._preprocessor.drop(self.data, drop_strategy)
 fill_strategy = {'Age': 'Median',
'Fare': 'Median',
'Embarked': 'Mode'}
self.data = self._preprocessor.fillna(self.data, fill_strategy)
 self.data = self._preprocessor.feature_engineering(self.data, 1)
 self.data = self._preprocessor._label_encoder(self.data)
 def _strategy1(self):
self._base_strategy()
self.data=self._preprocessor._get_dummies(self.data,
prefered_columns=['Pclass', 'Sex', 'Parch', 'Embarked', 'Title', 'IsAlone'])
 def _strategy2(self):
self._base_strategy()
self.data=self._preprocessor._get_dummies(self.data,
prefered_columns=None )<choose_model_class>
df_submission_group = df_submission.groupby(['matchId', 'groupId'] ).first().reset_index()
df_submission_group['rank'] = df_submission_group.groupby(['matchId'])['winPlacePerc'].rank()
df_submission_group = df_submission_group.merge(df_submission_group.groupby('matchId')['rank'].max().to_frame('max_rank' ).reset_index() , on='matchId', how='left')
 df_submission_group['adjusted_perc'] =(df_submission_group['rank'] - 1)/(df_submission_group['numGroups'] - 1)
 df_submission = df_submission.merge(df_submission_group[['adjusted_perc', 'matchId', 'groupId']], on=['matchId', 'groupId'], how='left')
 df_submission['winPlacePerc'] = df_submission['adjusted_perc']
df_submission.head()
class GridSearchHelper() :
def __init__(self):
print("GridSearchHelper Created")
 self.gridSearchCV=None
self.clf_and_params=list()
self._initialize_clf_and_params()
def _initialize_clf_and_params(self):
clf= KNeighborsClassifier()
params={'n_neighbors':[5,7,9,11,13,15],
'leaf_size':[1,2,3,5],
'weights':['uniform', 'distance']
}
self.clf_and_params.append(( clf, params))
clf=LogisticRegression()
params={'penalty':['l1', 'l2'],
'C':np.logspace(0, 4, 10)
 }
self.clf_and_params.append(( clf, params))
clf = SVC()
params = [ {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']}]
self.clf_and_params.append(( clf, params))
clf=DecisionTreeClassifier()
params={'max_features': ['auto', 'sqrt', 'log2'],
'min_samples_split': [2,3,4,5,6,7,8,9,10,11,12,13,14,15],
'min_samples_leaf':[1],
'random_state':[123]}
self.clf_and_params.append(( clf,params))
clf = RandomForestClassifier()
params = {'n_estimators': [4, 6, 9],
'max_features': ['log2', 'sqrt','auto'],
'criterion': ['entropy', 'gini'],
'max_depth': [2, 3, 5, 10],
'min_samples_split': [2, 3, 5],
'min_samples_leaf': [1,5,8]
}
self.clf_and_params.append(( clf, params))
def fit_predict_save(self, X_train, X_test, y_train, submission_id, strategy_type):
self.X_train=X_train
self.X_test=X_test
self.y_train=y_train
self.submission_id=submission_id
self.strategy_type=strategy_type
clf_and_params = self.get_clf_and_params()
models=[]
self.results={}
for clf, params in clf_and_params:
self.current_clf_name = clf.__class__.__name__
grid_search_clf = GridSearchCV(clf, params, cv=5)
 grid_search_clf.fit(self.X_train, self.y_train)
 self.Y_pred = grid_search_clf.predict(self.X_test)
 clf_train_acc = round(grid_search_clf.score(self.X_train, self.y_train)* 100, 2)
 print(self.current_clf_name, " trained and used for prediction on test data...")
 self.results[self.current_clf_name]=clf_train_acc
models.append(clf)
 self.save_result()
print()

 def show_result(self):
for clf_name, train_acc in self.results.items() :
print("{} train accuracy is {:.3f}".format(clf_name, train_acc))
def save_result(self):
Submission = pd.DataFrame({'PassengerId': self.submission_id,
'Survived': self.Y_pred})
 file_name="{}_{}.csv".format(self.strategy_type,self.current_clf_name.lower())
 Submission.to_csv(file_name, index=False)
 print("Submission saved file name: ",file_name)
 def get_clf_and_params(self):
return self.clf_and_params
def add(self,clf, params):
self.clf_and_params.append(( clf, params))<train_model>
df_submission.loc[df_submission.maxPlace == 0, 'winPlacePerc'] = 0
df_submission.loc[df_submission.maxPlace == 1, 'winPlacePerc'] = 1
class Visualizer:
def __init__(self):
print("Visualizer object created!")
 def RandianViz(self, X, y, number_of_features):
if number_of_features is None:
features=X.columns.values
else:
features=X.columns.values[:number_of_features]
fig, ax=plt.subplots(1, figsize=(15,12))
radViz=RadViz(classes=['survived', 'not survived'], features=features)
 radViz.fit(X, y)
 radViz.transform(X)
 radViz.poof() 
<prepare_x_and_y>
t = df_submission.loc[df_submission.maxPlace > 1]
gap = 1.0 /(t.maxPlace.values - 1)
 fixed_perc = np.around(t.winPlacePerc.values / gap)* gap
df_submission.loc[df_submission.maxPlace > 1, 'winPlacePerc'] = fixed_perc
class ObjectOrientedTitanic() :
def __init__(self, train, test):

 print("ObjectOrientedTitanic object created")
 self.testPassengerID=test['PassengerId']
self.number_of_train=train.shape[0]
self.y_train=train['Survived']
self.train=train.drop('Survived', axis=1)
 self.test=test
self.all_data=self._get_all_data()
self._info=Information()
self.preprocessStrategy = PreprocessStrategy()
self.visualizer=Visualizer()
self.gridSearchHelper = GridSearchHelper()
def _get_all_data(self):
return pd.concat([self.train, self.test])
 def information(self):

 self._info.info(self.all_data)
 def preprocessing(self, strategy_type):

 self.strategy_type=strategy_type
self.all_data = self.preprocessStrategy.strategy(self._get_all_data() , strategy_type)
 def visualize(self, visualizer_type, number_of_features=None):
self._get_train_and_test()
if visualizer_type=="RadViz":
self.visualizer.RandianViz(X=self.X_train,
y=self.y_train,
number_of_features=number_of_features)
 def machine_learning(self):

 self._get_train_and_test()
self.gridSearchHelper.fit_predict_save(self.X_train,
self.X_test,
self.y_train,
self.testPassengerID,
self.strategy_type)
 def show_result(self):
self.gridSearchHelper.show_result()
def _get_train_and_test(self):

 self.X_train=self.all_data[:self.number_of_train]
self.X_test=self.all_data[self.number_of_train:] 
<load_from_csv>
df_submission.loc[(df_submission.maxPlace > 1)&(df_submission.numGroups == 1), 'winPlacePerc'] = 0
assert df_submission['winPlacePerc'].isnull().sum() == 0
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 objectOrientedTitanic=ObjectOrientedTitanic(train, test)
<normalization>
df_submission[['Id', 'winPlacePerc']].to_csv('pytorch_adjusted.csv', index=False )