Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
import numpy as np
import pandas as pd
import numpy as np
import pandas as pd
from sympy import simplify, cos, sin, Symbol, Function, tanh, pprint, init_printing, exp
from sympy.functions import Min,Max
input_layer = Input(( img_size_target, img_size_target, 1))
output_layer = build_model(input_layer, 16,0.5)
 model = Model(input_layer, output_layer)
 model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["acc"] )<train_model>
A = 0.058823499828577
B = 0.841127
C = 0.138462007045746
D = 0.31830988618379069
E = 2.810815
F = 0.63661977236758138
G = 5.428569793701172
H = 3.1415926535897931
I = 0.592158
J = 4.869778
K = 0.063467
L = -0.091481
M = 0.0821533
N = 0.720430016517639
O = 0.230145
P = 9.89287
Q = 785
R = 1.07241
S = 281
T = 734
U = 5.3
V = 67.0
W = 2.484848
X = 8.48635
Y = 63
Z = 12.6275
AA = 0.735354
AB = 727
AC = 2.5
AD = 2.6
AE = 0.3
AF = 3.0
AG = 0.226263
AH = 2.0
AI = 12.4148
AJ = 96
AK = 0.130303
AL = 176
AM = 3.2
BIG = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM]
early_stopping = EarlyStopping(monitor='val_acc', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint("./unet_best1.model",monitor='val_acc',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_acc', mode = 'max',factor=0.2, patience=5, min_lr=0.00001, verbose=1)
 epochs = 200
batch_size = 32
history = model.fit(x_train, y_train,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr],
verbose=1 )<load_pretrained>
def GeneticFunction(data,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM):
return(( np.minimum(((((A + data["Sex"])- np.cos(( data["Pclass"] / AH)))* AH)) ,(( B)))* AH)+
np.maximum(((data["SibSp"] - AC)) ,(-(np.minimum(( data["Sex"]),(np.sin(data["Parch"])))* data["Pclass"])))+ 
(AG *(( np.minimum(( data["Sex"]),(((data["Parch"] / AH)/ AH)))* data["Age"])- data["Cabin"])) +
np.minimum(((np.sin(( data["Parch"] *(( data["Fare"] - AA)* AH)))* AH)) ,(( data["SibSp"] / AH)))+
np.maximum(( np.minimum(( -np.cos(data["Embarked"])) ,(C))),(np.sin(((data["Cabin"] - data["Fare"])* AH)))) +
-np.minimum(((((data["Age"] * data["Parch"])* data["Embarked"])+ data["Parch"])) ,(np.sin(data["Pclass"])))+
np.minimum(( data["Sex"]),(( np.sin(-(data["Fare"] * np.cos(( data["Fare"] * W)))) / AH)))+
np.minimum(((O)) ,(np.sin(np.minimum(((( V / AH)* np.sin(data["Fare"]))),(D)))))+
np.sin(( np.sin(data["Cabin"])*(np.sin(( Z)) * np.maximum(( data["Age"]),(data["Fare"])))))+
np.sin(((np.minimum(( data["Fare"]),(( data["Cabin"] * data["Embarked"])))/ AH)* -data["Fare"])) +
np.minimum(((( AD * data["SibSp"])* np.sin(((AJ)* np.sin(data["Cabin"]))))),(data["Parch"])) +
np.sin(np.sin(( np.maximum(( np.minimum(( data["Age"]),(data["Cabin"]))),(( data["Fare"] * AK)))* data["Cabin"])))+
np.maximum(( np.sin(((AI)*(data["Age"] / AH)))) ,(np.sin(( -AF * data["Cabin"])))) + 
(np.minimum(( np.sin(((( np.sin(((data["Fare"] * AH)* AH)) * AH)* AH)* AH))),(data["SibSp"])) / AH)+ 
(( data["Sex"] - data["SibSp"])*(np.cos(((data["Embarked"] - AA)+ data["Age"])) / AH)) + 
(( np.sin(data["Cabin"])/ AH)-(np.cos(np.minimum(( data["Age"]),(data["Embarked"])))* np.sin(data["Embarked"])))+
np.minimum(( AE),(( data["Sex"] *(J *(N - np.sin(( data["Age"] * AH)))))))+ 
(np.minimum(( np.cos(data["Fare"])) ,(np.maximum(( np.sin(data["Age"])) ,(data["Parch"])))) * np.cos(( data["Fare"] / AH)))+
np.sin(( data["Parch"] * np.minimum(((data["Age"] - K)) ,(( np.cos(( data["Pclass"] * AH)) / AH)))))+ 
(data["Parch"] *(np.sin(((data["Fare"] *(I * data["Age"])) * AH)) / AH)) + 
(D * np.cos(np.maximum(((0.5 * data["Fare"])) ,(( np.sin(N)* data["Age"])))))+ 
(np.minimum(((data["SibSp"] / AH)) ,(np.sin(((data["Pclass"] - data["Fare"])* data["SibSp"])))) * data["SibSp"])+
np.tanh(( data["Sex"] * np.sin(( U * np.sin(( data["Cabin"] * np.cos(data["Fare"])))))))+ 
(np.minimum(( data["Parch"]),(data["Sex"])) * np.cos(np.maximum(((np.cos(data["Parch"])+ data["Age"])) ,(AM)))) + 
(np.minimum(( np.tanh(((data["Cabin"] / AH)+ data["Parch"]))),(( data["Sex"] + np.cos(data["Age"])))) / AH)+ 
(np.sin(( np.sin(data["Sex"])*(np.sin(( data["Age"] * data["Pclass"])) * data["Pclass"])))/ AH)+ 
(data["Sex"] *(np.cos(((data["Sex"] + data["Fare"])*(( X)*(Y)))) / AH)) +
np.minimum(( data["Sex"]),(( np.cos(( data["Age"] * np.tanh(np.sin(np.cos(data["Fare"])))))/ AH)))+ 
(np.tanh(np.tanh(-np.cos(( np.maximum(( np.cos(data["Fare"])) ,(L)) * data["Age"])))) / AH)+ 
(np.tanh(np.cos(( np.cos(data["Age"])+(data["Age"] + np.minimum(( data["Fare"]),(data["Age"])))))) / AH)+ 
(np.tanh(np.cos(( data["Age"] *(( -AH + np.sin(data["SibSp"])) + data["Fare"])))) / AH)+ 
(np.minimum(((( S)- data["Fare"])) ,(np.sin(( np.maximum(((AL)) ,(data["Fare"])) * data["SibSp"])))) * AH)+
np.sin(((np.maximum(( data["Embarked"]),(data["Age"])) * AH)*(((Q)* H)* data["Age"])))+
np.minimum(( data["Sex"]),(np.sin(-(np.minimum(((data["Cabin"] / AH)) ,(data["SibSp"])) *(data["Fare"] / AH)))))+
np.sin(np.sin(( data["Cabin"] *(data["Embarked"] +(np.tanh(-data["Age"])+ data["Fare"])))))+ 
(np.cos(np.cos(data["Fare"])) *(np.sin(( data["Embarked"] -(( T)* data["Fare"])))/ AH)) + 
(( np.minimum(( data["SibSp"]),(np.cos(data["Fare"])))* np.cos(data["SibSp"])) * np.sin(( data["Age"] / AH)))+ 
(np.sin(( np.sin(( data["SibSp"] * np.cos(( data["Fare"] * AH)))) +(data["Cabin"] * AH)))/ AH)+ 
(((data["Sex"] * data["SibSp"])* np.sin(np.sin(-(data["Fare"] * data["Cabin"])))) * AH)+ 
(np.sin(( data["SibSp"] *(((( G + V)* AH)/ AH)* data["Age"])))/ AH)+ 
(data["Pclass"] *(np.sin(((data["Embarked"] * data["Cabin"])*(data["Age"] -(R)))) / AH)) + 
(np.cos(((((-data["SibSp"] + data["Age"])+ data["Parch"])* data["Embarked"])/ AH)) / AH)+ 
(D * np.sin(((data["Age"] *(( data["Embarked"] * np.sin(data["Fare"])) * AH)) * AH)))+ 
(( np.minimum(((data["Age"] * A)) ,(data["Sex"])) - F)* np.tanh(np.sin(data["Pclass"])))+
-np.minimum(((np.cos(((AB)*(( data["Fare"] + data["Parch"])* AH)))/ AH)) ,(data["Fare"])) + 
(np.minimum(( np.cos(data["Fare"])) ,(data["SibSp"])) * np.minimum(( np.sin(data["Parch"])) ,(np.cos(( data["Embarked"] * AH)))))+ 
(np.minimum(((( data["Fare"] / AH)- E)) ,(C)) * np.sin(( K * data["Age"])))+
np.minimum(((M)) ,(((np.sin(data["Fare"])+ data["Embarked"])- np.cos(( data["Age"] *(P)))))) )
model = load_model("./unet_best1.model" )<predict_on_test>
def CleanData(data):
data.drop(['Ticket', 'Name'], inplace=True, axis=1)
 data.Sex.fillna('0', inplace=True)
 data.loc[data.Sex != 'male', 'Sex'] = 0
data.loc[data.Sex == 'male', 'Sex'] = 1
data.Cabin.fillna('0', inplace=True)
 data.loc[data.Cabin.str[0] == 'A', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'B', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'C', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'D', 'Cabin'] = 4
data.loc[data.Cabin.str[0] == 'E', 'Cabin'] = 5
data.loc[data.Cabin.str[0] == 'F', 'Cabin'] = 6
data.loc[data.Cabin.str[0] == 'G', 'Cabin'] = 7
data.loc[data.Cabin.str[0] == 'T', 'Cabin'] = 8
data.loc[data.Embarked == 'C', 'Embarked'] = 1
data.loc[data.Embarked == 'Q', 'Embarked'] = 2
data.loc[data.Embarked == 'S', 'Embarked'] = 3
data.Embarked.fillna(0, inplace=True)
 data.fillna(-1, inplace=True)
 return data.astype(float)
 def Outputs(data):
return np.round(1.-(1./(1.+np.exp(-data))))
def predict_result(model,x_test,img_size_target):
preds_test = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test += np.array([ np.fliplr(a)for a in model.predict(np.array([np.fliplr(x)for x in x_test])).reshape(-1, img_size_target, img_size_target)])
 return preds_test/2.0<predict_on_test>
raw_train = pd.read_csv('.. /input/titanic/train.csv')
 raw_test = pd.read_csv('.. /input/titanic/test.csv')
 cleanedTrain = CleanData(raw_train)
 cleanedTest = CleanData(raw_test )
preds_valid = predict_result(model,x_valid,img_size_target )<define_search_space>
thisArray = BIG.copy()
testPredictions = Outputs(GeneticFunction(cleanedTrain,thisArray[0],thisArray[1],thisArray[2],thisArray[3],thisArray[4],thisArray[5],thisArray[6],thisArray[7],thisArray[8],thisArray[9],thisArray[10],thisArray[11],thisArray[12],thisArray[13],thisArray[14],thisArray[15],thisArray[16],thisArray[17],thisArray[18],thisArray[19],thisArray[20],thisArray[21],thisArray[22],thisArray[23],thisArray[24],thisArray[25],thisArray[26],thisArray[27],thisArray[28],thisArray[29],thisArray[30],thisArray[31],thisArray[32],thisArray[33],thisArray[34],thisArray[35],thisArray[36],thisArray[37],thisArray[38]))
pdcheck = pd.DataFrame({'Survived': testPredictions.astype(int)})
 ret = pdcheck.Survived.where(pdcheck["Survived"].values==cleanedTrain["Survived"].values ).notna()
t,f = ret.value_counts()
score = 100/(t+f)*t
print("Training set score: ",score)
def filter_image(img):
if img.sum() < 100:
return np.zeros(img.shape)
 else:
return img
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric(y_valid.reshape(( -1, img_size_target, img_size_target)) , [filter_image(img)for img in preds_valid > threshold])for threshold in tqdm_notebook(thresholds)])
<categorify>
testPredictions = Outputs(GeneticFunction(cleanedTest,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM))
pdtest = pd.DataFrame({'PassengerId': cleanedTest.PassengerId.astype(int),
'Survived': testPredictions.astype(int)})
 pdtest.to_csv('submission.csv', index=False)
 pdtest.head()
