Kaggle competition: Conway's Reverse Game of Life 2020
<SOS> metric: PostProcessorKernel Kaggle data source: conways-reverse-game-of-life-2020<train_model>
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
import pandas as pd
from PIL import Image
import time
import math
from tqdm.auto import tqdm
clf = RandomForestClassifier(n_estimators=100, random_state=1)
 clf = clf.fit(train_X, train_y )<drop_column>
print(sys.version )
useful = importances.tail(33 ).index.tolist()
useful.remove('title_group_Mr')
 useful.remove('Fare')
 useful.remove('older_relative_age')
 train_X=train[useful]
train_X.shape<train_on_grid>
import tensorflow as tf
search_best_hyperparameters = False
if search_best_hyperparameters:
parameter_grid = {
'n_estimators': [10, 20, 50, 100, 200, 500],
'learning_rate': [0.1, 0.2, 0.5, 1, 1.2],
'random_state': [1]
}
model = AdaBoostClassifier()
gs = GridSearchCV(
 model,
scoring='accuracy',
param_grid=parameter_grid,
cv=4,
n_jobs=-1)
 gs.fit(train_X, train_y)
 params = gs.best_params_
print(params)
 else:
params = {
'learning_rate': 0.1,
'n_estimators': 500,
'random_state': 1
}<train_model>
tf.__version__
clf = AdaBoostClassifier(**params)
 clf = clf.fit(train_X, train_y)
 clf.score(train_X, train_y )<predict_on_test>
SIZE = 25
test = df[df['Survived'].isnull() ]
test_X = test[useful]
test_y = clf.predict(test_X )<data_type_conversions>
def life_step_1(X):

 nbrs_count = sum(np.roll(np.roll(X, i, 0), j, 1)
 for i in(-1, 0, 1)for j in(-1, 0, 1)
 if(i != 0 or j != 0))
return(nbrs_count == 3)|(X &(nbrs_count == 2))
submit = pd.DataFrame(test_y.astype(int), index=test_X.index, columns=['Survived'])
 submit.head()<save_to_csv>
train_df = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/train.csv')
 test_df = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/test.csv')
 print(train_df.shape)
 print(test_df.shape )
submit.to_csv('submission.csv' )<import_modules>
train_df.groupby(['delta'] ).size()
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 
<prepare_x_and_y>
sample_start = train_sample.loc[:, train_sample.columns.str.startswith('start')]
sample_stop = train_sample.loc[:, train_sample.columns.str.startswith('stop')]
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] 
<compute_test_metric>
start_arr = np.asarray(sample_start ).reshape(25, 25)
 stop_arr = np.asarray(sample_stop ).reshape(25, 25)
 time_step = train_sample['delta'].values[0]
print(time_step )
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)))))) )<feature_engineering>
updated_arr = np.copy(start_arr)
 steps = []
steps.append(updated_arr)
 for x in range(time_step):
updated_arr = life_step_1(updated_arr)
 steps.append(updated_arr )
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)))) 
<load_from_csv>
single_step_df = train_df.loc[train_df['delta'] == 1]
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 )<create_dataframe>
idx = 13970
single_img = np.asarray(single_step_df.loc[idx][2:627] ).reshape(25,25 ).astype('uint8' )
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)
<save_to_csv>
end_single_img_33 = life_step_1(single_img_33 )
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()<import_modules>
def postprocess(arr):
return arr[4:-4, 4:-4]
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler,MinMaxScaler
import pickle
import string<define_variables>
start_key = ['start_' + str(i)for i in range(625)]
stop_key = ['stop_' + str(i)for i in range(625)]
HOWMANYITERS = 10<categorify>
concatenate, Dropout, Lambda, MaxPooling2D, BatchNormalization
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)
<load_from_csv>
K.image_data_format()
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 )<load_pretrained>
GeneticFunctionObject = GP_deap(evolved_train)
 with open("geneticfunction.pickle","wb")as file:
pickle.dump(GeneticFunction,file )<save_to_csv>
trainY = single_step_df.loc[:, single_step_df.columns.str.startswith('start')].values.reshape(-1, 25, 25)
 trainX = single_step_df.loc[:, single_step_df.columns.str.startswith('stop')].values.reshape(-1, 25, 25 )
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)
<import_modules>
trainX = np.array([preprocess(xi)for xi in trainX] ).astype(np.float32)
 trainX = np.expand_dims(trainX, axis=-1 ).astype(np.float32 )
print(f'Using PyTorch v{torch.__version__}' )<load_from_csv>
trainY = np.array([preprocess(xi)for xi in trainY] ).astype(np.float32)
 trainY = np.expand_dims(trainY, axis=-1 ).astype(np.float32 )
train = pd.read_csv(".. /input/digit-recognizer/train.csv" )<define_variables>
def life_step_1_tensor(X):

 nbrs_count = tf.stack([tf.roll(tf.roll(X, i, 0), j, 1)
 for i in(-1, 0, 1)for j in(-1, 0, 1)
 if(i != 0 or j != 0)])
 nbrs_count = tf.squeeze(K.sum(nbrs_count, axis=0, keepdims=True), axis=0)
 nbrs_count =(nbrs_count == 3)|(( X == 1)&(nbrs_count == 2))
return tf.cast(nbrs_count, dtype=tf.uint32 )
transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize(( 0.5,),(0.5,))
])
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 def get_digits(df):

 labels = []
start_inx = 0
if 'label' in df.columns:
labels = [v for v in df.label.values]
start_inx = 1
digits = []
for i in range(df.pixel0.size):
digit = df.iloc[i].astype(float ).values[start_inx:]
digit = np.reshape(digit,(28,28))
digit = transform(digit ).type('torch.FloatTensor')
 if len(labels)> 0:
digits.append([digit, labels[i]])
 else:
digits.append(digit)
 return digits<init_hyperparams>
def postprocess_tensor(arr):
return arr[4:-4, 4:-4,:]
train_X = get_digits(train)
 num_workers = 0
batch_size = 64
valid_size = 0.2
num_train = len(train_X)
 indices = list(range(num_train))
np.random.shuffle(indices)
 split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]
train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetRandomSampler(valid_idx)
 train_loader = torch.utils.data.DataLoader(train_X, batch_size=batch_size,
sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(train_X, batch_size=batch_size,
sampler=valid_sampler, num_workers=num_workers)
 dataiter = iter(train_loader)
 images, labels = dataiter.next()
print(type(images))
print(images.shape)
 print(labels.shape )<init_hyperparams>
def wrap_pad(t, extra_dims):
s = tf.shape(t)
 m = tf.constant([extra_dims[0], extra_dims[1]])
 d = tf.constant([1, 3, 3, s.numpy() [-1]])
 t = tf.tile(t, d)[:, s[1]-m[0]:m[0]-s[1], s[2]-m[1]:m[1]-s[2], :]
paddings = tf.constant([[0,0],[1, 1],[1, 1], [0,0]])
 t = tf.pad(t, paddings, 'CONSTANT')
 return t
def calc_out(in_layers, stride, padding, kernel_size, pool_stride):

 return int(( 1+(in_layers - kernel_size +(2*padding)) /stride)/pool_stride)
 class Net(nn.Module):
def __init__(self):
super(Net, self ).__init__()
inputs = [1,32,64,64]
kernel_size = [5,5,3]
stride = [1,1,1]
pool_stride = [2,2,2]
layers = []
self.out = 28
self.depth = inputs[-1]
for i in range(len(kernel_size)) :
padding = int(kernel_size[i]/2)
 self.out = calc_out(self.out, stride[i], padding,
kernel_size[i], pool_stride[i])
 layers.append(nn.Conv2d(inputs[i], inputs[i+1], kernel_size[i],
stride=stride[i], padding=padding))
layers.append(nn.ReLU())
 layers.append(nn.Conv2d(inputs[i+1], inputs[i+1], kernel_size[i],
stride=stride[i], padding=padding))
layers.append(nn.ReLU())
 layers.append(nn.MaxPool2d(pool_stride[i],pool_stride[i]))
layers.append(nn.Dropout(p=0.2))
self.cnn_layers = nn.Sequential(*layers)
 print(self.depth*self.out*self.out)
 layers2 = []
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(self.depth*self.out*self.out, 512))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(512, 256))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(256, 256))
layers2.append(nn.Dropout(p=0.2))
layers2.append(nn.Linear(256, 10))
self.fc_layers = nn.Sequential(*layers2)
 def forward(self, x):
