Kaggle competition: Human Protein Atlas Image Classification
randomforest = RandomForestClassifier(criterion = 'gini',bootstrap= True, max_depth= 5, max_features=10, min_samples_split= 31, n_estimators=20,random_state=seed_value)
 svc_model = SVC(C=0.5, cache_size=200,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf', max_iter=-1, probability=True, random_state=seed_value, shrinking=True, tol=0.001,verbose=False)
 knn = KNeighborsClassifier(leaf_size= 6,n_neighbors = 16, p=1 ,weights = 'uniform')
 modellt = LGBMClassifier(boosting_type='gbdt',num_leaves = 31,max_depth =5,learning_rate =0.05,subsample= 1,colsample_bytree = 0.8, reg_lambda =0.5,reg_alpha= 0.5,
n_estimators=500,objective='binary',metric='binary_logloss', seed=seed_value)
 xgb_model=XGBClassifier(alpha =0.5, colsample_bytree =0.8 ,gamma=0.5,learning_rate= 0.1, max_depth= 5,n_estimators=100,
objective= 'binary:logistic',subsample= 0.8, random_state=seed_value)
 modelM=MLPClassifier(hidden_layer_sizes=(16,8,1),activation='tanh',solver='adam', alpha=0.01, batch_size=32, learning_rate='adaptive', learning_rate_init=0.001, max_iter=850, shuffle=False, random_state=seed_value, tol=0.001, verbose=False,early_stopping=False,validation_fraction=0.3, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
 AdaB=AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=5),learning_rate=0.001,n_estimators=100,random_state=seed_value)
 gb = GradientBoostingClassifier(criterion='mae',n_estimators=150,learning_rate = 0.1,max_features=7,max_depth =3,
min_samples_leaf=3, min_samples_split= 32, subsample= 1,random_state=seed_value)
 NN1=('NN', modelM)
 XG1=('XGboost', xgb_model)
 GR1=('GradientB',gb)
 SVM1=('SVM',svc_model)
 ADA1=('Ada',AdaB)
 KNN1=('KNN',knn)
 RF1=('RF',randomforest)
 LG1=('LightGBM',modellt )<find_best_model_class>
%matplotlib inline
def evaluate_model(model):
seed=7
kfld = StratifiedKFold(n_splits=5,shuffle=True,random_state=seed)
 scores = cross_val_score(model, X_train , Y_train, scoring='accuracy', cv=kfld, n_jobs=-1, error_score='raise')
 return scores
clfs={'LightGBM':[modellt,78.707],'XGBoost':[xgb_model,99978.229],'GradientBoost':[gb,78.468]
,'AdaBoost':[AdaB,78.229],'RandomForest':[randomforest,78.707],
'SVM':[svc_model,78.229],'KNN':[knn,80.143],'MLP':[modelM,79.168]}
scores={}
for name, model in clfs.items() :
metr=evaluate_model(model[0])
 scores.update({name:[mean(metr),std(metr),model[1]]})
 results_table = pd.DataFrame(scores, index=['Mean_accuracy', 'Std', 'Leaderboard'] ).round(decimals=3)
 results_table<split>
try:
shutil.copytree(".. /input/keras-pretrained-models", "/tmp/.keras/models")
 except FileExistsError:
pass
x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.2,shuffle=True ,random_state=123 )<compute_train_metric>
Conv1D, Conv2D, Conv3D, \
Conv2DTranspose, \
AveragePooling1D, AveragePooling2D, \
MaxPooling1D, MaxPooling2D, MaxPooling3D, \
GlobalAveragePooling1D, \
GlobalMaxPooling1D, GlobalMaxPooling2D, GlobalMaxPooling3D, \
LocallyConnected1D, LocallyConnected2D, \
concatenate, Flatten, Average, Activation, \
RepeatVector, Permute, Reshape, Dot, \
multiply, dot, add, \
PReLU, \
Bidirectional, TimeDistributed, \
SpatialDropout1D, \
BatchNormalization
def evaluation(name,clf):
model=clf.fit(x_train,y_train)
 y_prob = model.predict_proba(x_test)[:, 1]
prAuc = average_precision_score(y_test, y_prob)
 y_pred = model.predict(x_test)
 acc = balanced_accuracy_score(y_test, y_pred)
 f1= f1_score(y_test, y_pred)
 return prAuc,acc,f1
scores={}
for name, model in models_set.items() :
metr=evaluation(name,model)
 scores.update({name:[metr[0],metr[1],metr[2]]})
 results =(pd.DataFrame(scores, index=['PR-AUC','Balanced_Accuracy','F1 score'])).round(decimals=3)
 results<choose_model_class>
from PIL import Image
from zipfile import ZipFile
import h5py
import cv2
from tqdm import tqdm
def create_clf(E,t):
if t=='Voting':
vclf=VotingClassifier(E, voting='hard')
 else:
vclf=StackingClassifier(E, final_estimator= LogisticRegression())
 return vclf
E1=[KNN1,NN1,SVM1,RF1,GR1]
E2=[KNN1,SVM1,RF1,GR1]
E3=[KNN1,SVM1,GR1]
E4=[KNN1,SVM1]
ensembles_set={'KNN-NN-SVM-RF-GR':[E1,'79.186','78.947'],'KNN-SVM-RF-GR':[E2,'79.425','79.186 ']
,'KNN-SVM-GR':[E3,'79.425','79.186'],'KNN-SVM1':[E4,'80.382','78.468']}
scores1={}
scores2={}
for clf, item in ensembles_set.items() :
newclv=create_clf(item[0],'Voting')
 newclST=create_clf(item[0],'Stacking')
 kfoldevalv=evaluate_model(newclv)
 kfoldevalst=evaluate_model(newclST)
 scores1.update({clf:[mean(kfoldevalv),std(kfoldevalv),item[1]]})
 scores2.update({clf:[mean(kfoldevalst),std(kfoldevalst),item[2]]})
 results1 = pd.DataFrame(scores1, index=['Mean_accuracy', 'Std','Leaderboard'])
 results2 = pd.DataFrame(scores2, index=['Mean_accuracy', 'Std','Leaderboard'])
 print('VOTING CLASSIFIERS')
 display(results1)
 print('STACKING CLASSIFIERS')
 display(results2 )<save_to_csv>
