Kaggle competition: Conway's Reverse Game of Life 2020
<SOS> metric: PostProcessorKernel Kaggle data source: conways-reverse-game-of-life-2020<categorify>
import pandas as pd
from tqdm import tqdm
import gc
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
dataset['Title'].replace(['Capt.'],'Board',inplace=True)
 dataset['Title'].replace(['Don.','Dona.','Lady.','Jonkheer.','Rev.','Sir.','the'],'Hon',inplace=True)
 dataset['Title'].replace(['Col.','Dr.','Major.','Master.'],'Professionals',inplace=True)
 dataset['Title'].replace(['Miss.','Mlle.','Mme.','Mr.','Mrs.','Ms.'],'NoHon', inplace=True)
 test['Title'].replace(['Capt.'],'Board',inplace=True)
 test['Title'].replace(['Don.','Dona.','Lady.','Jonkheer.','Rev.','Sir.','the'],'Hon',inplace=True)
 test['Title'].replace(['Col.','Dr.','Major.','Master.'],'Professionals',inplace=True)
 test['Title'].replace(['Miss.','Mlle.','Mme.','Mr.','Mrs.','Ms.'],'NoHon', inplace=True )<feature_engineering>
def get_features(delta=1):

 x_tr = []
x_te = []
nsteps = len(NEIGHBORS)
 for idx in range(nsteps):
right, up = NEIGHBORS[idx]
cols = [f"stop_{i + right + 25*up}" if(i + right + 25*up >=0 and i+ right + 25*up <625)\
else "pad" for i in range(625)]
new_cols = [f"n{idx}_{i}" for i in range(625)]
x_tr.append(tr.loc[tr.delta==delta, cols].values[:,:, np.newaxis])
 x_te.append(te.loc[te.delta==delta, cols].values[:,:, np.newaxis])
 x_tr = np.concatenate(x_tr, axis=2)
 x_te = np.concatenate(x_te, axis=2)
 y_tr = tr.loc[tr.delta==delta, INIT_COLS].values
gc.collect()
return x_tr, x_te, y_tr
def make_model(nh=9):

 nn = 100
inp = L.Input(name="grid", shape=(625,nh))
d1 = L.Dense(nn, name='d1', activation="relu" )(inp)
 d2 = L.Dense(nn, name='d2', activation="relu" )(d1)
 preds = L.Dense(1, name='preds', activation="sigmoid" )(d2)
 model = M.Model(inp, preds, name="ANN")
 model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
 return model
def make_conv(nh=1, h=2, nf=128, ks=5):
inp = L.Input(name="input", shape=(25, 25, nh))
for idx in range(h):
if idx==0:
x = L.Conv2D(nh, ks, padding="same", activation="relu", name=f"conv{idx+1}" )(inp)
 else:
x = L.Conv2D(nh, ks, padding="same", activation="relu", name=f"conv{idx+1}" )(x)
 x = L.BatchNormalization(name=f"norm{idx+1}" )(x)
 x = L.Conv2D(1, ks, padding="same", activation="sigmoid", name="last_conv" )(x)
 preds = L.Reshape(name="preds", target_shape=(625, 1))(x)
 model = M.Model(inp, preds, name="CNN")
 model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
 return model
def line_search(y_true, y_prob, start, end, breaks):

 space = np.linspace(start, end, breaks)
 best_threshold = 0.
 best_score = 0.
 for threshold in space:
acc = np.mean(y_true ==(y_prob > threshold))
if acc > best_score:
best_score = acc
best_threshold = threshold
print(f"threshold: {best_threshold}, score: {best_score}")
 return best_threshold, best_score
print(np.unique([x.split(' ')[0] if not(x.isdigit())else 'OnlyNumber' for x in dataset['Ticket']]))
dataset['Ticket'] = [x.split(' ')[0] if not(x.isdigit())else 'OnlyNumber' for x in dataset['Ticket']]
test['Ticket'] = [x.split(' ')[0] if not(x.isdigit())else 'OnlyNumber' for x in test['Ticket']]<categorify>
%%time
tr = pd.read_csv(".. /input/conways-reverse-game-of-life-2020/train.csv")
 te = pd.read_csv(".. /input/conways-reverse-game-of-life-2020/test.csv")
 tr["pad"] = -1
