Kaggle competition: Titanic - Machine Learning from Disaster
learn.lr_find()
learn.recorder.plot()<train_model>
%matplotlib inline
lr = 1e-2
learn.fit_one_cycle(3, lr )<normalization>
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )
learn.data = data =(
 src.transform(tfms,size=224)
.databunch() 
.normalize(imagenet_stats)
)
 learn.lr_find()
learn.recorder.plot()<train_model>
data = [["Variable","Definition","Datatype","Key"],
["Passenger ID","Index for the observational unit","Int", "1-1309"],
["Pclass","boarding class of the observational unit","Int", "1-3"],
["Name","Name of observational unit, including the title","String","Multiple"],
["Sex","Gender of the observational unit","String", "male/female"],
["Age","Age of the observational unit","Double","0,17 - 76"],
["SibSp","number of siblings/spouse(s)of the observational unit aboard the Titanic", "Int", "0, 1, 2, 3, 4, 5, 8"],
["Parch","
["Ticket","Ticket number","Int","Multiple"],
["Fare", "Fare paid for Ticket", "float", "0,0000 - 512.329200"],
["Cabin","Cabin number, including the info where this cabin is located","Variant", "Multiple"],
["Embarked","Port of Embarkation","String","'Q', 'S', 'C'"]
]
display(HTML(
 '<table><tr>{}</tr></table>'.format(
 '</tr><tr>'.join(
 '<td>{}</td>'.format('</td><td>'.join(str(_)for _ in row)) for row in data)
)
))
lr = 1e-2
learn.fit_one_cycle(20, lr )<find_best_params>
train.isnull().any()
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()<train_model>
test.isnull().any()
learn.fit_one_cycle(8, slice(1e-6,1e-3))<predict_on_test>
train.loc[train["Age"].isnull() ==True,"Age"]=np.mean(train["Age"] )
valid_preds = learn.get_preds(ds_type=DatasetType.Valid )<compute_test_metric>
test.loc[test["Age"].isnull() ==True,"Age"]=np.mean(test["Age"] )
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<train_model>
train.loc[train["Fare"].isnull() ==True,"Fare"]=np.mean(train["Fare"] )
optR = OptimizedRounder()
optR.fit(valid_preds[0],valid_preds[1] )<load_from_csv>
test.loc[test["Fare"].isnull() ==True,"Fare"]=np.mean(test["Fare"] )
sample_df = pd.read_csv(PATH/'sample_submission.csv')
 sample_df.head()<define_variables>
gmm=GaussianMixture(n_components=10,max_iter=500,random_state=0)
 age=train["Age"]
age=age.values
age=age.reshape(-1,1)
 gmm.fit(age )
learn.data.add_test(ImageList.from_df(sample_df,PATH,folder='test_images',suffix='.png'))<predict_on_test>
log_likelihood=gmm.score_samples(age )
preds,y = learn.get_preds(DatasetType.Test)
<predict_on_test>
detected_outliers_age=np.where(log_likelihood<-8)[0]
test_predictions = optR.predict(preds, coefficients )<data_type_conversions>
train.iloc[detected_outliers_age]
sample_df.diagnosis = test_predictions.astype(int)
 sample_df.head()<save_to_csv>
gmm=GaussianMixture(n_components=80,max_iter=500,random_state=0)
 SibSp=train["SibSp"]
SibSp=SibSp.values
SibSp=SibSp.reshape(-1,1)
 gmm.fit(SibSp )
sample_df.to_csv('submission.csv',index=False )<import_modules>
detected_outliers_SibSp=np.where(log_likelihood<1.5)[0]
base_dir = ".. /input/aptos2019-blindness-detection/"
train_csv = base_dir+"train.csv"
test_csv = base_dir+"test.csv"
test_dir = base_dir+"test_images/"
test_dir_processed = 'test_dir_processed'
train_dir = "train_data_cropped"
IMG_SIZE = 224
SEED = 72 
<install_modules>
train.iloc[detected_outliers_SibSp]
<import_modules>
gmm=GaussianMixture(n_components=80,max_iter=500,random_state=0)
 parch=train["Parch"]
parch=parch.values
parch=parch.reshape(-1,1)
 gmm.fit(parch )
sys.path.append(os.path.abspath('.. /input/efficientnet/efficientnet-master/efficientnet-master/')) 
<import_modules>
detected_outliers_parch=np.where(log_likelihood<0)[0]
import cv2
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.utils import class_weight, shuffle<train_model>
train.iloc[detected_outliers_parch]
image = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/295fdc964f6e.png')
 plt.imshow(image )<define_variables>
fare=train["Fare"]
fare=fare.values
fare=fare.reshape(-1,1)
 gmm=GaussianMixture(n_components=3,max_iter=100,random_state=0)
 gmm.fit(fare )
i = 0
for fileName in os.listdir("train_data_cropped/"):
i = i + 1
print(i )<define_variables>
detected_outliers_fare=np.where(log_likelihood<-10)[0]
i = 0
for fileName in os.listdir("test_dir_processed/"):
i = i + 1
print(i )<train_on_grid>
outliers=(detected_outliers_parch,detected_outliers_SibSp,detected_outliers_fare,detected_outliers_age)
 outliers=np.concatenate(outliers)
 outliers
image1 = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/295fdc964f6e.png')
 image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
 plt.imshow(image1 )<define_variables>
train=train.drop(index=outliers,axis=1)
 train.shape
WORKERS = 2
CHANNEL = 3
warnings.filterwarnings("ignore")
 NUM_CLASSES = 5
SEED = 72
TRAIN_NUM = 1000<split>
data=pd.concat(objs=[train.drop(labels="Survived",axis=1), test], axis=0 ).reset_index(drop=True)
def df_train_test_split_preprocess(df):
image_ids = df["id_code"].values.tolist()
labels = df["diagnosis"].values.tolist()
for i in range(len(image_ids)) :
imgname = image_ids[i]
newname = str(imgname)+ ".png"
image_ids[i] = newname
xtrain, xval, ytrain, yval = train_test_split(image_ids, labels, test_size = 0.15)
 df_train = pd.DataFrame({"id_code":xtrain, "diagnosis":ytrain})
 df_val = pd.DataFrame({"id_code":xval, "diagnosis":yval})
 df_train["diagnosis"] = df_train["diagnosis"].astype('str')
 df_val["diagnosis"] = df_val["diagnosis"].astype('str')
 print("Length of Training Data :",len(df_train))
print("Length of Validation Data :",len(df_val))
return df_train, df_val<split>
dist_Pclass=train["Pclass"].value_counts(sort=False)
 dist_Pclass.rename(index={1:"First class",2:"Second class",3:"Third class"},inplace=True)
 dist_Pclass_Norm=dist_Pclass/dist_Pclass.sum()
print("Abs.values
{0}
".format(dist_Pclass))
print("Rel.values
{0}".format(dist_Pclass_Norm))
df = pd.read_csv(train_csv)
 df_train, df_val = df_train_test_split_preprocess(df )<import_modules>
dist_sex=train["Sex"].value_counts(sort=False)
 dist_sex_Norm=dist_sex/dist_sex.sum()
print("Abs.values
{0}
".format(dist_sex))
print("Rel.values
{0}".format(dist_sex_Norm))
from sklearn.metrics import f1_score, fbeta_score, cohen_kappa_score<set_options>
data["title"]=data["Name"].apply(extract)
 train["title"]=train["Name"].apply(extract)
 data["title"].value_counts()
train_aug = ImageDataGenerator(rescale=1./255,
horizontal_flip = True,
zoom_range = 0.15,
vertical_flip = True,
shear_range=0.1,
rotation_range = 90
)<create_dataframe>
data.loc[(data["title"]=="Ms.")|(data["title"]=="Mlle.")|(data["title"]=="Mme."),"title"]="Miss."
data.loc[(data["title"]=="Master.")|(data["title"]=="Dr.")|(data["title"]=="Rev.")|(data["title"]=="Col.")|(data["title"]=="Major.")|(data["title"]=="Dona.")|(data["title"]=="Don.")|(data["title"]=="Capt.")|(data["title"]=="Sir.")|(data["title"]=="Jonkheer.")|(data["title"]=="Countess.")|(data["title"]=="Lady."),"title"]="wellSituated"
train.loc[(train["title"]=="Ms.")|(train["title"]=="Mlle.")|(train["title"]=="Mme."),"title"]="Miss."
train.loc[(train["title"]=="Master.")|(train["title"]=="Dr.")|(train["title"]=="Rev.")|(train["title"]=="Col.")|(train["title"]=="Major.")|(train["title"]=="Dona.")|(train["title"]=="Don.")|(train["title"]=="Capt.")|(train["title"]=="Sir.")|(train["title"]=="Jonkheer.")|(train["title"]=="Countess.")|(train["title"]=="Lady."),"title"]="wellSituated"
data["title"].value_counts()
train_generator = train_aug.flow_from_dataframe(dataframe = df_train,
directory = train_dir,
x_col = "id_code",
y_col = "diagnosis",
batch_size = 16,
target_size =(IMG_SIZE, IMG_SIZE),
class_mode = "categorical" )<create_dataframe>
data["title"]=data["title"].map({"Mr.":0,"Miss.":1,"Mrs.":2,"wellSituated":3})
 train["title"]=train["title"].map({"Mr.":0,"Miss.":1,"Mrs.":2,"wellSituated":3})
 data.head(5 )
validation_generator = train_aug.flow_from_dataframe(dataframe = df_val,
directory = train_dir,
x_col = "id_code",
y_col = "diagnosis",
batch_size = 16,
target_size =(IMG_SIZE, IMG_SIZE),
class_mode = "categorical" )<compute_test_metric>
train=pd.get_dummies(data=train,columns=["title"],prefix="title")
 data=pd.get_dummies(data=data,columns=["title"],prefix="title")
 train.head(5)
 train=train.drop(labels="Name",axis=1)
 data=data.drop(labels="Name",axis=1 )
def cohens_kappa(y_true, y_pred):
y_true_classes = tf.argmax(y_true, 1)
 y_pred_classes = tf.argmax(y_pred, 1)
 ck_val = tf.contrib.metrics.cohen_kappa(y_true_classes, y_pred_classes, 5)[1]
print(ck_val)
 return ck_val 
<feature_engineering>
labelencoder_X_1=LabelEncoder()
train["Sex"]=labelencoder_X_1.fit_transform(train.iloc[:,3])
 data["Sex"]=labelencoder_X_1.fit_transform(data.iloc[:,7])
 data.head(5)
test_df_orig = pd.read_csv(test_csv)
 def process_test_df(test_df):
test_ids = test_df["id_code"].values.tolist()
for i in range(len(test_ids)) :
imgname = test_ids[i]
newname = str(imgname)+ ".png"
test_ids[i] = newname
test_df["id_code"] = test_ids
return test_df
test_df = process_test_df(test_df_orig )<create_dataframe>
print(train["Cabin"].isnull().any())
 print(data["Cabin"].isnull().any() )
test_aug = ImageDataGenerator(rescale = 1./255)
 test_generator = test_aug.flow_from_dataframe(dataframe = test_df,
directory = test_dir_processed,
x_col = "id_code",
batch_size = 1,
target_size =(IMG_SIZE, IMG_SIZE),
shuffle = False,
class_mode = None )<choose_model_class>
train=pd.get_dummies(data=train,columns=["Cabin"],prefix="Cabin")
 data=pd.get_dummies(data=data,columns=["Cabin"],prefix="Cabin")
 train.head(5 )
class RAdam(keras.optimizers.Optimizer):

 def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0., weight_decay=0., amsgrad=False,
total_steps=0, warmup_proportion=0.1, min_lr=0., **kwargs):
super(RAdam, self ).__init__(**kwargs)
 with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
 self.lr = K.variable(lr, name='lr')
 self.beta_1 = K.variable(beta_1, name='beta_1')
 self.beta_2 = K.variable(beta_2, name='beta_2')
 self.decay = K.variable(decay, name='decay')
 self.weight_decay = K.variable(weight_decay, name='weight_decay')
 self.total_steps = K.variable(total_steps, name='total_steps')
 self.warmup_proportion = K.variable(warmup_proportion, name='warmup_proportion')
 self.min_lr = K.variable(lr, name='min_lr')
 if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.initial_weight_decay = weight_decay
self.initial_total_steps = total_steps
self.amsgrad = amsgrad
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
 self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr *(1./(1.+ self.decay * K.cast(self.iterations, K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx())+ 1
if self.initial_total_steps > 0:
warmup_steps = self.total_steps * self.warmup_proportion
decay_steps = self.total_steps - warmup_steps
lr = K.switch(
 t <= warmup_steps,
lr *(t / warmup_steps),
lr *(1.0 - K.minimum(t, decay_steps)/ decay_steps), 
)
 ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='m_' + str(i)) for(i, p)in enumerate(params)]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='v_' + str(i)) for(i, p)in enumerate(params)]
if self.amsgrad:
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='vhat_' + str(i)) for(i, p)in enumerate(params)]
else:
vhats = [K.zeros(1, name='vhat_' + str(i)) for i in range(len(params)) ]
self.weights = [self.iterations] + ms + vs + vhats
beta_1_t = K.pow(self.beta_1, t)
 beta_2_t = K.pow(self.beta_2, t)
 sma_inf = 2.0 /(1.0 - self.beta_2)- 1.0