src_dir = '.. /input/human-protein-atlas-image-classification'
E=[KNN1,SVM1]
vot=VotingClassifier(E, voting='hard')
 ensemble = vot.fit(X_train, Y_train)
 y_vote = ensemble.predict(X_test)
 submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": y_vote
})
 submission.to_csv('Titanic.csv', index=False)
 print("Your submission was successfully saved!")
<set_options>
train_labels = pd.read_csv(os.path.join(src_dir, "train.csv"))
print(train_labels.shape)
 train_labels.head(10 )
%matplotlib inline 
<load_from_csv>
test_labels = pd.read_csv(os.path.join(src_dir, "sample_submission.csv"))
print(test_labels.shape)
 test_labels.head()
df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_train = pd.read_csv('.. /input/titanic/train.csv' )<drop_column>
SH =(256, 256)
 ID_LIST_TRAIN = train_labels.Id.tolist()
df_train.drop(['Cabin','Fare','Parch','Ticket','Name'],axis=1,inplace = True )<drop_column>
def get_arr(Id, test=False):
if test:
arr = get_arr0(Id, test=True)
 else:
arr = get_arr0(Id)
 arr = cv2.resize(arr, SH ).astype('float32')
 return arr
df_test.drop(['Cabin','Fare','Parch','Ticket','Name'],axis=1,inplace = True )<correct_missing_values>
y_cat_train_dic = {}
for icat in range(28):
target = str(icat)
 y_cat_train_5 = np.array([int(target in ee.split())for ee in train_labels.Target.tolist() ])
 y_cat_train_dic[icat] = y_cat_train_5
df_test.fillna(35,inplace=True )<correct_missing_values>
up_sample = {}
for k in y_cat_train_dic:
v = y_cat_train_dic[k].sum()
up_sample[k] = np.ceil(( train_labels.shape[0]/28)/ v)
 up_sample
df_train.fillna(35,inplace=True )<categorify>
up_sample2 = list(zip(*sorted(list(up_sample.items()), key=lambda x: x[0])))[1]
up_sample2 = np.array(up_sample2)
 up_sample2
embark = pd.get_dummies(df_train['Embarked'],drop_first=True)
 sex = pd.get_dummies(df_train['Sex'],drop_first=True)
 train = pd.concat([df_train.drop(['Embarked','Sex'],axis=1),sex,embark],axis=1 )<categorify>
class Seq(object):
sections = None
index = None
def __init__(self, df, extend=False, aug=False, test=False, batch_size=32):
self.shaffle = None
self.extend = extend
self.aug = aug
self.test = test
self.batch_size = batch_size
self.df = df
self.ids = self.df.Id.tolist()
self.reversed = sorted(range(SH[0]), reverse=True)
 self.initialize_it()
self.len = 1
for _ in self.it:
self.len += 1
self.initialize_it()
def initialize_it(self):
if self.shaffle:

 raise NotImplementedError
self.it = iter(range(0, len(self.ids), self.batch_size))
self.idx_next = self.it.__next__()
def __len__(self):
return self.len
def __iter__(self):
return self
def __next__(self):
idx = self.idx_next
self.ids_part = self.ids[idx:(( idx+self.batch_size)if idx+self.batch_size<len(self.ids)else len(self.ids)) ]
res = self.getpart(self.ids_part)
 try:
self.idx_next = self.it.__next__()
except StopIteration:
self.initialize_it()
return res
def __getitem__(self, id0):
arr, tgts = self.get_data(id0)
 cat = self.convert_tgts(tgts)
 return arr, cat
k_list = list(range(4))
def random_transform(self, arr):
k = random.choice(self.k_list)
 arr0 = np.rot90(arr, k=k)
 if random.randint(0,1):
arr0 = arr0[self.reversed,:,:]
if random.randint(0,1):
arr0 = arr0[:,self.reversed,:]
return arr0
def convert_tgts(self, tgts):
try:
cats = to_categorical(tgts, num_classes=28)
 cat = cats.sum(axis=0)
 except TypeError:
cat = np.zeros(( 28,))
return cat
def get_data(self, id0):
arr = get_arr(id0, test=self.test)
 try:
y0 =(self.df.Target[self.df.Id == id0] ).tolist() [0]
y1 = y0.split()
y = [int(ee)for ee in y1]
except AttributeError:
y = None
return arr, y
def getpart(self, ids):
xs = []
ys = []
for id0 in ids:
self.extend_data(id0, xs, ys)
 x = np.stack(xs)
 y = np.stack(ys)
 x_dummy = np.zeros(( len(x), 1))
x_ret = {
'input': x,
'input_cls': y,
}
y_ret = {
'path_fit_cls_img': x_dummy,
'path_cls_img_cls': y,
'path_fit_imgA': x_dummy,
'path_fit_img_cls_img': x_dummy,
'path_cls_cls': y,
'path_img_cls': y,
'path_img_img_cls': y,
'path_fit_cls_img_imgE': x_dummy,
}
return(x_ret, y_ret)
 def split(self, arr, sections=sections):
res0 = np.vsplit(arr, sections)
 res = [np.hsplit(ee, sections)for ee in res0]
