<jupyter_start><jupyter_text>Table of content

- [Titanic](Titanic)
  - [Setup](Setup)
  - [Data](Data)
    - [Download](Download)
    - [Explore with QuickDA](Explore-with-QuickDA)
    - [Split Data](Split-Data)
  - [Model's Common Functions](Model's-Common-Functions)
  - [Baseline Only Females Survived : 0.76315](Baseline-Only-Females-Survived-:-0.76315)
  - [Log Sex Pclass : 0.76555](Log-Sex-Pclass-:-0.76555)
    - [Transformations](Transformations)
    - [Model](Model)
    - [Submission](Submission)  Titanic This notebook has been inspired from the book [*Handson-Machine Learning with Scikit-learn, Tensorflow and Keras*](https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/). 

Thanks to the author, [<NAME>](https://github.com/ageron).  Setup What does the environment require?<jupyter_code># Python ≥3.5 is required
from pathlib import Path
import sys

import numpy as np
import pandas as pd
import sklearn

assert sklearn.__version__ >= '0.20'
assert sys.version_info >= (3, 5)

np.random.seed(42)<jupyter_output><empty_output><jupyter_text>Data  Download<jupyter_code>def load_titanic_dataset(filename, path='titanic_dataset'):
    csv_path = Path.joinpath(Path(path), filename)
    return pd.read_csv(csv_path)


data = load_titanic_dataset('train.csv')
submit = load_titanic_dataset('test.csv')
gender_submission = load_titanic_dataset('gender_submission.csv')<jupyter_output><empty_output><jupyter_text>Split Data Before you modify your data, you need to create a test set!

The sex is an important feature. Therefore, we need to keep the same proportions of males and females in both sets with `stratify=data['Sex']`.<jupyter_code>from sklearn.model_selection import train_test_split

train, test = train_test_split(data,
                               test_size=0.2,
                               random_state=42,
                               stratify=data['Sex'])<jupyter_output><empty_output><jupyter_text>Model's Common Functions For [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) codes' purposes. Don't mind them, come back if you need to.<jupyter_code>from sklearn.metrics import accuracy_score


def show_accuracy(predictions, ground_truth):
    accuracy = accuracy_score(predictions, ground_truth)
    print(f'Test set\'s accuracy : {accuracy:.5f}.')


def save_submission(passenger_id, pred, FILE_NAME='submission_000'):
    FILE_NAME += '.csv'
    submission = pd.DataFrame({
        'PassengerId': passenger_id,
        'Survived': pred
    })
    output_dir = Path('submissions')
    output_dir.mkdir(parents=True, exist_ok=True)

    submission.to_csv(output_dir.joinpath(FILE_NAME), index=False)<jupyter_output><empty_output><jupyter_text>Baseline Only Females Survived : 0.76315 Since the females' survival rate is 74.2% and the males, 18.9%,
we can do a quick & easy model in which every female survived and every male died.<jupyter_code>def baseline_female(data):
    predictions = np.zeros(data.shape[0])
    predictions[data['Sex'] == 'male'] = 0
    predictions[data['Sex'] == 'female'] = 1
    return predictions
predictions = baseline_female(test)
show_accuracy(predictions, test['Survived'])<jupyter_output>Test set's accuracy : 0.77654.<jupyter_text>Not bad, let's do a submission :<jupyter_code>predictions = baseline_female(submit)
predictions[1] = 0  # Otherwise, Kaggle won't compute your score...

passenger_id = submit['PassengerId']
FILE_NAME = 'baseline_female_00'

save_submission(passenger_id, predictions, FILE_NAME)<jupyter_output><empty_output><jupyter_text>Log Sex : 0.76555 Now, let's do a *machine learning* model.<jupyter_code>train_copy = train.copy()
test_copy = test.copy()
submit_copy = submit.copy()<jupyter_output><empty_output><jupyter_text>Transformations Here, you can add some attributes to `x_att`.<jupyter_code>x_attributes = ['Sex', 'Pclass']
y_attribute = ['Survived']


x_train = train_copy[x_attributes]
y_train = train_copy[y_attribute]

x_test = test_copy[x_attributes]
y_test = test_copy[y_attribute]

x_submit = submit_copy[x_attributes]<jupyter_output><empty_output><jupyter_text>**You can do here some data preprocessing.** Machine learning algorithms don't understand strings, but do understand vectors.

Therefore you should use `OneHotEncoder()` to transform your feature this way (if `sparse=False`):
* `'male' -> [1, 0]`
* `'female' -> [0, 1]`

If we use two features, the two will be merged into one vector :
* `'male from first class' -> [1, 0, 1, 0, 0]`
* `'female from first class' -> [0, 1, 1, 0, 0]`
* `'female from second class' -> [0, 1, 0, 1, 0]`

As you can see, the first two values of the vector determine if the instance is a male or a female and the last 3 values, the passenger's class.

_Can you tell why we use `OneHotEncoder` on `Pclass`?_<jupyter_code>from sklearn.preprocessing import OneHotEncoder

one_hot = OneHotEncoder() 

x_train_tfm = one_hot.fit_transform(x_train)
x_test_tfm = one_hot.fit_transform(x_test)
x_submit_tfm = one_hot.fit_transform(x_submit)<jupyter_output><empty_output><jupyter_text>Model Once you transformed the data into something *edible* for your algorithm, you can train it.<jupyter_code>from sklearn.linear_model import LogisticRegression

log_reg = LogisticRegression(random_state=42)
log_reg.fit(x_train_tfm, np.array(y_train).ravel())

test_pred = log_reg.predict(x_test_tfm)

show_accuracy(test_pred, y_test['Survived'])<jupyter_output>Test set's accuracy : 0.77654.<jupyter_text>This is same result that we got with the baseline. How should you interpret this accuracy?  Submission<jupyter_code>passenger_id = submit['PassengerId']
pred = log_reg.predict(x_submit_tfm)
FILE_NAME = 'log_sex_pclass'

save_submission(passenger_id, pred, FILE_NAME)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.113776559287183, 'fraction_numerical': 0.019705277587388624, 'mean_word_length': 4.974411463664278, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 51, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '399624', 'license': 'MIT', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1941, 'n_words': 471, 'path': 'baselines.ipynb', 'repo_name': 'NathanielDamours/kaggle-titanic-template', 'size': '5844'}