def rle_encode(im):
pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<drop_column>
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler,MinMaxScaler
import pickle
import string
del x_train, x_valid, y_train, y_valid, preds_valid
gc.collect()<predict_on_test>
HOWMANYITERS = 10
x_test = np.array([(np.array(load_img(".. /input/test/images/{}.png".format(idx), grayscale = True)))/ 255 for idx in tqdm_notebook(test_df.index)] ).reshape(-1, img_size_target, img_size_target, 1)
 preds_test = predict_result(model,x_test,img_size_target )<compute_test_metric>
def GP_deap(evolved_train):
global HOWMANYITERS
outputs = evolved_train['Survived'].values.tolist()
evolved_train = evolved_train.drop(["Survived","PassengerId"],axis=1)
 inputs = evolved_train.values.tolist()
def protectedDiv(left, right):
try:
return left / right
except ZeroDivisionError:
return 1
def randomString(stringLength=10):

 letters = string.ascii_lowercase
return ''.join(random.choice(letters)for i in range(stringLength))
pset = gp.PrimitiveSet("MAIN", len(evolved_train.columns))
pset.addPrimitive(operator.add, 2)
 pset.addPrimitive(operator.sub, 2)
 pset.addPrimitive(operator.mul, 2)
 pset.addPrimitive(protectedDiv, 2)
 pset.addPrimitive(math.cos, 1)
 pset.addPrimitive(math.sin, 1)
 pset.addPrimitive(math.tanh,1)
 pset.addPrimitive(max, 2)
 pset.addPrimitive(min, 2)
 pset.addEphemeralConstant(randomString() , lambda: random.uniform(-10,10))