te["pad"] = -1
INIT_COLS = [f"start_{i}" for i in range(625)]
STOP_COLS = [f"stop_{i}" for i in range(625)]
print('before(train)-> '+ str(len(dataset.loc[dataset.Fare == 0.0]['Fare'])))
 print('before(test)-> '+ str(len(test.loc[test.Fare == 0.0]['Fare'])))
 dataset['Fare'].replace(0.0,np.nan,inplace=True)
 test['Fare'].replace(0.0,np.nan,inplace=True)
 meanFirst = dataset.loc[dataset.Pclass==1]['Fare'].mean()
print('First Class mean is ' + str(meanFirst))
meanSecond = dataset.loc[dataset.Pclass==2]['Fare'].mean()
print('Second Class mean is ' + str(meanSecond))
meanThird = dataset.loc[dataset.Pclass==3]['Fare'].mean()
print('Third Class mean is ' + str(meanThird))
means=[meanFirst,meanSecond,meanThird]
fcd = dataset.loc[dataset.Pclass == 1].index
scd = dataset.loc[dataset.Pclass == 2].index
tcd = dataset.loc[dataset.Pclass == 3].index
fct = test.loc[test.Pclass == 1].index
sct = test.loc[test.Pclass == 2].index
tct = test.loc[test.Pclass == 3].index
dataset.loc[fcd,'Fare'] = dataset.loc[fcd,'Fare'].fillna(means[0])
 dataset.loc[scd,'Fare'] = dataset.loc[scd,'Fare'].fillna(means[1])
 dataset.loc[tcd,'Fare'] = dataset.loc[tcd,'Fare'].fillna(means[2])
 test.loc[fct,'Fare'] = test.loc[fct,'Fare'].fillna(means[0])
 test.loc[sct,'Fare'] = test.loc[sct,'Fare'].fillna(means[1])
 test.loc[tct,'Fare'] = test.loc[tct,'Fare'].fillna(means[2])
 print('after(train)-> '+ str(len(dataset.loc[dataset.Fare == 0]['Fare'])))
 print('after(test)-> '+ str(len(test.loc[dataset.Fare == 0]['Fare'])))
 print('after(train)null -> '+ str(dataset['Fare'].count() -891))
print('after(test)null -> '+ str(test['Fare'].count() - 418))
minFare = 0
qntl25 = dataset['Fare'].quantile(0.25)
 qntl50 = dataset['Fare'].quantile(0.5)
 qntl75 = dataset['Fare'].quantile(0.75)
 qntplus = dataset['Fare'].quantile(0.75)+ 1.5*(qntl75-qntl50)
 maxFare = 1e6
dataset['Fare'] = pd.cut(dataset['Fare'], bins= [minFare,qntl25, qntl75,qntplus,maxFare], labels=['Low','Medium','High','VeryHigh'])
 test['Fare'] = pd.cut(test['Fare'], bins= [minFare,qntl25, qntl75,qntplus,maxFare], labels=['Low','Medium','High','VeryHigh'])
 sns.barplot(data=dataset, x='Fare', y='Survived' )<categorify>