sma_t = sma_inf - 2.0 * t * beta_2_t /(1.0 - beta_2_t)
 for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
m_t =(self.beta_1 * m)+(1.- self.beta_1)* g
v_t =(self.beta_2 * v)+(1.- self.beta_2)* K.square(g)
 m_corr_t = m_t /(1.0 - beta_1_t)
 if self.amsgrad:
vhat_t = K.maximum(vhat, v_t)
 v_corr_t = K.sqrt(vhat_t /(1.0 - beta_2_t)+ self.epsilon)
 self.updates.append(K.update(vhat, vhat_t))
else:
v_corr_t = K.sqrt(v_t /(1.0 - beta_2_t)+ self.epsilon)
 r_t = K.sqrt(( sma_t - 4.0)/(sma_inf - 4.0)* 
(sma_t - 2.0)/(sma_inf - 2.0)*
sma_inf / sma_t)
 p_t = K.switch(sma_t > 5, r_t * m_corr_t / v_corr_t, m_corr_t)
 if self.initial_weight_decay > 0:
p_t += self.weight_decay * p
p_t = p - lr * p_t
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
if getattr(p, 'constraint', None)is not None:
new_p = p.constraint(new_p)
 self.updates.append(K.update(p, new_p))
return self.updates
def get_config(self):
config = {
'lr': float(K.get_value(self.lr)) ,
'beta_1': float(K.get_value(self.beta_1)) ,
'beta_2': float(K.get_value(self.beta_2)) ,
'decay': float(K.get_value(self.decay)) ,
'weight_decay': float(K.get_value(self.weight_decay)) ,
'epsilon': self.epsilon,
'amsgrad': self.amsgrad,
'total_steps': float(K.get_value(self.total_steps)) ,
'warmup_proportion': float(K.get_value(self.warmup_proportion)) ,
'min_lr': float(K.get_value(self.min_lr)) ,
}
base_config = super(RAdam, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))<import_modules>
train.loc[train["Embarked"].isnull() ==True,"Embarked"]="S"
data.loc[data["Embarked"].isnull() ==True,"Embarked"]="S"
train=pd.get_dummies(data=train,columns=["Embarked"],prefix="Embarked")
 data=pd.get_dummies(data=data,columns=["Embarked"],prefix="Embarked" )