pset.renameArguments(ARG0='x1')
 pset.renameArguments(ARG1='x2')
 pset.renameArguments(ARG2='x3')
 pset.renameArguments(ARG3='x4')
 pset.renameArguments(ARG4='x5')
 pset.renameArguments(ARG5='x6')
 pset.renameArguments(ARG6='x7')
 pset.renameArguments(ARG7='x8')
 pset.renameArguments(ARG8='x9')
 pset.renameArguments(ARG9='x10')
 pset.renameArguments(ARG10='x11')
 pset.renameArguments(ARG11='x12')
 pset.renameArguments(ARG12='x13')
 pset.renameArguments(ARG13='x14')
 pset.renameArguments(ARG14='x15')
 pset.renameArguments(ARG15='x16')
 pset.renameArguments(ARG16='x17')
 pset.renameArguments(ARG17='x18')
 pset.renameArguments(ARG18='x19')
 pset.renameArguments(ARG19='x20')
 pset.renameArguments(ARG20='x21')
 pset.renameArguments(ARG21='x22')
 pset.renameArguments(ARG22='x23')
 pset.renameArguments(ARG23='x24')
 pset.renameArguments(ARG24='x25')
 pset.renameArguments(ARG25='x26')
 pset.renameArguments(ARG26='x27')
 pset.renameArguments(ARG27='x28')
 pset.renameArguments(ARG28='x29')
 pset.renameArguments(ARG29='x30')
 pset.renameArguments(ARG30='x31')
 pset.renameArguments(ARG31='x32')
 pset.renameArguments(ARG32='x33')
 pset.renameArguments(ARG33='x34')
 pset.renameArguments(ARG34='x35')
 pset.renameArguments(ARG35='x36')
 pset.renameArguments(ARG36='x37')
 pset.renameArguments(ARG37='x38')
 pset.renameArguments(ARG38='x39')
 pset.renameArguments(ARG39='x40')
 pset.renameArguments(ARG40='x41')
 pset.renameArguments(ARG41='x42')
 pset.renameArguments(ARG42='x43')
 pset.renameArguments(ARG43='x44')
 pset.renameArguments(ARG44='x45')
 pset.renameArguments(ARG45='x46')
 pset.renameArguments(ARG46='x47')
 pset.renameArguments(ARG47='x48')
 pset.renameArguments(ARG48='x49')
 pset.renameArguments(ARG49='x50')
 creator.create("FitnessMin", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMin)
 toolbox = base.Toolbox()
toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)
 toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
 toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 toolbox.register("compile", gp.compile, pset=pset)
 def evalSymbReg(individual):
func = toolbox.compile(expr=individual)
 return math.fsum(np.round(1.-(1./(1.+np.exp(-func(*in_)))))== out for in_, out in zip(inputs, outputs)) / len(evolved_train),
toolbox.register("evaluate", evalSymbReg)
 toolbox.register("select", tools.selTournament, tournsize=3)
 toolbox.register("mate", gp.cxOnePoint)
 toolbox.register("expr_mut", gp.genFull, min_=0, max_=3)
 toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
 toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17))
pop = toolbox.population(n=300)
 hof = tools.HallOfFame(1)
 stats_fit = tools.Statistics(lambda ind: ind.fitness.values)
 stats_size = tools.Statistics(len)
 stats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
 stats.register("avg", np.mean)
 stats.register("std", np.std)
 stats.register("min", np.min)
 stats.register("max", np.max)
 pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.7, mutpb=0.3, ngen=HOWMANYITERS, stats=stats,
halloffame=hof, verbose=True)
 func2 = toolbox.compile(expr=hof[0])
 return func2
def manualtree(df):
Model = pd.DataFrame(data = {'manual_tree':[]})
 male_title = ['Master']
for index, row in df.iterrows() :
Model.loc[index, 'manual_tree'] = 0
if(df.loc[index, 'Sex'] == 'female'):
Model.loc[index, 'manual_tree'] = 1
if(( df.loc[index, 'Sex'] == 'female')& 
(df.loc[index, 'Pclass'] == 3)& 
(df.loc[index, 'Embarked'] == 'S')& 
(df.loc[index, 'Fare'] > 8)
):
Model.loc[index, 'manual_tree'] = 0
if(( df.loc[index, 'Sex'] == 'male')& 
(df.loc[index, 'Title'] == 3)
):
Model.loc[index, 'manual_tree'] = 1
return Model
def MungeData(data):
title_list = [
'Dr', 'Mr', 'Master',
'Miss', 'Major', 'Rev',
'Mrs', 'Ms', 'Mlle','Col',
'Capt', 'Mme', 'Countess',
'Don', 'Jonkheer'
]
def replace_names_titles(x):
for title in title_list:
if title in x:
return title
data['Title'] = data.Name.apply(replace_names_titles)
 data['Title'] = data['Title'].replace(['Miss', 'Mrs','Ms', 'Mlle', 'Lady', 'Mme', 'the Countess', 'Dona'], 'Miss/Mrs/Ms')
 data['Title'] = data['Title'].replace(['Dr', 'Col', 'Major', 'Jonkheer', 'Capt', 'Sir', 'Don', 'Rev'], 'Dr/Military/Noble/Clergy')
 data['Title'] = data.Title.map({ 'Dr':1, 'Mr':2, 'Master':3, 'Miss':4, 'Major':5, 'Rev':6, 'Mrs':7, 'Ms':8, 'Mlle':9,
'Col':10, 'Capt':11, 'Mme':12, 'Countess':13, 'Don': 14, 'Jonkheer':15
})
 data = data.drop(['Name'],axis = 1)
 data.Title.fillna(0, inplace=True)
 data['Is_Married'] = 0