NF = 2
NEIGHBORS = [(right, up)for up in range(-NF, NF+1)for right in range(-NF, NF+1)]
nh = len(NEIGHBORS )
encSex = preprocessing.OrdinalEncoder().fit(pd.DataFrame(dataset['Sex']))
encTitle = preprocessing.OrdinalEncoder().fit(pd.DataFrame(dataset['Title']))
encParchSibSp = preprocessing.OrdinalEncoder().fit(pd.DataFrame(dataset['Parch-SibSp']))
encFare = preprocessing.OrdinalEncoder().fit(pd.DataFrame(dataset['Fare']))
encEmbark = preprocessing.OneHotEncoder().fit(pd.DataFrame(dataset['Embarked']))
dataset['Sex']= encSex.transform(pd.DataFrame(dataset['Sex']))
dataset['Title']= encTitle.transform(pd.DataFrame(dataset['Title']))
dataset['Parch-SibSp']= encParchSibSp.transform(pd.DataFrame(dataset['Parch-SibSp']))
dataset['Fare']= encFare.transform(pd.DataFrame(dataset['Fare']))
dataset = pd.concat([dataset,pd.DataFrame(encEmbark.transform(pd.DataFrame(dataset['Embarked'])).toarray() ,columns=['Embark_C','Embark_Q','Embark_S'])], axis=1)
 dataset.drop(columns=['Embarked'],inplace=True)
 test = pd.concat([test,pd.DataFrame(encEmbark.transform(pd.DataFrame(test['Embarked'])).toarray() ,columns=['Embark_C','Embark_Q','Embark_S'])], axis=1)
 test.drop(columns=['Embarked'],inplace=True)
 test['Sex'] = encSex.transform(pd.DataFrame(test['Sex']))
test['Title'] = encTitle.transform(pd.DataFrame(test['Title']))
test['Parch-SibSp'] = encParchSibSp.transform(pd.DataFrame(test['Parch-SibSp']))
test['Fare']= encFare.transform(pd.DataFrame(test['Fare']))<drop_column>
grid = te[STOP_COLS].values
delta_te = te['delta'].values
middle =(nh - 1)// 2
dataset.drop(columns=['Cabin','PassengerId','Name','Ticket','Survived'], inplace=True)
 test.drop(columns=['Cabin','PassengerId','Name','Ticket'], inplace=True )<drop_column>
%%time
CONV = True
for idx in range(5):
print("===================================
")
 print(f"DELTA = {idx + 1}")
 x_tr, x_te, y_tr = get_features(delta=idx+1)
 if CONV:
x_tr = x_tr.reshape(( -1, 25, 25,nh))
x_te = x_te.reshape(( -1, 25, 25,nh))
net = make_conv(nh=nh, h=2, nf=128, ks=5)
 else:
net = make_model(nh)
 net.fit(x_tr, y_tr, batch_size=100, epochs=15)
 if not CONV:
print("naive accuracy",(y_tr==x_tr[:,:,middle] ).mean())
 prob_tr = net.predict(x_tr, batch_size=50, verbose=1)[:,:,0]
bt, bs = line_search(y_tr, prob_tr, 0.3, 0.6, 7)
 preds = net.predict(x_te, batch_size=50, verbose=1)[:,:,0]
grid[delta_te==idx+1] =(preds>bt ).astype(int)
 del x_tr, x_te, y_tr
gc.collect()
if sel_threshold >= 0.05 and sel_threshold < 0.1:
dataset.drop(columns=['Embark_S','Embark_Q','Embark_C','Title'],inplace=True)
 test.drop(columns=['Embark_S','Embark_Q','Embark_C','Title'],inplace=True)
 if sel_threshold >= 0.1 and sel_threshold < 0.15:
dataset.drop(columns=['Embark_S','Embark_Q','Embark_C','Parch-SibSp','Fare','Title'],inplace=True)
 test.drop(columns=['Embark_S','Embark_Q','Embark_C','Parch-SibSp','Fare','Title'],inplace=True)
 if sel_threshold == 0.15:
dataset.drop(columns=['Embark_S','Embark_Q','Embark_C','Parch-SibSp','Pclass','Title','Fare'],inplace=True)
 test.drop(columns=['Embark_S','Embark_Q','Embark_C','Parch-SibSp','Pclass','Title','Fare'],inplace=True )<normalization>
sub = te[['id']].copy()
tp = pd.DataFrame(grid, columns=INIT_COLS)
 sub = sub.join(tp)
 del tp
gc.collect()
scaler = preprocessing.StandardScaler()
X = scaler.fit_transform(dataset)
 test_X = scaler.transform(test )<define_variables>
sub.to_csv("submission.csv", index=False )