x = self.cnn_layers(x)
 x = x.view(-1, self.depth*self.out*self.out)
 x = self.fc_layers(x)
 return x
model = Net()
model<choose_model_class>
t = trainY[0, :, :, :]
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005 )<train_model>
def custom_loss(y_actual,y_pred):
pred_fwd = y_pred
actual_fwd = y_actual
y_actual_f = tf.cast(K.flatten(actual_fwd), tf.float32)
 y_pred_f = tf.cast(K.flatten(pred_fwd), tf.float32)
 bce = BinaryCrossentropy(from_logits=False)
 return bce(y_actual_f, y_pred_f )
n_epochs = 25
valid_loss_min = np.Inf
print(device)
 model.to(device)
 tLoss, vLoss = [], []
for epoch in range(n_epochs):
train_loss = 0.0
valid_loss = 0.0
model.train()
for data, target in train_loader:
data = data.to(device)
 target = target.to(device)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item() *data.size(0)
 model.eval()
for data, target in valid_loader:
data = data.to(device)
 target = target.to(device)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item() *data.size(0)
 train_loss = train_loss/len(train_loader.dataset)
 valid_loss = valid_loss/len(valid_loader.dataset)
 tLoss.append(train_loss)
 vLoss.append(valid_loss)
 print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
 epoch, train_loss, valid_loss))
if valid_loss <= valid_loss_min:
print('Validation loss decreased({:.6f} --> {:.6f} ).Saving model...'.format(
 valid_loss_min,
valid_loss))
torch.save(model.state_dict() , 'model_cifar.pt')
 valid_loss_min = valid_loss<load_from_disk>
def crglnet_v1() :
inputShape =(33, 33, 1)
 inputs = Input(inputShape)
 c1 = Conv2D(64,(3, 3), activation='elu', padding='same' )(inputs)
 c2 = Conv2D(64,(3, 3), activation='elu', padding='same' )(c1)
 c3 = Conv2D(128,(5, 5), activation='elu', padding='same' )(c2)
 c4 = Conv2D(64,(3, 3), activation='elu', padding='same' )(c3)
 c5 = Conv2D(64,(3, 3), activation='elu', padding='same' )(c4)
 c6 = Conv2D(1,(1, 1), activation='sigmoid' )(c5)
 model = Model(inputs=[inputs], outputs=[c6])
 return model
model.load_state_dict(torch.load('model_cifar.pt')) ;<find_best_params>
def crglnet_v2() :
inputShape =(33, 33, 1)
 inputs = Input(inputShape)
 c1 = Conv2D(32,(3, 3), activation='elu', padding='same' )(inputs)
 c2 = Conv2D(64,(3, 3), activation='elu', padding='same' )(c1)
 c3 = Conv2D(128,(5, 5), activation='elu', padding='same' )(c2)
 c4 = concatenate([Conv2D(64,(3, 3), activation='elu', padding='same' )(c3), c2])
 c5 = concatenate([Conv2D(32,(3, 3), activation='elu', padding='same' )(c4), c1])
 c6 = Conv2D(64,(3, 3), activation='elu', padding='same' )(c5)
 c7 = Conv2D(32,(3, 3), activation='elu', padding='same' )(c6)
 c8 = Conv2D(1,(1, 1), activation='sigmoid' )(c7)
 model = Model(inputs=[inputs], outputs=[c8])
 return model
