<jupyter_start><jupyter_text>Split data into train and test<jupyter_code>normalized_train_data = normalized_data[normalized_data.Survived.notna()]
normalized_test_data = normalized_data[normalized_data.Survived.isna()]
normalized_train_data<jupyter_output><empty_output><jupyter_text>Creating and validating model<jupyter_code>x_train = normalized_train_data.drop(["Survived"], axis=1).values
y_train = normalized_train_data["Survived"].values

number_of_epochs = 50

number_of_folds = 7
number_of_samples = len(x_train) // number_of_folds

all_histories = []
for i in range(number_of_folds):
    print("processing fold #", i)
    
    partial_x_train = np.concatenate([x_train[:i*number_of_samples],
                                          x_train[(i+1)*number_of_samples:]])
    parital_y_train = np.concatenate([y_train[:i*number_of_samples],
                                          y_train[(i+1)*number_of_samples:]])
    
    partial_x_validation = x_train[i*number_of_samples:(i+1)*number_of_samples]
    partial_y_validation = y_train[i*number_of_samples:(i+1)*number_of_samples]
    
    model = models.Sequential()
    model.add(layers.Dense(64, activation="relu", input_shape=(x_train.shape[1],)))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(32, activation="relu"))
    model.add(layers.Dropout(0.2))
    model.add(layers.Dense(16, activation="relu"))
    model.add(layers.Dropout(0.1))
    model.add(layers.Dense(1, activation="sigmoid"))

    model.compile(RMSprop(lr=0.001),
                  loss="binary_crossentropy",
                  metrics=["acc"])

    history = model.fit(partial_x_train,
                        parital_y_train,
                        epochs=number_of_epochs,
                        batch_size=16,
                        validation_data=[partial_x_validation,partial_y_validation])
    all_histories.append(history.history)<jupyter_output>processing fold # 0
Train on 764 samples, validate on 127 samples
Epoch 1/50
764/764 [==============================] - 0s 580us/step - loss: 0.6560 - acc: 0.6309 - val_loss: 0.5543 - val_acc: 0.8031
Epoch 2/50
764/764 [==============================] - 0s 186us/step - loss: 0.5642 - acc: 0.7592 - val_loss: 0.4841 - val_acc: 0.8110
Epoch 3/50
764/764 [==============================] - 0s 133us/step - loss: 0.5238 - acc: 0.7788 - val_loss: 0.4637 - val_acc: 0.8031
Epoch 4/50
764/764 [==============================] - 0s 164us/step - loss: 0.5096 - acc: 0.7814 - val_loss: 0.4498 - val_acc: 0.8031
Epoch 5/50
764/764 [==============================] - 0s 151us/step - loss: 0.5166 - acc: 0.7801 - val_loss: 0.4440 - val_acc: 0.8110
Epoch 6/50
764/764 [==============================] - 0s 135us/step - loss: 0.4892 - acc: 0.7932 - val_loss: 0.4520 - val_acc: 0.8031
Epoch 7/50
764/764 [==============================] - 0s 150us/step - loss: 0.4741 - acc: 0.8010 - val_loss: 0.4477 - val_acc: 0.7[...]<jupyter_text>Drawing plots<jupyter_code>history_dict = history.history

loss_values = [np.mean([x["loss"][i] for x in all_histories]) for i in range(number_of_epochs)]
val_loss_values = [np.mean([x["val_loss"][i] for x in all_histories]) for i in range(number_of_epochs)]

epochs = range(1, len(loss_values) + 1)

plt.plot(epochs, loss_values, "bo", label="Training loss")
plt.plot(epochs, val_loss_values, "b", label="Validation loss", color="red")
plt.title("Training and validation loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()

plt.show()

acc_values = [np.mean([x["acc"][i] for x in all_histories]) for i in range(number_of_epochs)]
val_acc_values = [np.mean([x["val_acc"][i] for x in all_histories]) for i in range(number_of_epochs)]

plt.plot(epochs, acc_values, "bo", label="Training acc")
plt.plot(epochs, val_acc_values, "b", label="Validation acc", color="red")
plt.title("Training and validation acc")
plt.xlabel("Epochs")
plt.ylabel("Acc")
plt.legend()

plt.show()<jupyter_output><empty_output><jupyter_text>Evaluate results<jupyter_code>results = model.predict(normalized_test_data.drop(["Survived"], axis=1))
results = pd.DataFrame(results, columns=["Survived"], index=normalized_test_data.index)

results.loc[results.Survived < 0.5, ["Survived"]] = 0
results.loc[results.Survived >= 0.5, ["Survived"]] = 1
results = results.fillna(0)
results.Survived = results.Survived.astype(int)
results<jupyter_output><empty_output><jupyter_text>Save results<jupyter_code>results.to_csv("../Output/my_prediction.csv", index=True)<jupyter_output><empty_output><jupyter_text>Compare with genderr submission<jupyter_code>compare = pd.read_csv("../Dataset/gender_submission.csv", index_col=0)
compare = compare.rename(columns={"Survived": "Example"})
compare = pd.concat([compare, results], axis=1)
compare = compare.rename(columns={"Survived": "MyPrediction"})
compare[compare.Example != compare.MyPrediction]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18404277195147029, 'fraction_numerical': 0.05654945506888752, 'mean_word_length': 4.722352941176471, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '316720', 'license': 'MIT', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1751, 'n_words': 290, 'path': 'Source/survived_prediction.ipynb', 'repo_name': 'wiktor779/Kaggle-Titanic', 'size': '4863'}