Kaggle competition: Titanic - Machine Learning from Disaster
@numba.jit('f8(i8, i8, i8)', nopython=True, parallel=False)
 def cities_distance(offset, id_from, id_to):
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 if offset % 10 == 9 and is_not_prime[id_from]:
return 1.1 * distance
return distance
@numba.jit('f8(i4, i8[:])', nopython=True, parallel=False)
 def score_chunk(offset, chunk):
pure_distance, penalty = 0.0, 0.0
penalty_modulo = 9 - offset % 10
for path_index in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[path_index], chunk[path_index+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if path_index % 10 == penalty_modulo and is_not_prime[id_from]:
penalty += distance
return pure_distance + 0.1 * penalty
@numba.jit('f8(i8[:])', nopython=True, parallel=False)
 def score_path(path):
return score_chunk(0, path)
 @numba.jit
def chunk_scores(chunk):
scores = np.zeros(10)
 pure_distance = 0
for i in numba.prange(chunk.shape[0] - 1):
id_from, id_to = chunk[i], chunk[i+1]
xy_from, xy_to = XY[id_from], XY[id_to]
dx, dy = xy_from[0] - xy_to[0], xy_from[1] - xy_to[1]
distance = sqrt(dx * dx + dy * dy)
 pure_distance += distance
if is_not_prime[id_from]:
scores[9-i%10] += distance
scores *= 0.1
scores += pure_distance
return scores
def score_compound_chunk(offset, head, chunks, tail, scores, indexes_permutation=None):
if indexes_permutation is None:
indexes_permutation = range(len(chunks))
score = 0.0
last_city_id = head
for index in indexes_permutation:
chunk, chunk_scores = chunks[index], scores[index]
score += cities_distance(offset % 10, last_city_id, chunk[0])
 score += chunk_scores[(offset + 1)% 10]
last_city_id = chunk[-1]
offset += len(chunk)
 return score + cities_distance(offset % 10, last_city_id, tail)
<choose_model_class>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
kdt = KDTree(XY )<load_from_csv>
train_data.isna().sum()
path = np.array(pd.read_csv('.. /input/close-ends-chunks-optimization-aka-2-opt/1515567.693648386.csv' ).Path )<randomize_order>
train_data = train_data.drop(labels=["Cabin"], axis="columns")
 test_data = test_data.drop(labels=["Cabin"], axis="columns" )
def not_trivial_permutations(iterable):
perms = permutations(iterable)
 next(perms)
 yield from perms
@lru_cache(maxsize=None)
 def not_trivial_indexes_permutations(length):
return np.array([list(p)for p in not_trivial_permutations(range(length)) ])
 path_index = np.argsort(path[:-1])
 print(f'Total score is {score_path(path):.2f}.')
 for _ in range(3):
for ids in tqdm(triplets):
i, j, k = sorted(path_index[ids])
 head, tail = path[i-1], path[k+1]
chunks = [path[i:i+1], path[i+1:j], path[j:j+1], path[j+1:k], path[k:k+1]]
chunks = [chunk for chunk in chunks if len(chunk)]
scores = [chunk_scores(chunk)for chunk in chunks]
default_score = score_compound_chunk(i-1, head, chunks, tail, scores)
 best_score = default_score
for indexes_permutation in not_trivial_indexes_permutations(len(chunks)) :
score = score_compound_chunk(i-1, head, chunks, tail, scores, indexes_permutation)
 if score < best_score:
permutation = [chunks[i] for i in indexes_permutation]
best_chunk = np.concatenate([[head], np.concatenate(permutation), [tail]])
 best_score = score
if best_score < default_score:
path[i-1:k+2] = best_chunk
path_index = np.argsort(path[:-1])
 print(f'New total score is {score_path(path):.2f}.Permutating path at indexes {i}, {j}, {k}.')
 triplets = triplets[:10**6]<compute_test_metric>
train_data["Age"] = train_data["Age"].fillna(28)
 test_data["Age"] = test_data["Age"].fillna(28 )
print(f'Final score is {score_path(path):.2f}.' )<save_to_csv>
test_data.isna().sum()
def make_submission(name, path):
pd.DataFrame({'Path': path} ).to_csv(f'{name}.csv', index=False )<compute_test_metric>
test_data["Fare"] = test_data["Fare"].fillna(14.454200 )
make_submission(score_path(path), path )<import_modules>
label_encoder = LabelEncoder()
train_data["Sex"] = label_encoder.fit_transform(train_data["Sex"])
 test_data["Sex"] = label_encoder.fit_transform(test_data["Sex"] )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras.models import Sequential, load_model
from keras.layers import Conv2D, Dense, Flatten, MaxPool2D, Dropout
from keras.optimizers import Adam
from keras.callbacks import LearningRateScheduler, EarlyStopping, ModelCheckpoint
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import RandomizedSearchCV<load_from_csv>
train_data["Embarked"] = train_data["Embarked"].fillna("X")
 test_data["Embarked"] = test_data["Embarked"].fillna("X" )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 print(train.head())
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 print(test.head() )<prepare_x_and_y>
train_data["Embarked"] = label_encoder.fit_transform(train_data["Embarked"])
 test_data["Embarked"] = label_encoder.fit_transform(test_data["Embarked"] )
X_train = train.drop(labels = ['label'], axis = 1)
 y_train = train.label
del train<normalization>
Ticket1 = []
for i in list(train_data.Ticket):
if not i.isdigit() :
Ticket1.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket1.append("X")
 train_data["Ticket"] = Ticket1
Ticket2 = []
for j in list(test_data.Ticket):
if not j.isdigit() :
Ticket2.append(j.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket2.append("X")
 test_data["Ticket"] = Ticket2
X_train = X_train / 255.0
test = test / 255.0<categorify>
unique1 = ["Ticket_" + s for s in unique1]
unique2 = ["Ticket_" + s for s in unique2]
y_train = to_categorical(y_train, num_classes = 10 )<split>
train_data = pd.get_dummies(train_data, columns = ["Ticket"])
 test_data = pd.get_dummies(test_data, columns = ["Ticket"] )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train,
test_size = 0.1,
random_state = 10,
stratify = y_train )<choose_model_class>
train_data = train_data.drop(labels = unique1 , axis = "columns")
 test_data = test_data.drop(labels = unique2 , axis = "columns" )
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose = 0)
 nets = 3