res = list(chain.from_iterable(res))
return res
def extend_data(self, id0, xs, ys):
arr0, cat = self[id0]
if self.extend:
mm = up_sample2[cat==1].max()
mm = int(mm)
 for ii in range(mm):
if self.aug:
img = self.random_transform(arr0)
 else:
img = arr0
xs.append(img.flatten())
 ys.append(cat)
 else:
if self.aug:
img = self.random_transform(arr0)
 else:
img = arr0
xs.append(img.flatten())
 ys.append(cat )
embark = pd.get_dummies(df_test['Embarked'],drop_first=True)
 sex = pd.get_dummies(df_test['Sex'],drop_first=True)
 test = pd.concat([df_test.drop(['Embarked','Sex'],axis=1),sex,embark],axis=1 )<drop_column>
from keras import applications
train = train.drop(['C','PassengerId'],axis=1)
 test = test.drop(['PassengerId'],axis=1 )<choose_model_class>
def make_trainable_false(model_resnet, trainable=False):
layers = model_resnet.layers
for ilayer in layers:
ilayer.trainable = trainable
return
model = Sequential()
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse' )<train_model>
def make_model_cnvt(img_dim, img_shape):

 inp = Input(shape=(img_dim,))
oup = Reshape(img_shape )(inp)
 oup = Conv2D(3,
kernel_size=1,
strides=1,
padding='same',
activation='tanh',
kernel_regularizer=regularizers.l2(1e-4))(oup)
 model_cnvt = Model(inp, oup, name='model_cnvt')
 return model_cnvt
model.fit(train.drop(['Survived'],axis=1),train['Survived'],epochs=200 )<predict_on_test>
def make_model_classifier(input_dim=1536):
inp_cls = Input(( input_dim,))
oup_cls = Dense(28 )(inp_cls)
 oup_cls = Activation('sigmoid' )(oup_cls)
 model_classifier = Model(inp_cls, oup_cls, name='classifier')
 return model_classifier
pred = model.predict_classes(test )<define_variables>
def make_model(img_dim, model_cnvt, model_resnet, model_classifier):

 inp = Input(shape=(img_dim,), name='input')
 oup = model_cnvt(inp)
 oup = model_resnet(oup)
 oup = model_classifier(oup)
 oup = Activation('linear', name='path_cls_cls' )(oup)
 model = Model(inp, oup, name='model')
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy'])
 return {
'model_classifier': model_classifier,
'model_resnet': model_resnet,
'model_cnvt': model_cnvt,
'model': model
}
idpred = []
for i in range(len(pred)) :
idpred.append(pred[i][0] )<concatenate>
model_cnvt = make_model_cnvt(img_dim, img_shape)
 model_cnvt.summary()
submit = pd.concat([df_test['PassengerId'],p],axis=1 )<rename_columns>
model_cnvt.layers[2].get_weights()
submit.rename(columns={0:'Survived'},inplace = True )<save_to_csv>
model_cnvt.load_weights('.. /input/keras-inceptionresnetv2-resize139x139-005focal/model_5_cnvt.h5')
 model_cnvt.layers[2].get_weights()
submit.to_csv('Submission.csv',index=False )<set_options>
model_resnet = applications.inception_resnet_v2.InceptionResNetV2(
 include_top=False,
weights='imagenet',
input_tensor=None,
input_shape=list(SH)+ [3],
pooling='avg',
classes=None )
pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 10)
 pd.set_option('display.width', 1000)
 %pip install autoviz
%pip install ppscore
print("")
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
model_resnet.layers[1].get_weights() [0][0]
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 display(df.head(10))<load_from_csv>
model_resnet.load_weights('.. /input/keras-inceptionresnetv2-resize139x139-005focal/model_5_resnet.h5')
 model_resnet.layers[1].get_weights() [0][0]
AV = AutoViz_Class()
report_2 = AV.AutoViz("/kaggle/input/titanic/train.csv" )<define_variables>
model_classifier.layers[1].get_weights() [0][0]
df_survivors = df[df['Survived'] == 1]
df_nonsurvivors = df[df['Survived'] == 0]<compute_test_metric>
model_classifier.load_weights('.. /input/keras-inceptionresnetv2-resize139x139-005focal/model_5_classifier.h5')
 model_classifier.layers[1].get_weights() [0][0]
t_stat, p_value = ztest(dist_a, dist_b)
 print("----- Z Test Results -----")
 print("T stat.= " + str(t_stat))
print("P value = " + str(p_value))
print("")
 t_stat_2, p_value_2 = stats.ttest_ind(dist_a, dist_b)
 print("----- T Test Results -----")
 print("T stat.= " + str(t_stat_2))
print("P value = " + str(p_value_2))<create_dataframe>
THRESHOLD = 0.5
K_epsilon = K.epsilon()
def f1(y_true, y_pred):
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), THRESHOLD), K.floatx())
 tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )
df_survivors_sex = df_survivors['Sex'].value_counts()
df_survivors_sex = pd.DataFrame({'Sex':df_survivors_sex.index, 'count':df_survivors_sex.values})
 df_nonsurvivors_sex = df_nonsurvivors['Sex'].value_counts()