data['Is_Married'].loc[data['Title'] == 7] = 1
data["manual_tree"] = manualtree(data)
 data['Age'] = data.groupby(['Sex', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))
data['Relatives'] = data.SibSp + data.Parch
data['Fare_per_person'] = data.Fare / np.mean(data.SibSp + data.Parch + 1)
 med_fare = data.groupby(['Pclass', 'Parch', 'SibSp'] ).Fare.median() [3][0][0]
data = data.drop(['SibSp', 'Parch'], axis=1)
 data['Fare'] = data['Fare'].fillna(med_fare)
 data.Sex.fillna('0', inplace=True)
 data.loc[data.Sex != 'male', 'Sex'] = 0
data.loc[data.Sex == 'male', 'Sex'] = 1
data['Ticket_Frequency'] = data.groupby('Ticket')['Ticket'].transform('count')
 data = data.drop(['Ticket'], axis=1)
 data.Cabin.fillna('0', inplace=True)
 data.loc[data.Cabin.str[0] == 'A', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'B', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'C', 'Cabin'] = 1
data.loc[data.Cabin.str[0] == 'D', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'E', 'Cabin'] = 2
data.loc[data.Cabin.str[0] == 'F', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'G', 'Cabin'] = 3
data.loc[data.Cabin.str[0] == 'T', 'Cabin'] = 3
data.loc[data.Embarked == 'C', 'Embarked'] = 1
data.loc[data.Embarked == 'Q', 'Embarked'] = 2
data.loc[data.Embarked == 'S', 'Embarked'] = 3
data.Embarked.fillna(3, inplace=True)
 data["Cabin"] = data["Cabin"].astype(int)
 numeric_features = ['Relatives','Fare_per_person', 'Fare', 'Age','Ticket_Frequency']