from keras.applications import ResNet50
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D, Dropout, Input
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
import keras
from keras.engine import Layer,InputSpec 
<choose_model_class>
train=train.drop(labels="Ticket",axis=1)
 data=data.drop(labels="Ticket",axis=1 )
class GroupNormalization(Layer):

 def __init__(self,
groups=32,
axis=-1,
epsilon=1e-5,
center=True,
scale=True,
beta_initializer='zeros',
gamma_initializer='ones',
beta_regularizer=None,
gamma_regularizer=None,
beta_constraint=None,
gamma_constraint=None,
**kwargs):
super(GroupNormalization, self ).__init__(**kwargs)
 self.supports_masking = True
self.groups = groups
self.axis = axis
self.epsilon = epsilon
self.center = center
self.scale = scale
self.beta_initializer = initializers.get(beta_initializer)
 self.gamma_initializer = initializers.get(gamma_initializer)
 self.beta_regularizer = regularizers.get(beta_regularizer)
 self.gamma_regularizer = regularizers.get(gamma_regularizer)
 self.beta_constraint = constraints.get(beta_constraint)
 self.gamma_constraint = constraints.get(gamma_constraint)
 def build(self, input_shape):
dim = input_shape[self.axis]
if dim is None:
raise ValueError('Axis ' + str(self.axis)+ ' of '
'input tensor should have a defined dimension '
'but the layer received an input with shape ' +
str(input_shape)+ '.')
 if dim < self.groups:
raise ValueError('Number of groups(' + str(self.groups)+ ')cannot be '
'more than the number of channels(' +
str(dim)+ ' ).')
 if dim % self.groups != 0:
raise ValueError('Number of groups(' + str(self.groups)+ ')must be a '
'multiple of the number of channels(' +
str(dim)+ ' ).')
 self.input_spec = InputSpec(ndim=len(input_shape),
axes={self.axis: dim})
 shape =(dim,)
 if self.scale:
self.gamma = self.add_weight(shape=shape,
name='gamma',
initializer=self.gamma_initializer,
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint)
 else:
self.gamma = None
if self.center:
self.beta = self.add_weight(shape=shape,
name='beta',
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
constraint=self.beta_constraint)
 else:
self.beta = None
self.built = True
def call(self, inputs, **kwargs):
input_shape = K.int_shape(inputs)
 tensor_input_shape = K.shape(inputs)
 reduction_axes = list(range(len(input_shape)))
 del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
 broadcast_shape[self.axis] = input_shape[self.axis] // self.groups
broadcast_shape.insert(1, self.groups)
 reshape_group_shape = K.shape(inputs)
 group_axes = [reshape_group_shape[i] for i in range(len(input_shape)) ]
group_axes[self.axis] = input_shape[self.axis] // self.groups
group_axes.insert(1, self.groups)
 group_shape = [group_axes[0], self.groups] + group_axes[2:]
