Kaggle competition: Conway's Reverse Game of Life 2020
train['Title'] = train['Title'].replace(['Lady.', 'Capt.', 'Col.',
'Don.', 'Dr.', 'Major.', 'Rev.', 'Jonkheer.', 'Dona.'], 'Rare.')
 train['Title'] = train['Title'].replace(['Countess.', 'Lady', 'Sir'], 'Royal')
 train['Title'] = train['Title'].replace('Mlle.', 'Miss.')
 train['Title'] = train['Title'].replace('Ms.', 'Miss.')
 train['Title'] = train['Title'].replace('Mme.', 'Mrs.')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<feature_engineering>
sample = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/sample_submission.csv')
 test = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/test.csv')
 train = pd.read_csv('/kaggle/input/conways-reverse-game-of-life-2020/train.csv')
test['Title'] = test['Title'].replace(['Lady.', 'Capt.', 'Col.',
'Don.', 'Dr.', 'Major.', 'Rev.', 'Jonkheer.', 'Dona.'], 'Rare.')
 test['Title'] = test['Title'].replace(['Countess.', 'Lady.', 'Sir.'], 'Royal.')
 test['Title'] = test['Title'].replace('Mlle.', 'Miss')
 test['Title'] = test['Title'].replace('Ms.', 'Miss.')
 test['Title'] = test['Title'].replace('Mme.', 'Mrs.')
<categorify>
train.id.nunique()
title_mapping = {"Mr.": 1, "Miss.": 2, "Mrs.": 3, "Master.": 4, "Royal.": 5, "Rare.": 6}
train['Title'] = train['Title'].map(title_mapping)
 train['Title'] = train['Title'].fillna(0)
 train.head()<categorify>
train.delta.value_counts()
title_mapping = {"Mr.": 1, "Miss.": 2, "Mrs.": 3, "Master.": 4, "Royal.": 5, "Rare.": 6}
test['Title'] = test['Title'].map(title_mapping)
 test['Title'] = test['Title'].fillna(0)
<drop_column>
train.start_0.value_counts()
test = test.drop(['Ticket'], axis = 1)
 test = test.drop(['Name'], axis = 1)
 test = test.drop(['Parch'], axis = 1)
 test = test.drop(['Fare','SibSp'], axis = 1)
 train.drop(['Name', 'Ticket'], axis = 1, inplace = True)
 train = train.drop(['Parch'], axis = 1)
 train = train.drop(['Fare','SibSp'], axis = 1 )<split>
train.stop_0.value_counts()
X_train, X_test, y_train, y_test = train_test_split(train.drop(['Survived','PassengerId'], axis=1),
train['Survived'], test_size = 0.2,
random_state = 0 )<train_model>
sample_start_1 = train.loc[1, train.columns.str.startswith('start')]
sample_stop_1 = train.loc[1, train.columns.str.startswith('stop')]
logisticRegression = LogisticRegression(max_iter = 30000)
 logisticRegression.fit(X_train, y_train)
<predict_on_test>
sample_start_1 = np.asarray(sample_start_1 ).reshape(25, 25)
 sample_stop_1 = np.asarray(sample_stop_1 ).reshape(25, 25 )
predictions = logisticRegression.predict(X_test)
 acc_LOG = round(accuracy_score(predictions, y_test)* 100, 2)
 print(acc_LOG)
 print(predictions)
<compute_test_metric>
train.sum() [1:].sort_values()
print(confusion_matrix(y_test, predictions))<compute_test_metric>
gs=train[:1][[col for col in train.columns if 'start' in col]].values.reshape(-1,1 )
accuracy=(( 88+50)/(88+50+22+19))
print('accuracy is: ',(round(accuracy, 2)*100))<train_model>
%%cython
c
@cython.cdivision(True)
 @cython.boundscheck(False)
 @cython.nonecheck(False)
 @cython.wraparound(False)
 cdef int calc_neighs(unsigned char[:, :] field, int i, int j, int n, int k):
cdef:
int neighs = 0;
int i_min = i - 1;
int i_pl = i + 1;
int j_min = j - 1;
int j_pl = j + 1;
neighs = 0
if i_min >= 0:
if j_min >= 0:
neighs += field[i_min, j_min]
neighs += field[i_min, j]
if j_pl < k:
neighs += field[i_min, j_pl]
if j_min >= 0:
neighs += field[i, j_min]
if j_pl < k:
neighs += field[i, j_pl]
if i_pl < n:
if j_min >= 0:
neighs += field[i_pl, j_min]
neighs += field[i_pl, j]
if j_pl < k:
neighs += field[i_pl, j_pl]
return neighs
@cython.cdivision(True)
 @cython.boundscheck(False)
 @cython.nonecheck(False)
 @cython.wraparound(False)
 cpdef make_move(unsigned char[:, :] field, int moves):
