Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_train_metric>
warnings.filterwarnings('ignore' )
class MRCMetrics(object):
def __init__(self, criterion):
self.loss = 0
self.size = 0
self.F1 = 0
self.EM = 0
self.ja = 0
self.criterion = criterion
def update(self, st_prob, ed_prob, st, ed):
st_pred = st_prob.argmax(-1)
 ed_pred = ed_prob.argmax(-1)
 size = st_pred.shape[0]
zeros = torch.zeros_like(st_pred, dtype=torch.float)
 F1 = torch.max(zeros , zeros+torch.min(ed_pred, ed)- torch.max(st_pred, st)+1)*2 \
/torch.max(zeros+2, zeros+ed_pred+ed-st_pred-st+2)
 ja = torch.max(zeros , zeros+torch.min(ed_pred, ed)- torch.max(st_pred, st)+1)\
/torch.max(zeros , zeros+torch.max(ed_pred, ed)- torch.min(st_pred, st)+1)
 loss = self.criterion(st_prob, st)+ criterion(ed_prob, ed)*size
EM =(( st_pred == st)*(ed_pred == ed)).to(torch.float)
 self.F1 += F1.sum().item()
self.EM += EM.sum().item()
self.ja += ja.sum().item()
self.loss += loss.sum().item()
self.size += size
return loss/size
def __getitem__(self, key):
return getattr(self, key)/self.size
def __str__(self):
return("loss: %.4f, EM: %.4f, F1: %.4f, ja: %.4f" %(self["loss"], self["EM"], self["F1"], self["ja"]))<choose_model_class>
train_df = pd.read_csv('.. /input/titanic/train.csv')
 test_df = pd.read_csv('.. /input/titanic/test.csv')
 df = [train_df, test_df]
total_num = sum(p.numel() for p in model.parameters())
 print('Total:', total_num)
 criterion = torch.nn.CrossEntropyLoss().to(device)
 if model_name == "bert":
lr = 1e-5
elif model_name == "roberta":
lr = 1e-5
elif model_name == "xlnet":
lr = 6e-6
clip_grad_value = 5
optimizer = torch.optim.Adam(model.parameters() , lr=lr, weight_decay=3e-3)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.2, patience=3 )<train_on_grid>
train_df.isnull().sum()
def model_check() :
batch = next(iter(valid_iter))
print(batch.text[0])
 st_prob, ed_prob = model(batch.text)
 print(st_prob.shape)

metrics = MRCMetrics(criterion)
 loss = metrics.update(st_prob, ed_prob, batch.answer_start, batch.answer_end)
 print(loss)
 optimizer.zero_grad()
print(metrics)
<train_model>
test_df.isnull().sum()
def run_epoch(data_iter, train):
metrics = MRCMetrics(criterion)
 num_iter = len(data_iter)
 if train:
model.train()
else:
model.eval()
for i, batch in enumerate(data_iter):
text = batch.text.to(device)
 answer_start = batch.answer_start.to(device)
 answer_end = batch.answer_end.to(device)
 if train:
st_prob, ed_prob = model(text)
 loss = metrics.update(st_prob, ed_prob, answer_start, answer_end)
 loss.backward()
optimizer.step()
optimizer.zero_grad()
if(( i+1)%400==0):
scheduler.step(metrics["ja"])
 print(i, ": train", metrics, "lr:", optimizer.param_groups[0]["lr"])

else:
with torch.no_grad() :
st_prob, ed_prob = model(text)
 loss = metrics.update(st_prob, ed_prob, answer_start, answer_end)
 return metrics<train_model>
train_df.Embarked.value_counts()
for epoch in range(n_epochs):
start_time = time()
train_metrics = run_epoch(train_iter, True)
 valid_metrics = run_epoch(valid_iter, False)
 secs = int(time() - start_time)
 print("epoch", epoch,"finished in "+str(secs)+"s")
 print("train:", train_metrics)
 print("valid:", valid_metrics)
<categorify>
train_df['Embarked'] = train_df['Embarked'].fillna(train_df['Embarked'].mode() [0])
 df = [train_df, test_df]