group_shape = K.stack(group_shape)
 inputs = K.reshape(inputs, group_shape)
 group_reduction_axes = list(range(len(group_axes)))
 group_reduction_axes = group_reduction_axes[2:]
mean = K.mean(inputs, axis=group_reduction_axes, keepdims=True)
 variance = K.var(inputs, axis=group_reduction_axes, keepdims=True)
 inputs =(inputs - mean)/(K.sqrt(variance + self.epsilon))
inputs = K.reshape(inputs, group_shape)
 outputs = inputs
if self.scale:
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
 outputs = outputs * broadcast_gamma
if self.center:
broadcast_beta = K.reshape(self.beta, broadcast_shape)
 outputs = outputs + broadcast_beta
outputs = K.reshape(outputs, tensor_input_shape)
 return outputs
def get_config(self):
config = {
'groups': self.groups,
'axis': self.axis,
'epsilon': self.epsilon,
'center': self.center,
'scale': self.scale,
'beta_initializer': initializers.serialize(self.beta_initializer),
'gamma_initializer': initializers.serialize(self.gamma_initializer),
'beta_regularizer': regularizers.serialize(self.beta_regularizer),
'gamma_regularizer': regularizers.serialize(self.gamma_regularizer),
'beta_constraint': constraints.serialize(self.beta_constraint),
'gamma_constraint': constraints.serialize(self.gamma_constraint)
 }