df_nonsurvivors_sex = pd.DataFrame({'Sex':df_nonsurvivors_sex.index, 'count':df_nonsurvivors_sex.values})
 pie_survivors_sex = go.Pie(
 labels = df_survivors_sex['Sex'],
values = df_survivors_sex['count'],
domain=dict(x=[0, 0.5]),
name='Survivors',
hole = 0.5,
marker = dict(colors=['violet', 'cornflowerblue'], line=dict(color=' 
)
 pie_nonsurvivors_sex = go.Pie(
 labels = df_nonsurvivors_sex['Sex'],
values = df_nonsurvivors_sex['count'],
domain=dict(x=[0.5, 1.0]),
name='non-Survivors',
hole = 0.5,
marker = dict(colors=['cornflowerblue', 'violet'], line=dict(color=' 
)
 data = [pie_survivors_sex, pie_nonsurvivors_sex]
layout = go.Layout(
 paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
title='"Sex" percentage from Survivors vs non-Survivors',
annotations=[dict(text='Survivors', x=0.18, y=0.5, font_size=15, showarrow=False),
dict(text='Non-Survivors', x=0.85, y=0.5, font_size=15, showarrow=False)] 
)
 fig = go.Figure(data=data, layout=layout)
 fig.show()<create_dataframe>
models['model'].compile(loss=f1_loss,
optimizer='adam',
metrics=['categorical_accuracy', 'binary_accuracy', f1] )
df_survivors_pclass = df_survivors['Pclass'].value_counts()
df_survivors_pclass = pd.DataFrame({'Pclass':df_survivors_pclass.index, 'count':df_survivors_pclass.values})
 df_nonsurvivors_pclass = df_nonsurvivors['Pclass'].value_counts()
df_nonsurvivors_pclass = pd.DataFrame({'Pclass':df_nonsurvivors_pclass.index, 'count':df_nonsurvivors_pclass.values})
 pie_survivors_pclass = go.Pie(
 labels = df_survivors_pclass['Pclass'],
values = df_survivors_pclass['count'],
domain=dict(x=[0, 0.5]),
name='Survivors',
hole = 0.5,
marker = dict(colors=[' 
)
 pie_nonsurvivors_pclass = go.Pie(
 labels = df_nonsurvivors_pclass['Pclass'],
values = df_nonsurvivors_pclass['count'],
domain=dict(x=[0.5, 1.0]),
name='non-Survivors',
hole = 0.5,
marker = dict(colors=[' 
)
 data = [pie_survivors_pclass, pie_nonsurvivors_pclass]
layout = go.Layout(
 paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
title='"Pclass" percentage from Survivors vs non-Survivors',
annotations=[dict(text='Survivors', x=0.18, y=0.5, font_size=15, showarrow=False),
dict(text='Non-Survivors', x=0.85, y=0.5, font_size=15, showarrow=False)] 
)
 fig = go.Figure(data=data, layout=layout)
 fig.show()<compute_test_metric>
seq = Seq(train_labels, extend=False, aug=True, batch_size=32)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=3,
steps_per_epoch=len(seq),
callbacks=[] )
t_stat_3, p_value_3 = ztest(dist_c, dist_d)
 print("----- Z Test Results -----")
 print("T stat.= " + str(t_stat_3))
print("P value = " + str(p_value_3))
print("")
 t_stat_4, p_value_4 = stats.ttest_ind(dist_c, dist_d)
 print("----- T Test Results -----")
 print("T stat.= " + str(t_stat_4))
print("P value = " + str(p_value_4))<import_modules>
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 y_pred = models['model_cnvt'].predict(xs['input'])
 print(y_pred.shape )
import collections
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from imblearn.combine import SMOTEENN
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from category_encoders import OneHotEncoder, TargetEncoder, LeaveOneOutEncoder
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import SimpleImputer, IterativeImputer
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit, RandomizedSearchCV, train_test_split
from sklearn.feature_selection import SelectPercentile, SelectFromModel
from sklearn.preprocessing import StandardScaler, MinMaxScaler, PolynomialFeatures
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, log_loss, precision_recall_curve, average_precision_score, roc_curve, roc_auc_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, StackingClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from xgboost import XGBClassifier, plot_importance as plot_importance_xgb
from lightgbm import LGBMClassifier, plot_importance as plot_importance_lgbm<feature_engineering>
Image.fromarray(np.uint8(( y_pred[0]+1)/2*255))
df['AgeCat'] = ''
df['AgeCat'].loc[(df['Age'] < 18)] = 'young'
df['AgeCat'].loc[(df['Age'] >= 18)&(df['Age'] < 56)] = 'mature'
df['AgeCat'].loc[(df['Age'] >= 56)] = 'senior'
df['FamilySize'] = ''
df['FamilySize'].loc[(df['SibSp'] <= 2)] = 'small'
df['FamilySize'].loc[(df['SibSp'] > 2)&(df['SibSp'] <= 5)] = 'medium'
df['FamilySize'].loc[(df['SibSp'] > 5)] = 'large'
df['IsAlone'] = ''
df['IsAlone'].loc[(( df['SibSp'] + df['Parch'])> 0)] = 'no'
df['IsAlone'].loc[(( df['SibSp'] + df['Parch'])== 0)] = 'yes'
df['SexCat'] = ''
df['SexCat'].loc[(df['Sex'] == 'male')&(df['Age'] <= 21)] = 'youngmale'
df['SexCat'].loc[(df['Sex'] == 'male')&(( df['Age'] > 21)&(df['Age'])< 50)] = 'maturemale'
df['SexCat'].loc[(df['Sex'] == 'male')&(df['Age'] > 50)] = 'seniormale'
df['SexCat'].loc[(df['Sex'] == 'female')&(df['Age'] <= 21)] = 'youngfemale'
df['SexCat'].loc[(df['Sex'] == 'female')&(( df['Age'] > 21)&(df['Age'])< 50)] = 'maturefemale'
df['SexCat'].loc[(df['Sex'] == 'female')&(df['Age'] > 50)] = 'seniorfemale'
df['Title'] = df['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
df['Is_Married'] = 0
df['Is_Married'].loc[df['Title'] == 'Mrs'] = 1
df['Title'] = df['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 df['Title'] = df['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 df['Ticket_Frequency'] = df.groupby('Ticket')['Ticket'].transform('count')
 df.head(10 )<drop_column>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred )
def get_feature_names(df):
target = df['Survived']
df.drop(['PassengerId', 'Survived', 'Ticket', 'Name', 'Cabin'], axis=1, inplace=True)
 categorical_df = df.select_dtypes(include=['object'])
 numeric_df = df.select_dtypes(exclude=['object'])
 categorical_columns = list(categorical_df.columns)
 numeric_columns = list(numeric_df.columns)
 print("Categorical columns:
", categorical_columns)
 print("
Numeric columns:
", numeric_columns)
 return target, categorical_columns, numeric_columns
target, categorical_columns, numeric_columns = get_feature_names(df )<train_model>
pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1 )
def balancingClassesRus(x_train, y_train):
rus = RandomUnderSampler(random_state=7)
 features_balanced, target_balanced = rus.fit_resample(x_train, y_train)
 print("Count for each class value after RandomUnderSampler:", collections.Counter(target_balanced))
return features_balanced, target_balanced
def balancingClassesSmoteenn(x_train, y_train):
smn = SMOTEENN(random_state=7)
 features_balanced, target_balanced = smn.fit_resample(x_train, y_train)
 print("Count for each class value after SMOTEEN:", collections.Counter(target_balanced))
return features_balanced, target_balanced
def balancingClassesSmote(x_train, y_train):
sm = SMOTE(random_state=7)
 features_balanced, target_balanced = sm.fit_resample(x_train, y_train)
 print("Count for each class value after SMOTE:", collections.Counter(target_balanced))
return features_balanced, target_balanced<compute_train_metric>
def calc_threshold(pred):
threshold_dic = {}
for idx in tqdm(range(28)) :
m = 0
for ii in range(100):
threshold0 = ii*0.01
f1_val = f1_score(y_cat_train_dic[idx], threshold0<(pred[:,idx]))
if m < f1_val:
threshold_dic[idx] = threshold0+0.005
m = f1_val
return threshold_dic
def testSetResultsClassifier(classifier, x_test, y_test):
results = []
predictions = classifier.predict(x_test)
 predicted_probas_class1 = classifier.predict_proba(x_test)[:, 1]
roc_auc = roc_auc_score(y_test, predicted_probas_class1)
 avg_precision = average_precision_score(y_test, predicted_probas_class1)
 accuracy = accuracy_score(y_test, predictions)
 precision = precision_score(y_test, predictions)
 recall = recall_score(y_test, predictions)
 f1 = f1_score(y_test, predictions)
 results.append(accuracy)
 results.append(precision)
 results.append(recall)
 results.append(f1)
 results.append(avg_precision)
 results.append(roc_auc)
 print("
")
 print("Accuracy:", round(results[0], 3))
print("Precision:", round(results[1], 3))
print("Recall:", round(results[2], 3))
print("F1-Score:", round(results[3], 3))
print("Average Precision(Precision/Recall AUC):", round(results[4], 3))
print("ROC_AUC:", round(results[5], 3))
return results<split>
threshold_dic = calc_threshold(pred)
 threshold_dic
def defineBestModelPipeline(df, target, numeric_columns, categorical_columns):
x_train, x_test, y_train, y_test = train_test_split(df, target, test_size=0.10, random_state=42)
 y_train = y_train.to_numpy()
y_test = y_test.to_numpy()
numeric_transformer_1 = Pipeline(steps=[('imp', IterativeImputer(max_iter=30, random_state=42)) , 
('scaler', MinMaxScaler())])
 numeric_transformer_2 = Pipeline(steps=[('imp', SimpleImputer(strategy='mean')) , 
('scaler', StandardScaler())])
 categorical_transformer_1 = Pipeline(steps=[('frequent', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(use_cat_names=True)) ])
 categorical_transformer_2 = Pipeline(steps=[('frequent', SimpleImputer(strategy='most_frequent')) , 
('targetencoder', TargetEncoder(min_samples_leaf=3, smoothing=3)) ])
 data_transformations_1 = ColumnTransformer(transformers=[('num', numeric_transformer_1, numeric_columns), 
('cat', categorical_transformer_1, categorical_columns)])
 data_transformations_2 = ColumnTransformer(transformers=[('num', numeric_transformer_1, numeric_columns), 
('cat', categorical_transformer_2, categorical_columns)])
 data_transformations_3 = ColumnTransformer(transformers=[('num', numeric_transformer_2, numeric_columns), 
('cat', categorical_transformer_1, categorical_columns)])
 data_transformations_4 = ColumnTransformer(transformers=[('num', numeric_transformer_2, numeric_columns), 
('cat', categorical_transformer_2, categorical_columns)])
 pipe = Pipeline(steps=[('data_transformations', data_transformations_1), 
('feature_transf', PCA()), 
('clf', SVC())])
 params_grid = [
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [SelectPercentile(percentile=75),
SelectFromModel(DecisionTreeClassifier() , threshold=-np.inf, max_features=x_train.shape[1]*0.75),
PCA(n_components=round(x_train.shape[1]*0.75)) ,
PCA(0.75)],
'clf': [KNeighborsClassifier() ],
'clf__n_neighbors': stats.randint(1, 30),
'clf__metric': ['minkowski', 'euclidean']},
{'data_transformations': [data_transformations_1, data_transformations_3],
'feature_transf': [None],
'clf': [LogisticRegression() ],
'clf__penalty': ['l1', 'l2'],
'clf__C': stats.uniform(0.01, 10)},
{'data_transformations': [data_transformations_1, data_transformations_3],
'feature_transf': [None],
'clf': [SVC() ],
'clf__C': stats.uniform(0.1, 10),
'clf__gamma': stats.uniform(0.1, 10),
'clf__kernel': ['linear', 'poly', 'rbf']},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [DecisionTreeClassifier() ],
'clf__criterion': ['gini', 'entropy'],
'clf__max_features': [None, "sqrt", "log2"],
'clf__max_depth': [None, stats.randint(1, 7)]},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [RandomForestClassifier() ],
'clf__n_estimators': stats.randint(10, 175),
'clf__max_features': [None, "auto", "log2"],
'clf__max_depth': [None, stats.randint(1, 5)],
'clf__random_state': stats.randint(1, 49)},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [LGBMClassifier() ],
'clf__n_estimators': stats.randint(1, 100),
'clf__learning_rate': stats.uniform(0.01, 0.7),
'clf__max_depth': [None, stats.randint(1, 6)]},
{'data_transformations': [data_transformations_2, data_transformations_4],
'feature_transf': [None],
'clf': [XGBClassifier() ],
'clf__n_estimators': stats.randint(5, 125),
'clf__eta': stats.uniform(0.01, 1),
'clf__max_depth': [None, stats.randint(1, 6)],
'clf__gamma': stats.uniform(0.01, 1)},
]
metrics = ['accuracy', 'precision', 'recall', 'f1', 'average_precision', 'roc_auc']
cross_validator = StratifiedShuffleSplit(n_splits=10, train_size=0.8, test_size=0.2, random_state=49)
 best_model_pipeline = RandomizedSearchCV(estimator=pipe, param_distributions=params_grid,
n_iter=50, scoring=metrics, refit='roc_auc',
n_jobs=-1, cv=cross_validator, random_state=7)
 best_model_pipeline.fit(x_train, y_train)
 print("
", best_model_pipeline.best_estimator_[0])
 print("
", best_model_pipeline.best_estimator_[1])
 print("
", best_model_pipeline.best_estimator_[2])
 print("
", best_model_pipeline.best_score_)
 return x_train, x_test, y_train, y_test, best_model_pipeline<find_best_params>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
x_test = best_model_pipeline.best_estimator_[0].transform(x_test)
 precision, recall, thresholds_prc = precision_recall_curve(y_test, best_model_pipeline.best_estimator_[2].predict_proba(x_test)[:, 1])
 closest_to_025_prc = np.argmin(np.abs(thresholds_prc - 0.25))
closest_to_default_prc = np.argmin(np.abs(thresholds_prc - 0.5))
closest_to_075_prc = np.argmin(np.abs(thresholds_prc - 0.75))
plt.plot(precision, recall, label="Results, Pecision Recall Curve")
 plt.plot(precision[closest_to_025_prc], recall[closest_to_025_prc], 'v', c='k',
markersize=10, label="Threshold 0.25", fillstyle="none", mew=2)
 plt.plot(precision[closest_to_default_prc], recall[closest_to_default_prc], 's', c='k',
markersize=10, label="Default threshold(0.5)", fillstyle="none", mew=2)
 plt.plot(precision[closest_to_075_prc], recall[closest_to_075_prc], '^', c='k',
markersize=10, label="Threshold 0.75", fillstyle="none", mew=2)
 plt.title("Precision-Recall Curve for the best model found")
 plt.xlabel("Precision: TP /(TP + FP)")
 plt.ylabel("Recall: TP /(TP + FN)")
 plt.legend(loc="best" )<find_best_params>
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1 )
fpr, tpr, thresholds_roc = roc_curve(y_test, best_model_pipeline.best_estimator_[2].predict_proba(x_test)[:, 1])
 closest_to_025_roc = np.argmin(np.abs(thresholds_roc - 0.25))
closest_to_default_roc = np.argmin(np.abs(thresholds_roc - 0.5))
closest_to_075_roc = np.argmin(np.abs(thresholds_roc - 0.75))
plt.plot(fpr, tpr, label="Results, ROC Curve")
 plt.plot(fpr[closest_to_025_roc], tpr[closest_to_025_roc], 'v', c='k',
markersize=10, label="Threshold 0.25", fillstyle="none", mew=2)
 plt.plot(fpr[closest_to_default_roc], tpr[closest_to_default_roc], 's', c='k',
markersize=10, label="Default threshold(0.5)", fillstyle="none", mew=2)
 plt.plot(fpr[closest_to_075_roc], tpr[closest_to_075_roc], '^', c='k',
markersize=10, label="Threshold 0.75", fillstyle="none", mew=2)
 plt.title("ROC Curve for the best model found")
 plt.xlabel("FPR: FP /(FP + TN)")
 plt.ylabel("TPR(Recall): TP /(TP + FN)")
 plt.legend(loc="best" )<create_dataframe>
def make_test(pred):
test_labels1 = test_labels.copy()
test_labels1['Predicted'] = [str(ee)for ee in np.argmax(pred, axis=1)]
print(test_labels1.head())
 test_labels2 = test_labels1.copy()
for ii in range(test_labels2.shape[0]):
threshold = list(zip(*sorted(list(threshold_dic.items()), key=lambda x:x[0], reverse=False)))[1]
idx = threshold < pred[ii,:]
tgt = test_labels2['Predicted'][ii]
tgt = [tgt] + [str(ee)for ee in np.arange(28)[idx]]
tgt = set(tgt)
 tgt = ' '.join(tgt)
 test_labels2['Predicted'][ii] = tgt
print(test_labels2.head())
 return test_labels1, test_labels2
df_results = pd.DataFrame(best_model_pipeline.cv_results_)
 display(df_results )<categorify>
test_labels1_1, test_labels1_2 = make_test(pred_test )
feature_names_in_order = numeric_columns + categorical_columns
print(feature_names_in_order )<create_dataframe>
test_labels1_2.to_csv('InceptionResNetV1_2.csv', index=False )
Features chosen by Feature Selector:", best_model_pipeline.best_estimator_.named_steps['feature_transf'].get_support())
<load_from_csv>

 No = 1
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_test['AgeCat'] = ''
df_test['AgeCat'].loc[(df_test['Age'] < 18)] = 'young'
df_test['AgeCat'].loc[(df_test['Age'] >= 18)&(df_test['Age'] < 56)] = 'mature'
df_test['AgeCat'].loc[(df_test['Age'] >= 56)] = 'senior'
df_test['FamilySize'] = ''
df_test['FamilySize'].loc[(df_test['SibSp'] <= 2)] = 'small'
df_test['FamilySize'].loc[(df_test['SibSp'] > 2)&(df_test['SibSp'] <= 5)] = 'medium'
df_test['FamilySize'].loc[(df_test['SibSp'] > 5)] = 'large'
df_test['IsAlone'] = ''
df_test['IsAlone'].loc[(( df_test['SibSp'] + df_test['Parch'])> 0)] = 'no'
df_test['IsAlone'].loc[(( df_test['SibSp'] + df_test['Parch'])== 0)] = 'yes'
df_test['SexCat'] = ''
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(df_test['Age'] <= 21)] = 'youngmale'
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(( df_test['Age'] > 21)&(df_test['Age'])< 50)] = 'maturemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'male')&(df_test['Age'] > 50)] = 'seniormale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(df_test['Age'] <= 21)] = 'youngfemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(( df_test['Age'] > 21)&(df_test['Age'])< 50)] = 'maturefemale'
df_test['SexCat'].loc[(df_test['Sex'] == 'female')&(df_test['Age'] > 50)] = 'seniorfemale'
df_test['Title'] = df_test['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
df_test['Is_Married'] = 0
df_test['Is_Married'].loc[df['Title'] == 'Mrs'] = 1
df_test['Title'] = df_test['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 df_test['Title'] = df_test['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 df_test['Ticket_Frequency'] = df_test.groupby('Ticket')['Ticket'].transform('count')
 df_test.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True )<predict_on_test>
ls -la
test_predictions = best_model_pipeline.predict(df_test)
 print(test_predictions )<load_from_csv>
seq = Seq(train_labels, extend=False, aug=True, batch_size=32)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=2,
steps_per_epoch=len(seq),
callbacks=[] )
df_submission = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_submission['Survived'] = test_predictions
df_submission.drop(df_submission.columns.difference(['PassengerId', 'Survived']), axis=1, inplace=True)
 df_submission.head(10 )<count_values>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
df_submission.count()<save_to_csv>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels2_1, test_labels2_2 = make_test(pred_test)
 test_labels2_2.head()
df_submission.to_csv('submission.csv', index=False )<feature_engineering>
test_labels2_2.to_csv('InceptionResNetV2_2.csv', index=False )
def definePCAPipeline(numeric_columns):
numeric_transformer = Pipeline(steps=[('imp', SimpleImputer(strategy='mean')) , 
('scaler', StandardScaler())])
 data_transformations = ColumnTransformer(transformers=[('num', numeric_transformer, numeric_columns)])
 pca_pipeline = Pipeline(steps=[('data_transformations', data_transformations), 
('feature_transf', PCA(n_components=2, whiten=True)) ])
 return pca_pipeline<prepare_output>

 No = 2
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
pca_df = pd.DataFrame(pca_arr, columns=["PC1", "PC2"])
 pca_df['Survived'] = target
pca_df.head(10 )<categorify>
seq = Seq(train_labels, aug=True, batch_size=32)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=2,
steps_per_epoch=len(seq),
callbacks=[] )
transformed_df = pca_pipeline[0].transform(df)
 tsne = TSNE(n_components=2, random_state=42)
 tsne_arr = tsne.fit_transform(transformed_df )<prepare_output>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
tsne_df = pd.DataFrame(tsne_arr, columns=["tsne_dim1", "tsne_dim2"])
 tsne_df['Survived'] = target
tsne_df.head(10 )<init_hyperparams>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels3_1, test_labels3_2 = make_test(pred_test)
 test_labels3_2.head()
def findOptimalNClustersKMeans(transformed_df):
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]
silhouette_scores = []
for n_clusters in range_n_clusters:
kmeans = KMeans(n_clusters=n_clusters, random_state=7)
 cluster_labels = kmeans.fit_predict(transformed_df)
 silhouette_avg = silhouette_score(transformed_df, cluster_labels)
 print("K-Means: for n_clusters =", n_clusters, ", the average silhouette_score is", silhouette_avg)
 silhouette_scores.append(silhouette_avg)
 return range_n_clusters, silhouette_scores
range_n_clusters, silhouette_scores = findOptimalNClustersKMeans(transformed_df )<predict_on_test>
test_labels3_2.to_csv('InceptionResNetV3_2.csv', index=False )
kmeans = KMeans(n_clusters=2, random_state=7)
 kmeans_defined_clusters = kmeans.fit_predict(transformed_df)
 pca_df['KMeans_Defined_Clusters'] = ''
pca_df['KMeans_Defined_Clusters'] = kmeans_defined_clusters
pca_df['KMeans_Defined_Clusters'] = pca_df['KMeans_Defined_Clusters'].astype(str)
 pca_df.head(10 )<prepare_output>

 No = 3
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
pca_cluster_centers = pca_pipeline[1].transform(kmeans.cluster_centers_)
 centroids_df = pd.DataFrame(pca_cluster_centers, columns=["X_coord", "Y_coord"])
 centroids_df['cluster_centroid'] = ""
centroids_df['cluster_centroid'][0] = 'centroid, cluster 0'
centroids_df['cluster_centroid'][1] = 'centroid, cluster 1'
centroids_df.head(10 )<compute_train_metric>
def lr_schedule(epoch):
lr = 1e-4
print('Learning rate: ', lr)
 return lr
def findOptimalNClustersAC(transformed_df):
range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]
silhouette_scores = []
for n_clusters in range_n_clusters:
ac = AgglomerativeClustering(n_clusters=n_clusters)
 cluster_labels = ac.fit_predict(transformed_df)
 silhouette_avg = silhouette_score(transformed_df, cluster_labels)
 print("Agglomerative Clustering: for n_clusters =", n_clusters, ", the average silhouette_score is", silhouette_avg)
 silhouette_scores.append(silhouette_avg)
 return range_n_clusters, silhouette_scores
range_n_clusters_ac, silhouette_scores_ac = findOptimalNClustersAC(transformed_df )<train_on_grid>
lr_scheduler = LearningRateScheduler(lr_schedule)
 callbacks = [lr_scheduler]
seq = Seq(train_labels, aug=True, batch_size=32)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=2,
steps_per_epoch=len(seq),
callbacks=callbacks )
ac = AgglomerativeClustering(n_clusters=2)
 ac_defined_clusters = ac.fit_predict(transformed_df)
 pca_df['AgglomerativeClustering_Defined_Clusters'] = ''
pca_df['AgglomerativeClustering_Defined_Clusters'] = ac_defined_clusters
pca_df['AgglomerativeClustering_Defined_Clusters'] = pca_df['AgglomerativeClustering_Defined_Clusters'].astype(str)
 pca_df.head(10 )<find_best_params>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
dbscan = DBSCAN(min_samples=20, eps=2.4)
 dbscan_defined_clusters = dbscan.fit_predict(transformed_df)
 silhouette_dbscan = silhouette_score(transformed_df, dbscan_defined_clusters)
 print("DBSCAN: for eps = 2.4 and min_samples = 20, the average silhouette_score is", silhouette_dbscan )<data_type_conversions>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels4_1, test_labels4_2 = make_test(pred_test)
 test_labels4_2.head()
pca_df['DBSCAN_Defined_Clusters'] = ''
pca_df['DBSCAN_Defined_Clusters'] = dbscan_defined_clusters
pca_df['DBSCAN_Defined_Clusters'] = pca_df['DBSCAN_Defined_Clusters'].astype(str)
 pca_df.head(10 )<load_from_csv>
test_labels4_2.to_csv('InceptionResNetV4_2.csv', index=False )
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 passenger_ids = test_df['PassengerId']<count_values>

 No = 4
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
train_df['Survived'].value_counts()<feature_engineering>
def lr_schedule(epoch):
lr = 1e-4
print('Learning rate: ', lr)
 return lr
hist_survived_vs_feature('SibSp' )<feature_engineering>
lr_scheduler = LearningRateScheduler(lr_schedule)
 callbacks = [lr_scheduler]
seq = Seq(train_labels, aug=True, batch_size=32)
 print(len(seq))
hst = models['model'].fit_generator(seq, epochs=2,
steps_per_epoch=len(seq),
callbacks=callbacks )
dataset = [train_df, test_df]
for df in dataset:
df['Title'] = df['Name'].map(lambda x: re.search(r'([A-Za-z]+)\.', x ).group().strip().replace('.', ''))
train_df['Title'].value_counts().index<count_values>
seq_pred = Seq(train_labels, test=False, aug=False, batch_size=128)
 len(seq_pred)
 pred = models['model'].predict_generator(seq_pred, steps=len(seq_pred), verbose=1)
 threshold_dic = calc_threshold(pred)
 threshold_dic
test_df['Title'].value_counts().index<categorify>
seq_test = Seq(test_labels, test=True, aug=False, batch_size=128)
 seq_test
pred_test = models['model'].predict_generator(seq_test, steps=len(seq_test), verbose=1)
 test_labels5_1, test_labels5_2 = make_test(pred_test)
 test_labels5_2.head()
title_mapping = {'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Dr': 5, 'Rev': 5, 'Col': 5,
'Major': 5, 'Mlle': 5, 'Ms': 5, 'Countess': 5, 'Lady': 5, 'Capt': 5,
'Jonkheer': 5, 'Don': 5, 'Sir': 5, 'Mme': 5}
for df in dataset:
df['Title'] = df['Title'].map(title_mapping)
 train_df.head()<categorify>
test_labels5_2.to_csv('InceptionResNetV5_2.csv', index=False )
def concat_dummies(feature_name):
global dataset
new_train_df = pd.concat([train_df, pd.get_dummies(train_df[feature_name], prefix=feature_name)], axis=1)
 new_test_df = pd.concat([test_df, pd.get_dummies(test_df[feature_name], prefix=feature_name)], axis=1)
 dataset = [new_train_df, new_test_df]
return new_train_df, new_test_df<categorify>

 No = 5
models['model_cnvt'].save_weights('model_{}_cnvt.h5'.format(No))
models['model_resnet'].save_weights('model_{}_resnet.h5'.format(No))
models['model_classifier'].save_weights('model_{}_classifier.h5'.format(No))
train_df, test_df = concat_dummies('Title')
 train_df.head()<categorify>
ls -la
sex_mapping = {'male': 0, 'female': 1}
for df in dataset:
df['Sex'] = df['Sex'].map(sex_mapping)
 train_df.head()<categorify>
seq = Seq(train_labels, extend=False, aug=False, batch_size=32)
 print(len(seq))
xs, ys = next(seq)
 print(xs['input'].shape)
 y_pred = models['model_cnvt'].predict(xs['input'])
 print(y_pred.shape )
<normalization><EOS> 
Image.fromarray(np.uint8(( y_pred[0]+1)/2*255))