def decode(st_prob, ed_prob, word_to_char):
global cnt
if st_prob.argmax().item() > ed_prob.argmax().item() :
cnt = cnt + 1
print("empty out ", cnt)
 n = st_prob.shape[1]-1
mx = -12345
start, end = -1, -1
for i in range(n):
for j in range(i, n):
if word_to_char[i] < 0: continue
if word_to_char[j] < 0: continue
logit = st_prob[0,i].item() + ed_prob[0,j].item()
if logit > mx:
mx = logit
start, end = i, j
return start, end 
<find_best_params>
train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean()
model.eval()
out = []
cnt = 0
for batch, context_raw, word_to_char in zip(test_iter, context_raw_test, word_to_char_list):
text = batch.text.to(device)
 with torch.no_grad() :
st_prob, ed_prob = model(text)
 assert(st_prob.shape[0]==1)
 start = st_prob.argmax().item()
end = ed_prob.argmax().item()
start, end = decode(st_prob, ed_prob, word_to_char)
 answer= context_raw[word_to_char[start]: word_to_char[end+1]]
out.append(answer)

sample = pd.read_csv("/kaggle/input/tweet-sentiment-extraction/sample_submission.csv")
 sample.loc[:, 'selected_text'] = out
sample.to_csv("submission.csv", index=False )<load_from_csv>
for dataset in df:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()
train_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/train.csv")
 question_p="positive sentence? "
question_n="negative sentence?"
question_neu="neutral sentence? "<feature_engineering>
train_df.Parch.value_counts()
def make_question(x):
if x=="positive":
return question_p
elif x=="negative":
return question_n
else:
return question_neu
train_df["sentiment"]=train_df["sentiment"].apply(make_question)
 train_df["sentiment"]<correct_missing_values>