base_config = super(GroupNormalization, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape<load_pretrained>
train=pd.get_dummies(data=train,columns=["Pclass"],prefix="Pclass")
 data=pd.get_dummies(data=data,columns=["Pclass"],prefix="Pclass" )
input_layer = Input(shape =(IMG_SIZE,IMG_SIZE,3))
base_model = EfficientNetB5(weights = None,
include_top = False,
input_tensor = input_layer)
 base_model.load_weights('.. /input/effnet/efficientnetb5notop.h5')
 x = GlobalAveragePooling2D()(base_model.output)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(0.40 )(x)
 x = Dense(512, activation='relu' )(x)
 x = Dropout(0.3 )(x)
 out = Dense(5, activation = 'softmax' )(x)
 model = Model(inputs = input_layer, outputs = out )<choose_model_class>
print("NaN-Vals in SibSp in train set present? {0}".format(train["SibSp"].isnull().any()))
print("NaN-Vals in SibSp in data set present? {0}".format(data["SibSp"].isnull().any()))
print("NaN-Vals in Parch in train present? {0}".format(train["Parch"].isnull().any()))
print("NaN-Vals in Parch in data set present? {0}".format(data["Parch"].isnull().any()))
optimizer = RAdam(lr=0.0005)
 es = EarlyStopping(monitor='cohens_kappa', mode='auto', verbose=1, patience=3,restore_best_weights=True)
 rlrop = ReduceLROnPlateau(monitor='cohens_kappa',
factor=0.2,
patience=5,
verbose=1,
mode='auto',
min_lr=1e-6)
 callback_list = [ rlrop ]
model.compile(optimizer = optimizer, loss = "categorical_crossentropy", metrics = ["accuracy",cohens_kappa] )<feature_engineering>
train["familySize"]=train["SibSp"]+train["Parch"]+1
data["familySize"]=data["SibSp"]+data["Parch"]+1
train.head(5 )
K.get_session().run(tf.local_variables_initializer() )<train_model>
train["lonely"]=train["familySize"].map(lambda x: 1 if x==1 else 0)
 data["lonely"]=data["familySize"].map(lambda x: 1 if x==1 else 0)
 train["smallFamily"]=train["familySize"].map(lambda x:1 if x==2 else 0)
 data["smallFamily"]=data["familySize"].map(lambda x:1 if x==2 else 0)
 train["biggerFamily"]=train["familySize"].map(lambda x:1 if x>2 & x<=4 else 0)
 data["biggerFamily"]=data["familySize"].map(lambda x:1 if x>2 & x<=4 else 0)
 train["biggerFamily"]=train["familySize"].map(lambda x:1 if x>4 else 0)
 data["biggerFamily"]=data["familySize"].map(lambda x:1 if x>4 else 0)
 train=train.drop(labels=["SibSp","Parch","familySize"],axis=1)
 data=data.drop(labels=["SibSp","Parch","familySize"],axis=1)
 train.head(5 )
history = model.fit_generator(generator = train_generator,
steps_per_epoch = len(train_generator),
epochs = 18,
validation_data = validation_generator,
validation_steps = len(validation_generator),
callbacks = callback_list )<predict_on_test>
data.drop(labels = ["PassengerId"], axis = 1, inplace = True)
 train_set=data[:len(train)]
test=data[len(train):]
train.head(5 )
predprobs = model.predict_generator(test_generator, steps=len(test_generator))<drop_column>
y=train["Survived"]
shutil.rmtree('train_data_cropped')
 shutil.rmtree('test_dir_processed' )<prepare_output>
kfold = StratifiedKFold(n_splits=10 )
predictions = []
for i in predprobs:
predictions.append(np.argmax(i))<prepare_output>
classifier=XGBClassifier()
parameters = {'nthread':[4],
'objective':['binary:logistic'],
'learning_rate': [0.05],
'max_depth': [6],
'min_child_weight': [11],
'silent': [1],
'subsample': [0.8],
'colsample_bytree': [0.7],
'n_estimators': [5],
'missing':[-999],
'seed': [1337]}
clf=GridSearchCV(classifier,parameters, n_jobs=5,cv=kfold,scoring='roc_auc',verbose=2, refit=True)
 clf.fit(train_set,y)
 best_parameters = clf.best_score_
test=clf.predict(test)
test_df_orig["diagnosis"] = predictions<feature_engineering>
y_pred= test
test_ids = test_df_orig["id_code"].values.tolist()
for i in range(len(test_ids)) :
imgname = test_ids[i]
newname = imgname.split('.')[0]
test_ids[i] = newname
test_df_orig["id_code"] = test_ids<save_to_csv>
y_pred=pd.DataFrame(data=y_pred )
<load_from_csv><EOS> 
submission = pd.read_csv(".. /input/gender_submission.csv")
 submission['Survived'] = y_pred
submission.to_csv('.. /working/submit.csv',index=False )