cdef:
int _, i, j, neighs;
int n, k;
int switch = 0;
unsigned char[:, :] cur_field;
unsigned char[:, :] next_field;
cur_field = np.copy(field)
 next_field = np.zeros_like(field, 'uint8')
 n = field.shape[0]
k = field.shape[1]
for _ in range(moves):
if switch == 0:
for i in range(n):
for j in range(k):
neighs = calc_neighs(cur_field, i, j, n, k)
 if cur_field[i, j] and neighs == 2:
next_field[i, j] = 1
elif neighs == 3:
next_field[i, j] = 1
else:
next_field[i, j] = 0
else:
for i in range(n):
for j in range(k):
neighs = calc_neighs(next_field, i, j, n, k)
 if next_field[i, j] and neighs == 2:
cur_field[i, j] = 1
elif neighs == 3:
cur_field[i, j] = 1
else:
cur_field[i, j] = 0
switch =(switch + 1)% 2
return np.array(next_field if switch else cur_field )
randomforest = RandomForestClassifier(random_state = 5, criterion = 'gini', max_depth = 10, max_features = 'auto', n_estimators = 500)
 randomforest.fit(X_train, y_train)
 pred = randomforest.predict(X_test)
 acc_randomforest = round(accuracy_score(pred, y_test)* 100, 2)
 print(acc_randomforest)
<train_model>
NROW, NCOL = 25, 25
def generate_samples(delta=1, n=32):
batch = np.split(np.random.binomial(1, 0.5,(NROW * n, NCOL)).astype('uint8'), n)
 Yy = [life.make_move(state, 5)for state in batch]
Xx = [life.make_move(state, 1)for state in Yy]
Y = np.array([y.ravel() for y in Yy])
 X = np.array([x.ravel() for x in Xx])
 return X, Y
def data_generator(delta=1, batch_size=32, ravel=True):
while True:
batch = np.split(np.random.binomial(1, 0.5,(NROW * batch_size, NCOL)).astype('uint8'),
batch_size)
 Yy = [make_move(state, 5)for state in batch]
Xx = [make_move(state, delta)for state in Yy]
if ravel:
Y = np.array([y.ravel() for y in Yy])
 X = np.array([x.ravel() for x in Xx])
 yield X, Y
else:
yield np.array(Xx)[:,:, :, np.newaxis], np.array(Yy)[:, :, :, np.newaxis]
gbk = GradientBoostingClassifier()
gbk.fit(X_train, y_train)
 pred = gbk.predict(X_test)
 acc_gbk = round(accuracy_score(pred, y_test)* 100, 2)
 print(acc_gbk )<create_dataframe>
def create_model(n_hidden_convs=2, n_hidden_filters=128, kernel_size=5):
nn = Sequential()
nn.add(Conv2D(n_hidden_filters, kernel_size, padding='same', activation='relu',
input_shape=(25, 25, 1)))
 nn.add(BatchNormalization())
 for i in range(n_hidden_convs):
nn.add(Conv2D(n_hidden_filters, kernel_size, padding='same', activation='relu'))
nn.add(BatchNormalization())
 nn.add(Conv2D(1, kernel_size, padding='same', activation='sigmoid'))
nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return nn
see={'TECHNIQUE':['RANDOM FOREST','LOGISTIC REGRESSION','GRADIENT BOOSTING'],'ACCURACY':[acc_randomforest,acc_LOG,acc_gbk]}
mod=pd.DataFrame(see)
 mod<save_to_csv>
models = []
for delta in range(1, 6):
model = create_model(n_hidden_convs=6, n_hidden_filters=256)
 es = EarlyStopping(monitor='loss', patience=9, min_delta=0.001)
 model.fit_generator(data_generator(delta=delta, ravel=False),
steps_per_epoch=500, epochs=50, verbose=1, callbacks=[es])
 models.append(model )
ids = test['PassengerId']
predictions = randomforest.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )<set_options>
test = pd.read_csv('.. /input/conways-reverse-game-of-life-2020/test.csv', index_col='id' )
np.random.seed(1234 )<load_from_csv>
submit_df = pd.DataFrame(index=test.index, columns=['start_' + str(i)for i in range(625)] )
dataset = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 y = dataset['Survived'].values
dataset.head()<data_type_conversions>
for delta in range(1, 6):
mod = models[delta-1]
delta_df = test[test.delta == delta].iloc[:, 1:].values.reshape(-1, 25, 25, 1)
 submit_df[test.delta == delta] = mod.predict(delta_df ).reshape(-1, 625 ).round(0 ).astype('uint8' )
<feature_engineering><EOS> 
submit_df.to_csv('submission.csv' )