train_df.SibSp.value_counts()
train_df=train_df.dropna()<feature_engineering>
train_df[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
def answer_index(x):
return x["text"].find(str(x["selected_text"]))
train_df["answer_index"]=train_df.apply(answer_index,axis=1 )<feature_engineering>
train_df[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
train_data=[]
def make_train_data(df):
for ind in df.index:
main_d=dict()
context=df.loc[ind,"text"].lower()
main_d["context"]=context
qas=[]
inside_qas={}
inside_qas["id"]=df.loc[ind,"textID"]
inside_qas["question"]=df.loc[ind,"sentiment"]
inside_answer=[{"text":df.loc[ind,"selected_text"].lower() ,"answer_start": int(df.loc[ind,"answer_index"])}]
inside_qas["answers"]=inside_answer
inside_qas["is_impossible"]=False
qas.append(inside_qas)
 main_d["qas"]=qas
train_data.append(main_d)
 return train_data 
<create_dataframe>
for dataset in df:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
train_data=make_train_data(train_df )<load_from_disk>
for dataset in df:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
with open('/kaggle/working/train.json', 'w')as outfile:
json.dump(train_data, outfile)
<load_pretrained>
train_df.Sex.value_counts()
!pip install '/kaggle/input/simple-transformers-pypi/seqeval-0.0.12-py3-none-any.whl' -q
!pip install '/kaggle/input/simple-transformers-pypi/simpletransformers-0.22.1-py3-none-any.whl' -q
MODEL_PATH = '/kaggle/input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'
model=QuestionAnsweringModel("distilbert",MODEL_PATH,
args={'reprocess_input_data': True,
'overwrite_output_dir': True,
'learning_rate': 5e-5,
'num_train_epochs': 2,
'max_seq_length': 128,
'doc_stride': 64,
'fp16': False,
},
use_cuda=True)
<train_model>
train_df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()
model.train_model('/kaggle/working/train.json' )<feature_engineering>
for dataset in df:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int )
test_data=[]
def make_test_data(df):
for ind in df.index:
main_d=dict()
context=df.loc[ind,"text"].lower()
main_d["context"]=context
qas=[]
inside_qas={}
inside_qas["id"]=df.loc[ind,"textID"]
inside_qas["question"]=df.loc[ind,"sentiment"]
inside_qas["is_impossible"]=False
qas.append(inside_qas)
 main_d["qas"]=qas
test_data.append(main_d)
 return test_data 
<load_from_csv>
for dataset in df:
dataset['Ticket_num'] = dataset['Ticket'].apply(lambda x: func(x))
dataset['Ticket_num'] = dataset['Ticket_num'].replace('LINE', 0 )
test_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/test.csv")
 test_df["sentiment"]=test_df["sentiment"].apply(make_question)
 test_data=make_test_data(test_df)
 test_data_json=json.dumps(test_data)
 with open('/kaggle/working/test.json', 'w')as outfile:
json.dump(test_data, outfile )<predict_on_test>
train_df.Ticket_num.unique().shape
predictions=model.predict(test_data )<save_to_csv>
for dataset in df:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
predictions_df = pd.DataFrame.from_dict(predictions)
 sub_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/sample_submission.csv")
 sub_df['selected_text'] = predictions_df['answer']
sub_df.to_csv('submission.csv', index=False )<import_modules>
train_df[['Title', 'Age']].groupby(['Title'], as_index=False ).mean()
import numpy as np
import pandas as pd
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv2D,MaxPool2D,Flatten,BatchNormalization
from keras.optimizers import Adam,Nadam
from sklearn.model_selection import train_test_split
from keras import regularizers
from keras.preprocessing.image import ImageDataGenerator
from sklearn.utils import shuffle
import matplotlib.pyplot as plt<load_from_csv>
for dataset in df:
dataset['Age_fill'] = 0
dataset.loc[dataset['Title'] == 'Capt', 'Age_fill'] = 70
dataset.loc[dataset['Title'] == 'Col', 'Age_fill'] = 58
dataset.loc[dataset['Title'] == 'Countess', 'Age_fill'] = 33
dataset.loc[dataset['Title'] == 'Don', 'Age_fill'] = 40
dataset.loc[dataset['Title'] == 'Dr', 'Age_fill'] = 42
dataset.loc[dataset['Title'] == 'jonkheer', 'Age_fill'] = 38
dataset.loc[dataset['Title'] == 'Lady', 'Age_fill'] = 48
dataset.loc[dataset['Title'] == 'Major', 'Age_fill'] = 48.5
dataset.loc[dataset['Title'] == 'Master', 'Age_fill'] = 4.6
dataset.loc[dataset['Title'] == 'Miss', 'Age_fill'] = 21.8
dataset.loc[dataset['Title'] == 'Mlle', 'Age_fill'] = 24
dataset.loc[dataset['Title'] == 'Mme', 'Age_fill'] = 24
dataset.loc[dataset['Title'] == 'Mr', 'Age_fill'] = 32.4
dataset.loc[dataset['Title'] == 'Mrs', 'Age_fill'] = 35.9
dataset.loc[dataset['Title'] == 'Ms', 'Age_fill'] = 28
dataset.loc[dataset['Title'] == 'Rev', 'Age_fill'] = 43.2
dataset.loc[dataset['Title'] == 'Sir', 'Age_fill'] = 49
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 submission = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 display(train.shape,test.shape )<define_variables>
for dataset in df:
dataset['Age'] = dataset['Age'].fillna(dataset['Age_fill'] )
Id = test['id']<split>
train_df.isnull().sum()
x_train, x_test, y_train, y_test = train_test_split(train.iloc[:, 1:], train.iloc[:, 0], test_size=0.05)
 display(x_train.shape,y_train.shape,x_test.shape,y_test.shape )<train_model>
test_df.isnull().sum()
x_train, y_train = shuffle(x_train, y_train )<categorify>
for dataset in df:
dataset.drop(['Age_fill', 'Cabin', 'Ticket_num', 'Ticket', 'FamilySize', 'SibSp', 'Parch', 'Name'], axis=1, inplace=True )
x_train = x_train.values.reshape(-1,28,28,1)/255.0
x_test = x_test.values.reshape(-1,28,28,1)/255.0
y_train = np_utils.to_categorical(y_train,num_classes=10)
 y_test = np_utils.to_categorical(y_test,num_classes=10 )<train_model>
print('Before conversion')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
imagegen = ImageDataGenerator(
 rotation_range = 10,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 20,
zoom_range = 0.2 
)
 imagegen.fit(x_train )<choose_model_class>
for dataset in df:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 print('After conversion')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
model = Sequential()
model.add(Conv2D(input_shape=(28, 28, 1),filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.5))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dense(10, activation = "softmax"))
model.compile(optimizer=Nadam() ,loss='categorical_crossentropy',metrics=['accuracy'])
 fit = model.fit_generator(imagegen.flow(x_train,y_train,batch_size=128),epochs=80,validation_data=(x_test,y_test),verbose = 1,steps_per_epoch=100)
 loss,accuracy = model.evaluate(x_test,y_test)
 print('test loss',loss)
 print('test accuracy',accuracy )<predict_on_test>