model = [0] * nets
for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(filters = 32, kernel_size = 5, padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 model[i].add(MaxPool2D())
 if i > 0:
model[i].add(Conv2D(filters = 48, kernel_size = 5, padding = 'same',
activation = 'relu'))
model[i].add(MaxPool2D())
 if i > 1:
model[i].add(Conv2D(filters = 64, kernel_size = 5, padding = 'same',
activation = 'relu'))
model[i].add(MaxPool2D(padding = 'same'))
model[i].add(Flatten())
 model[i].add(Dense(256, activation = 'relu'))
model[i].add(Dense(10, activation = 'softmax'))
model[i].compile(optimizer = 'adam', loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 history = [0] * nets
names = ['CNN-1', 'CNN-2', 'CNN-3']
epochs = 20
for i in range(nets):
history[i] = model[i].fit(X_train, y_train, batch_size = 100,
epochs = epochs,
validation_data =(X_val, y_val),
callbacks = [annealer], verbose = 0)
 print('{0}: Epochs = {1: d}, Train accuracy = {2:.5f}, Validation accuracy = {3:.5f}'.format(names[i],
epochs, max(history[i].history['accuracy']),
max(history[i].history['val_accuracy'])))
<choose_model_class>
train_title = [i.split(",")[1].split(".")[0].strip() for i in train_data["Name"]]
train_data["Title"] = pd.Series(train_title)
 test_title = [i.split(",")[1].split(".")[0].strip() for i in test_data["Name"]]
test_data["Title"] = pd.Series(test_title )
nets = 7
model = [0] * nets
for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(i*8+8, kernel_size = 5,
padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 model[i].add(MaxPool2D())
 model[i].add(Conv2D(i*16+16, kernel_size = 5,
padding = 'same',
activation = 'relu'))
model[i].add(MaxPool2D())
 model[i].add(Flatten())
 model[i].add(Dense(256, activation = 'relu'))
model[i].add(Dense(10, activation = 'softmax'))
model[i].compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 history = [0] * nets
names = []
for i in range(nets):
names.append(i*8+8)
 for i in range(nets):
history[i] = model[i].fit(X_train, y_train,
batch_size = 100,
epochs = epochs,
validation_data =(X_val, y_val),
callbacks = [annealer], verbose = 0)
 print('CNN {0: d} maps: Epochs = {1: d}, Train accuracy = {2:.5f}, Validation accuracy = {3:.5f}'.format(names[i],
epochs, max(history[i].history['accuracy']), max(history[i].history['val_accuracy'])) )<choose_model_class>
train_data = train_data.drop(labels = "Name", axis = "columns")
 test_data = test_data.drop(labels = "Name", axis = "columns" )
nets = 8
model = [0] * nets
for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(48, kernel_size = 5,
padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 model[i].add(MaxPool2D())
 model[i].add(Conv2D(96, kernel_size = 5,
activation = 'relu'))
model[i].add(MaxPool2D())
 model[i].add(Flatten())
 if i>0:
model[i].add(Dense(2**(i+4), activation = 'relu'))
model[i].add(Dense(10, activation = 'softmax'))
model[i].compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 history = [0] * nets
names = [0]
for i in range(nets - 1):
names.append(2**(i+5))
for i in range(nets):
history[i] = model[i].fit(X_train, y_train,
batch_size = 100,
epochs = epochs,
validation_data =(X_val, y_val),
callbacks = [annealer], verbose = 0)
 print('CNN {0: d}N: Epochs {1: d}, Training accuracy {2:.5f}, Validation accuracy {3:.5f}'.format(names[i],
epochs, max(history[i].history['accuracy']), max(history[i].history['val_accuracy'])) )<choose_model_class>
train_data["Title"] = train_data["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 train_data["Title"] = train_data["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 train_data["Title"] = train_data["Title"].astype(int)
 test_data["Title"] = test_data["Title"].replace(['Lady', 'the Countess','Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 test_data["Title"] = test_data["Title"].map({"Master":0, "Miss":1, "Ms" : 1 , "Mme":1, "Mlle":1, "Mrs":1, "Mr":2, "Rare":3})
 test_data["Title"] = test_data["Title"].astype(int )
nets = 8
model = [0] * nets
names = []
for i, n in enumerate(range(8)) :
names.append(f'{n*10}%')
 for i in range(nets):
model[i] = Sequential()
model[i].add(Conv2D(48, kernel_size = 5,
padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 model[i].add(MaxPool2D())
 model[i].add(Dropout(i*0.1))
model[i].add(Conv2D(96, kernel_size = 5,
activation = 'relu'))
model[i].add(MaxPool2D())
 model[i].add(Dropout(i*0.1))
model[i].add(Flatten())
 model[i].add(Dense(256, activation = 'relu'))
model[i].add(Dropout(i*0.1))
model[i].add(Dense(10, activation = 'sigmoid'))
model[i].compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 history = [0] * nets
for i in range(nets):
history[i] = model[i].fit(X_train, y_train,
batch_size = 100,
epochs = epochs,
validation_data =(X_val, y_val),
callbacks = [annealer],
verbose = 0)
 print('CNN Dropouts = {0}: Epochs = {1: d}, Training accuracy = {2:.5f}, Validation accuracy = {3:.5f}'.format(names[i],
epochs, max(history[i].history['accuracy']), max(history[i].history['val_accuracy'])) )<choose_model_class>
pred = train_data['Survived']
def create_model(optimizer = 'adam', activation = 'relu'):
model = Sequential()
model.add(Conv2D(48, kernel_size = 5,
padding = 'same',
activation = activation,
input_shape =(28, 28, 1)))
 model.add(MaxPool2D())
 model.add(Dropout(0.4))
model.add(Conv2D(96, kernel_size = 5,
activation = activation))
model.add(MaxPool2D())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(256, activation = activation))
model.add(Dropout(0.4))
model.add(Dense(10, activation = 'sigmoid'))
model.compile(optimizer = optimizer,
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
 return model
model = KerasClassifier(build_fn = create_model)
 params = dict(optimizer = ['sgd', 'adam'], activation = ['relu', 'tanh'],
batch_size = [50, 100, 150, 200], epochs = [10, 20, 30, 50])
 random_search = RandomizedSearchCV(model, param_distributions = params, cv = 3,
n_iter = 10)
 random_search_results = random_search.fit(X_train, y_train,
validation_data =(X_val, y_val))<train_model>
import statsmodels.formula.api as smf
import statsmodels.api as sm
print('Best: {0: 5f} using {1}'.format(random_search_results.best_score_,
random_search_results.best_params_))<choose_model_class>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A4 + Ticket_A5 +Ticket_C + Ticket_CA+Ticket_FC + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SC + Ticket_SCA4 + Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOPP+Ticket_SOTONO2+Ticket_SOTONOQ + Ticket_STONO + Ticket_STONO2 + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
beta1 = reg_log.params[0]
beta2 = reg_log.params[1]
print(reg_log.summary() )
early_stopping = EarlyStopping(monitor = 'val_loss', patience = 5,
verbose = 0,
restore_best_weights = True)
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x, verbose = 0)
 model_checkpoint = ModelCheckpoint('Digit_Recognizer.hdf5', monitor='val_accuracy',
verbose=1, save_best_only=True,
mode='max')
 cnn = create_model(optimizer = 'adam', activation = 'relu')
 results = cnn.fit(X_train, y_train, validation_data =(X_val, y_val),
batch_size = 50, epochs = 30,
callbacks = [early_stopping, model_checkpoint, annealer] )<save_to_csv>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A4 + Ticket_A5 +Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOPP+Ticket_SOTONO2+Ticket_SOTONOQ + Ticket_STONO + Ticket_STONO2 + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
beta1 = reg_log.params[0]
beta2 = reg_log.params[1]
print(reg_log.summary() )
model = load_model('Digit_Recognizer.hdf5')
 model.evaluate(X_val, y_val)
 pred = model.predict(test)
 pred = np.argmax(pred, axis=1)
 pred = pd.Series(pred,name="Label")
 result = pd.concat([pd.Series(range(1,28001),name = "ImageId"),pred],axis = 1)
 result.to_csv("./digit_recognizer.csv",index=False )<load_pretrained>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A4 + Ticket_A5 +Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOPP+Ticket_SOTONOQ + Ticket_STONO + Ticket_STONO2 + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
%run.. /input/python-recipes/dhtml.py
%run.. /input/python-recipes/load_kaggle_digits.py
%run.. /input/python-recipes/classify_kaggle_digits.py
dhtml('Data Processing' )<train_model>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A5 +Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOPP+Ticket_SOTONOQ + Ticket_STONO + Ticket_STONO2 + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
k=.75; cmap='Pastel1_r'
x_train,y_train,x_valid,y_valid,x_test,y_test,\
test_images,num_classes=\
load_kaggle_digits(k,cmap )<compute_test_metric>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A5 +Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_STONO2 + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
num_test=100
model_evaluation(cnn_model,x_test,y_test,
weights,color,num_test )<save_to_csv>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_A5 +Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
cnn_model.load_weights(weights)
 predict_test_labels=\
cnn_model.predict_classes(test_images)
 submission=pd.DataFrame(
 {'ImageId':range(1,len(predict_test_labels)+1),
'Label':predict_test_labels})
 submission.to_csv('kaggle_digits.csv',index=False)
 fig=pl.figure(figsize=(10,6))
for i in range(15):
ax=fig.add_subplot(3,5,i+1,xticks=[],yticks=[])
 ax.imshow(test_images[i].reshape(28,28),
cmap=pl.get_cmap('Pastel1'))
ax.set_title('%d'%submission['Label'][i],
fontsize=10)
 pl.show()<set_options>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCPARIS + Ticket_SCParis + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
random_seed = 2
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SCParis + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
X = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 Y = X["label"]
X.drop("label",axis = 1, inplace = True)
<count_missing_values>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP+ Ticket_SCAH + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
np.sum(X.isnull().any() )<count_missing_values>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP + Ticket_X +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
sum(test.isnull().any() )<choose_model_class>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP + Ticket_SOC+Ticket_SOTONOQ + Ticket_STONO + Ticket_WC + Ticket_WEP +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
def get_model(optim, loss):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
model.compile(optimizer= optim, loss=loss, metrics=['accuracy'])
 return model
def normalize(X, Y = None):
X = X / 255.0
X = X.values.reshape(-1,28,28,1)
 if Y is not None:
Y = Y.values.reshape(-1,1)
 return X,Y
def get_samples(X, Y, encoder, val_size = 0.9):
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size = val_size, random_state=42, stratify= Y)
 X_train = np.vstack([X_train,X_train,X_train])
 y_train = np.concatenate([y_train, y_train, y_train])
 idx = np.arange(0, X_train.shape[0])
 np.random.shuffle(idx)
 X_train = X_train[idx,:]
y_train = y_train[idx]
X_train = X_train.reshape(-1,28,28,1)
 X_val = X_val.reshape(-1,28,28,1)
 y_train = encoder.transform(y_train ).todense()
y_val = encoder.transform(y_val ).todense()
return X_train,X_val,y_train,y_val<categorify>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C + Ticket_CA + Ticket_FCC + Ticket_PC + Ticket_PP + Ticket_SOC+ Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
X_train,Y_train = normalize(X,Y)
 X_test,_ = normalize(test)
 encoder = OneHotEncoder()
encoder.fit(Y_train )<define_variables>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C +Ticket_PC + Ticket_PP + Ticket_SOC+ Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
data_generator = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
<choose_model_class>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Ticket_C +Ticket_PC + Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
EPOCHS = 50
BATCH_SIZE = 20
ENSEMBLES = 7
results = np.zeros(( test.shape[0],10))
histories = []
models = []
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.25, min_lr=0.00001, patience=2, verbose=1),
EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=3, verbose=1)
 ]
optimizer = Adam(learning_rate=0.001)
 loss_fn = CategoricalCrossentropy()<train_model>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Embarked + Ticket_C +Ticket_PC + Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
for i in range(ENSEMBLES):
X_train_tmp, X_val, y_train_tmp, y_val = get_samples(X_train, Y_train, encoder)
 data_generator.fit(X_train_tmp)
 models.append(get_model(optimizer, loss_fn))
history = models[i].fit_generator(data_generator.flow(X_train_tmp, y_train_tmp, batch_size=BATCH_SIZE),
epochs=EPOCHS,
callbacks=[callback_list],
validation_data=(X_val, y_val),
steps_per_epoch=X_train_tmp.shape[0] // BATCH_SIZE,
use_multiprocessing=True,
verbose = 2)
 histories.append(history )<predict_on_test>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp + Parch + Embarked + Ticket_C + Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
results = np.zeros(( X_test.shape[0],10))
for i in range(ENSEMBLES):
results = results + models[i].predict(X_test)
 results = np.argmax(results, axis = 1 )<save_to_csv>
reg_log = smf.glm('Survived ~ Pclass + Sex + Age + SibSp+ Embarked + Ticket_STONO + Ticket_WC +Title', data=train_data, family=sm.families.Binomial() ).fit()
print(reg_log.summary() )
submission = pd.DataFrame({"ImageID": range(1,len(results)+1), "Label": results})
 submission.to_csv('submission.csv', index=False )<define_variables>
pr = reg_log.predict(test_data )
data_dir='/kaggle/input/digit-recognizer/'<load_from_csv>
y_pred =(pr > 0.65 ).astype(int )
<count_values><EOS> 
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_pred})
 output.to_csv('./submission.csv', index=False )