test_loss = 0.0
class_correct = [0]*10
class_total = [0]*10
model.eval()
conf_matrix = np.zeros(( 10,10))
for data, target in valid_loader:
data = data.to(device)
 target = target.to(device)
 output = model(data)
 loss = criterion(output, target)
 test_loss += loss.item() *data.size(0)
 _, pred = torch.max(output, 1)
 correct_tensor = pred.eq(target.data.view_as(pred))
correct = np.squeeze(correct_tensor.numpy())if device == "cpu" else np.squeeze(correct_tensor.cpu().numpy())
 for i in range(target.size(0)) :
label = target.data[i]
class_correct[label] += correct[i].item()
class_total[label] += 1
conf_matrix[label][pred.data[i]] += 1
test_loss = test_loss/len(valid_loader.dataset)
 print('Test Loss: {:.6f}
'.format(test_loss))
for i in range(10):
if class_total[i] > 0:
print('Test Accuracy of %3s: %2d%%(%2d/%2d)' %(
 i, 100 * class_correct[i] / class_total[i],
np.sum(class_correct[i]), np.sum(class_total[i])))
 else:
print('Test Accuracy of %3s: N/A(no training examples)' %(classes[i]))
print('
Test Accuracy(Overall): %2d%%(%2d/%2d)' %(
 100.* np.sum(class_correct)/ np.sum(class_total),
np.sum(class_correct), np.sum(class_total)) )<load_from_csv>
model = crglnet_v2()
test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 test_X = get_digits(test)
 test_loader = torch.utils.data.DataLoader(test_X, batch_size=batch_size,
num_workers=num_workers )<prepare_output>
opt = Adam(lr=1e-3)
 model.compile(optimizer=opt, loss=custom_loss, metrics=['accuracy', tf.keras.metrics.MeanIoU(num_classes=2)] )
ImageId, Label = [],[]
for data in test_loader:
data = data.to(device)
 output = model(data)
 _, pred = torch.max(output, 1)
 for i in range(len(pred)) :
ImageId.append(len(ImageId)+1)
 Label.append(pred[i].cpu().numpy())
 sub = pd.DataFrame(data={'ImageId':ImageId, 'Label':Label})
 sub.describe<save_to_csv>
from sklearn.model_selection import train_test_split
sub.to_csv("submission.csv", index=False )<install_modules>
from sklearn.model_selection import train_test_split
!pip install autofeat<set_options>
train_x, test_x, train_y, test_y = train_test_split(trainX, trainY, test_size=0.2, random_state=42 )
warnings.filterwarnings("ignore")
 pd.set_option('max_columns', 100 )<load_from_csv>
def step(train_x, true_y):
loss = []
with tf.GradientTape() as tape:
pred_y = model(train_x)
 pred_y = tf.cast(pred_y,tf.float32)
 model_loss = custom_loss(true_y, pred_y)
 loss.append(model_loss.numpy())
 model_gradients = tape.gradient(model_loss, model.trainable_variables)
 opt.apply_gradients(zip(model_gradients, model.trainable_variables))
return np.mean(loss )
traindf = pd.read_csv('.. /input/titanic/train.csv' ).set_index('PassengerId')
 testdf = pd.read_csv('.. /input/titanic/test.csv' ).set_index('PassengerId')
 df = pd.concat([traindf, testdf], axis=0, sort=False )<feature_engineering>
epochs = 20
batch_size = 32
bat_per_epoch = math.floor(len(train_x)/ batch_size)
 epoch_loss = []