mapping = {'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Master': 4, 'Rare': 5}
for dataset in df:
dataset['Title'] = dataset['Title'].map(mapping)
 train_df.head()
test = test.drop(['id'],axis=1)
 test = test.values.reshape(test.shape[0],28,28,1)/255.0
FINAL_PREDS = model.predict_classes(test )<save_to_csv>
for dataset in df:
dataset['Age'] = dataset['Age'].astype(int )
submission = pd.DataFrame({ 'id': Id,
'label': FINAL_PREDS })
 submission.to_csv(path_or_buf ="submission.csv", index=False)
 submission.head()<load_from_csv>
train_df['AgeBand'] = pd.cut(train_df['Age'], 5)
 train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
%%time
train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )<groupby>
for dataset in df:
dataset.loc[dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[dataset['Age'] > 64, 'Age'] = 4
train_df.head()
train_data.groupby(by='label' ).size()<define_variables>
test_df['Fare'] = test_df['Fare'].fillna(test_df['Fare'].mode() [0] )
IMG_SIZE = 28<data_type_conversions>
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)
 train_df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False ).mean().sort_values(by='FareBand', ascending=True )
img_train = train_data.drop(["label"], axis=1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 img_label = to_categorical(train_data["label"])
 img_test = test_data.drop(["id"], axis=1 ).values.reshape(-1, IMG_SIZE, IMG_SIZE, 1 ).astype('float32')
 print("img_train.shape = ", img_train.shape)
 print("img_label.shape = ", img_label.shape)
 print("img_test.shape = ", img_test.shape )<split>
for dataset in df:
dataset.loc[dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 df = [train_df, test_df]
train_df.head()
x_train, x_test, y_train, y_test = train_test_split(img_train, img_label, test_size=0.15)
 print("x_train.shape = ", x_train.shape)
 print("y_train.shape = ", y_train.shape)
 print("x_test.shape = ", x_test.shape)
 print("y_test.shape = ", y_test.shape )<import_modules>
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean()
import keras
from keras.datasets import mnist
from keras.layers import Input, Dense, Dropout, Flatten, add
from keras.layers import Conv2D, Activation, MaxPooling2D, AveragePooling2D, BatchNormalization
from keras import backend as K
from keras.callbacks import ModelCheckpoint
import tensorflow as tf
from keras.models import Model
from keras.utils import plot_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input, Dense, PReLU, Dropout
from keras.models import Model
from keras.callbacks import LearningRateScheduler, ModelCheckpoint, TensorBoard, EarlyStopping, ReduceLROnPlateau
from keras.optimizers import SGD, Adam<choose_model_class>
for dataset in df:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_df.loc[:, ['Age*Class', 'Age', 'Pclass']].head()
def build_model(input_shape=(28, 28, 1), classes = 10):
input_layer = Input(shape=input_shape)
 x = Conv2D(16,(3,3), strides=1, padding="same", name="conv1" )(input_layer)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1" )(x)
 x = Activation('relu',name='relu1' )(x)
 x = Dropout(0.1 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv2" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2" )(x)
 x = Activation('relu',name='relu2' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2" )(x)
 x = Conv2D(64,(5,5), strides=1, padding ="same", name="conv3" )(x)
 x = BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3" )(x)
 x = Activation('relu', name="relu3" )(x)
 x = MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3" )(x)
 x = Conv2D(128,(5,5), strides=1, padding="same", name="conv4" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4" )(x)
 x = Activation('relu', name="relu4" )(x)
 x = Dropout(0.17 )(x)
 x = Conv2D(64,(3,3), strides=1, padding="same", name="conv5" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5" )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.2 )(x)
 x = Conv2D(32,(3,3), strides=1, padding="same", name="conv6" )(x)
 x = BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
 x = Activation('relu', name="relu6" )(x)
 x = Dropout(0.05 )(x)
 x = Flatten()(x)
 x = Dense(50, name="Dense1" )(x)
 x = Activation('relu', name='relu7' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(25, name="Dense2" )(x)
 x = Activation('relu', name='relu8' )(x)
 x = Dropout(0.03 )(x)
 x = Dense(classes, name="Dense3" )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs=input_layer, outputs=x)
 return model<choose_model_class>
train_df.drop(['PassengerId', 'AgeBand', 'FareBand'], axis=1, inplace=True )
model = build_model(input_shape=(28, 28, 1), classes = 10 )<train_on_grid>
X_train = train_df.drop(['Survived'], axis=1)
 y_train = train_df['Survived']
X_test = test_df.drop(['PassengerId'], axis=1 )
train_datagen = ImageDataGenerator(
 rotation_range=9,
zoom_range=0.25,
width_shift_range=0.25,
height_shift_range=0.25,
rescale=1./255 
)
 train_datagen.fit(x_train)
 test_datagen = ImageDataGenerator(rescale=1./255)
 adam = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 sgd = SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.5, min_lr=0.00001)
 checkpoint = ModelCheckpoint("bestmodel.model", monitor='val_acc', verbose=1, save_best_only=True)
 earlyStopping = EarlyStopping(monitor='val_acc', patience=15, verbose=1, mode='min' )<choose_model_class>
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import cross_val_score
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'] )<define_variables>
classifier = LogisticRegression()
classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test )
epochs = 80
batch_size = 128<train_model>
acc_Logistic = cross_val_score(classifier, X_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_Logistic
history = model.fit_generator(
 train_datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=test_datagen.flow(x_test, y_test),
validation_steps=x_test.shape[0] // batch_size,
callbacks=[checkpoint, learning_rate_reduction] )<load_pretrained>
classifier = KNeighborsClassifier(n_neighbors=5)
 classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test )
model.load_weights('bestmodel.model' )<predict_on_test>
acc_KNN = cross_val_score(classifier, X_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_KNN
results=model.predict(img_test/255.0 )<split>
classifier = SVC()
classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test )
x2_train, x2_test, y2_train, y2_test = train_test_split(img_test, results, test_size=0.15 )<concatenate>
acc_SVC = cross_val_score(classifier, X_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_SVC
x_train_final = np.concatenate(( img_train,x2_train), axis=0)
 y_train_final = np.concatenate(( img_label,y2_train), axis=0 )<split>
classifier = DecisionTreeClassifier()
classifier.fit(X_train, y_train)
 y_pred = classifier.predict(X_test )
x_train, x_test, y_train, y_test = train_test_split(x_train_final, y_train_final, test_size=0.15)
 print("x_train.shape = ", x_train.shape)
 print("y_train.shape = ", y_train.shape)
 print("x_test.shape = ", x_test.shape)
 print("y_test.shape = ", y_test.shape )<drop_column>
acc_Tree = cross_val_score(classifier, X_train, y_train, cv=10, scoring='accuracy' ).mean()
acc_Tree
keras.backend.clear_session()<choose_model_class>
datasets = pd.DataFrame({
'PassengerId': test_df['PassengerId'],
'Survived': y_pred
})
 datasets.to_csv('gender_submission3.csv', index=False )