for feature in numeric_features:
x = data[feature].values
min_max_scaler = MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x.reshape(-1, 1))
data[feature] = pd.DataFrame(x_scaled)
 cat_features = ['Pclass','Embarked', 'Sex', 'Cabin', 'Title','manual_tree','Is_Married']
encoded_features = []
for feature in cat_features:
encoded_feat = OneHotEncoder().fit_transform(data[feature].values.reshape(-1, 1)).toarray()
n = data[feature].nunique()
cols = ['{}_{}'.format(feature, n)for n in range(1, n + 1)]
encoded_df = pd.DataFrame(encoded_feat, columns=cols)
 encoded_df.index = data.index
encoded_features.append(encoded_df)
 data = pd.concat([data, *encoded_features], axis=1)
 return data.astype(float)
t1 = time.time()
pred_dict = {idx: rle_encode(filter_image(preds_test[i] > threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )<save_to_csv>
raw_train = pd.read_csv('.. /input/titanic/train.csv')
 raw_test = pd.read_csv('.. /input/titanic/test.csv')
 pass_id_train = raw_train["PassengerId"]
survived_train = raw_train["Survived"]
pass_id_test = raw_test["PassengerId"]
evolved_train = MungeData(raw_train)
 evolved_test = MungeData(raw_test )
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('submission.csv' )<set_options>
GeneticFunctionObject = GP_deap(evolved_train)
 with open("geneticfunction.pickle","wb")as file:
pickle.dump(GeneticFunction,file )
plt.style.use('seaborn-white')
 sns.set_style("white")
 %matplotlib inline 
<define_variables>
evolved_train = evolved_train.drop(["PassengerId","Survived"],axis=1)
 train_nparray = evolved_train.values.tolist()
trainPredictions = Outputs(np.array([GeneticFunctionObject(*x)for x in train_nparray]))
print("Your score based on Train set(Remember, Kaggle/Test set score will be different):")
 print(accuracy_score(survived_train.astype(int),trainPredictions.astype(int)))
 pd_train = pd.DataFrame({'PassengerId': pass_id_train.astype(int),
'Predicted': trainPredictions.astype(int),
'Survived': survived_train.astype(int)})
 pd_train.to_csv('gptrain_yourgenalgo.csv', index=False)
 evoled_test = evolved_test.drop(["PassengerId"],axis=1)
 test_nparray = evolved_test.values.tolist()
testPredictions = Outputs(np.array([GeneticFunctionObject(*x)for x in test_nparray]))
pd_test = pd.DataFrame({'PassengerId': pass_id_test.astype(int),
'Survived': testPredictions.astype(int)})
 pd_test.to_csv('submission_yourgenalgo.csv', index=False)