for epoch in tqdm(range(epochs)) :
step_loss = []
for i in tqdm(range(bat_per_epoch)) :
n = i*batch_size
step_loss.append(step(train_x[n:n+batch_size], train_y[n:n+batch_size]))
epoch_loss.append(np.mean(step_loss))
print(f'Epoch: {epoch}, Loss: {epoch_loss[epoch]}')
df = pd.concat([traindf, testdf], axis=0, sort=False)
 df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['IsWomanOrBoy'] =(( df.Title == 'Master')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['WomanOrBoyCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).count())
 df['WomanOrBoyCount'] = df.mask(df.IsWomanOrBoy, df.WomanOrBoyCount - 1, axis=0)
 df['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).sum())
 df['FamilySurvivedCount'] = df.mask(df.IsWomanOrBoy, df.FamilySurvivedCount - \
df.Survived.fillna(0), axis=0)
 df['WomanOrBoySurvived'] = df.FamilySurvivedCount / df.WomanOrBoyCount.replace(0, np.nan)
 df.WomanOrBoyCount = df.WomanOrBoyCount.replace(np.nan, 0)
 df['Alone'] =(df.WomanOrBoyCount == 0)
 df['Title'] = df['Title'].replace('Ms','Miss')
 df['Title'] = df['Title'].replace('Mlle','Miss')
 df['Title'] = df['Title'].replace('Mme','Mrs')
 df['Embarked'] = df['Embarked'].fillna('S')
 df['Deck'] = df['Cabin'].apply(lambda s: s[0] if pd.notnull(s)else 'M')
 df.loc[(df['Deck'] == 'T'), 'Deck'] = 'A'
med_fare = df.groupby(['Pclass', 'Parch', 'SibSp'] ).Fare.median() [3][0][0]
df['Fare'] = df['Fare'].fillna(med_fare)
 df['Age'] = df.groupby(['Sex', 'Pclass', 'Title'])['Age'].apply(lambda x: x.fillna(x.median()))
df['Family_Size'] = df['SibSp'] + df['Parch'] + 1
cols_to_drop = ['Name','Ticket','Cabin']
df = df.drop(cols_to_drop, axis=1)
 df.WomanOrBoySurvived = df.WomanOrBoySurvived.fillna(0)
 df.WomanOrBoyCount = df.WomanOrBoyCount.fillna(0)
 df.FamilySurvivedCount = df.FamilySurvivedCount.fillna(0)
 df.Alone = df.Alone.fillna(0)
 df.Alone = df.Alone*1<prepare_x_and_y>
model.compile(optimizer=opt, loss=custom_loss, metrics=['accuracy', tf.keras.metrics.MeanIoU(num_classes=2)])
 print(model.evaluate(test_x, test_y, verbose=0))
target = df.Survived.loc[traindf.index]
df = df.drop(['Survived'], axis=1)
 train, test = df.loc[traindf.index], df.loc[testdf.index]<define_variables>
preds = model.predict(test_x)
 preds.shape
numerics = ['int8', 'int16', 'int32', 'int64', 'float16', 'float32', 'float64']
categorical_columns = []
features = train.columns.values.tolist()
for col in features:
if train[col].dtype in numerics: continue
categorical_columns.append(col)
 categorical_columns<data_type_conversions>
preds_thresh = tf.where(preds>0.5,1,0)
 pred_fwd = tf.map_fn(life_step_1_tensor, preds_thresh, fn_output_signature=tf.uint32)
 actual_fwd = tf.map_fn(life_step_1_tensor, test_y, fn_output_signature=tf.uint32 )
for col in categorical_columns:
if col in train.columns:
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values))<choose_model_class>
start_features = [f for f in train_df.columns if "start" in f]
stop_features = [f for f in train_df.columns if "stop" in f]
model = AutoFeatRegressor()
model<categorify>
sub = pd.DataFrame()
m = tf.keras.metrics.Accuracy()
accuracies = []
t1 = tqdm(range(1,6), desc=f'Delta: ')
 for delta in t1:
m.reset_states()
t1.set_description(f'Delta: {delta}')
 t1.refresh()
test_data_iter = train_df.loc[train_df['delta'] == delta]
tmp_sub = test_data_iter[["id"]].copy()
testY = test_data_iter.loc[:, test_data_iter.columns.str.startswith('start')].values.reshape(-1, 25, 25)
 testX = test_data_iter.loc[:, test_data_iter.columns.str.startswith('stop')].values.reshape(-1, 25, 25)
 testX = np.array([preprocess(xi)for xi in testX] ).astype(np.float32)
 testX = np.expand_dims(testX, axis=-1 ).astype(np.float32)
 testY = np.array([preprocess(xi)for xi in testY] ).astype(np.float32)
 testY = np.expand_dims(testY, axis=-1 ).astype(np.float32)
 t2 = tqdm(range(delta))
for i in t2:
if i == 0:
preds = model.predict(testX)
 else:
preds = tf.where(preds>0.5,1,0)
 preds = model.predict(preds)
 preds = tf.cast(tf.where(preds>0.5,1,0), tf.uint32)
 m.update_state(preds, testY)
 acc = m.result().numpy()
print(f'Accuracy: {acc}')
 accuracies.append(acc)
 preds = preds[:, 4:-4, 4:-4,:].numpy()
tmp = pd.DataFrame(preds.reshape(-1, 625 ).astype(np.uint8), columns=start_features, index=tmp_sub['id'])
 tmp_sub = tmp_sub.join(tmp)
 sub = sub.append(tmp_sub)
 sub.sort_index(inplace = True )
X_train_feature_creation = model.fit_transform(train, target)
 X_test_feature_creation = model.transform(test)
 X_train_feature_creation.head()<count_values>
print(f'Mean accuracy: {np.array(accuracies ).mean() }')
 print(f'LB score estimate from training: {1 - np.array(accuracies ).mean() }' )
print('Number of new features -',X_train_feature_creation.shape[1] - train.shape[1] )<concatenate>
sub = pd.DataFrame()
m = tf.keras.metrics.Accuracy()
accuracies = []
t1 = tqdm(range(1,6), desc=f'Delta: ')
 for delta in t1:
m.reset_states()
t1.set_description(f'Delta: {delta}')
 t1.refresh()
test_data_iter = test_df.loc[test_df['delta'] == delta]
tmp_sub = test_data_iter[["id"]].copy()
tmp_sub.set_index(tmp_sub['id'].values)
 testX = test_data_iter.loc[:, test_data_iter.columns.str.startswith('stop')].values.reshape(-1, 25, 25)
 testX = np.array([preprocess(xi)for xi in testX] ).astype(np.float32)
 testX = np.expand_dims(testX, axis=-1 ).astype(np.float32)
 t2 = tqdm(range(delta))
for i in t2:
if i == 0:
preds = model.predict(testX)
 else:
preds = tf.where(preds>0.5,1,0)
 preds = model.predict(preds)
 preds = tf.cast(tf.where(preds>0.5,1,0), tf.uint32)
 preds = preds[:, 4:-4, 4:-4,:].numpy()
tmp = pd.DataFrame(preds.reshape(-1, 625 ).astype(np.uint8), columns=start_features, index=tmp_sub['id'].values)
 tmp.insert(loc = 0, column='id', value=tmp_sub['id'].values)
 sub = sub.append(tmp)
 sub.sort_index(inplace = True)
 sub.reset_index(drop = True, inplace = True )
df2 = pd.concat([df.WomanOrBoySurvived.fillna(0), df.Alone, df.Sex.replace({'male': 0, 'female': 1})], axis=1)
 train2, test2 = df2.loc[traindf.index], df2.loc[testdf.index]<categorify>
sub.to_csv('submission.csv', index=False )
<count_values><EOS> 
sub.to_csv('submission.csv', index=False )