<jupyter_start><jupyter_text>Load packages<jupyter_code>import pandas as pd
import numpy as np
import random as rnd

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier<jupyter_output><empty_output><jupyter_text>Load input data<jupyter_code>train_df = pd.read_csv('../data/train.csv')
test_df = pd.read_csv('../data/test.csv')
combine = [train_df, test_df]<jupyter_output><empty_output><jupyter_text>Add new columns1. from column ['Name'] extract title (e.g. Mr, Ms, ...) 2. create new column ['Family'] by adding siblings/spouses and parents on board<jupyter_code>for dataset in combine:
    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\.', expand=False)
    dataset['Family'] = dataset['SibSp']+dataset['Parch']<jupyter_output><empty_output><jupyter_text>Drop unwanted columns from train and test dataframes<jupyter_code>train_df = train_df.drop(['Name', 'PassengerId', 'SibSp','Parch','Ticket','Cabin'], axis=1)
test_df = test_df.drop(['Name', 'SibSp','Parch','Ticket','Cabin'], axis=1)
combine = [train_df, test_df]
train_df.head()<jupyter_output><empty_output><jupyter_text>Modify columns Modify column ['Title'] so that:* least common titles are grouped in catgory 'Rare'* duplicate titles (e.g. Mlle and Miss) are merged<jupyter_code>for dataset in combine:
    dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')

    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')<jupyter_output><empty_output><jupyter_text>Modify column ['Embarked'] so that:* the embarkation ports are marked as C = Cherbourg, Q = Queenstown, S = Southampton* NaN are filled with the most represented category (S = Southampton) Modify column ['Sex'] so that:* the categories are marek as 0 = male, 1 = female<jupyter_code>train_df.Embarked.unique()
for dataset in combine:
    dataset['Sex'] = dataset['Sex'].map( {'female': 1, 'male': 0} ).astype(int)
    dataset['Embarked'] = dataset['Embarked'].fillna('S').map( {'S': 1, 'C': 2, 'Q': 3} ).astype(int)<jupyter_output><empty_output><jupyter_text>Modify column ['Title'] so that:* a number is assigned to each category: 1 = Mr, 2 = Mrs, 3 = Miss, 4 = Master, 5 = Rare<jupyter_code>for dataset in combine:
        dataset['Title'] = dataset['Title'].map( {'Mr':1,
                                             'Mrs':2,
                                             'Miss':3,
                                             'Master':4,
                                             'Rare':5} ).astype(int)
test_df.head()<jupyter_output><empty_output><jupyter_text>Modify column ['Age'] so that:* NaN are guessed depending on the values of columns ['Sex'] and ['Pclass']* divide into ranges and assign an integer to each category<jupyter_code>guess_ages = np.zeros((2,3))
for dataset in combine:
    for i in range(0, 2):
        for j in range(0, 3):
            guess_df = dataset[(dataset['Sex'] == i) & (dataset['Pclass'] == j+1)]['Age'].dropna()

            # age_mean = guess_df.mean()
            # age_std = guess_df.std()
            # age_guess = rnd.uniform(age_mean - age_std, age_mean + age_std)

            age_guess = guess_df.median()

            # Convert random age float to nearest .5 age
            guess_ages[i,j] = int( age_guess/0.5 + 0.5 ) * 0.5
            
    for i in range(0, 2):
        for j in range(0, 3):
            dataset.loc[ (dataset.Age.isnull()) & (dataset.Sex == i) & (dataset.Pclass == j+1),\
                    'Age'] = guess_ages[i,j]

    dataset['Age'] = dataset['Age'].astype(int)

for dataset in combine:    
    dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
    dataset.loc[(dataset['Age'] > 16) & (dataset['Age'] <= 32), 'Age'] = 1
    dataset.loc[(dataset['Age'] > 32) & (dataset['Age'] <= 48), 'Age'] = 2
    dataset.loc[(dataset['Age'] > 48) & (dataset['Age'] <= 64), 'Age'] = 3
    dataset.loc[ dataset['Age'] > 64, 'Age']
train_df.head()<jupyter_output><empty_output><jupyter_text>Modify column ['Fare'] so that:* divide into ranges and assign an integer to each category<jupyter_code>for dataset in combine:
    dataset['Fare'].fillna(dataset['Fare'].dropna().median(), inplace=True)
    dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
    dataset.loc[(dataset['Fare'] > 7.91) & (dataset['Fare'] <= 14.454), 'Fare'] = 1
    dataset.loc[(dataset['Fare'] > 14.454) & (dataset['Fare'] <= 31), 'Fare']   = 2
    dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
    dataset['Fare'] = dataset['Fare'].astype(int)<jupyter_output><empty_output><jupyter_text>Set train and test dataframes for ML model<jupyter_code>titanic_train = train_df.drop("Survived", axis=1)
titanic_target = train_df["Survived"]
titanic_test  = test_df.drop("PassengerId", axis=1)<jupyter_output><empty_output><jupyter_text>Run some optimisation on the Random Forest classifier parameters<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score

best_param = {}
best_score = []

for i in range(10):
    X_train, X_test, y_train, y_test = train_test_split(titanic_train, titanic_target)

    clf = RandomForestClassifier()
    grid_values = {'n_estimators': [25, 50, 100],
                   'min_impurity_decrease':[0.,0.5,0.9]
                  }

    grid_clf_auc = GridSearchCV(clf, param_grid = grid_values, scoring = 'accuracy')
    grid_clf_auc.fit(X_train, y_train)
    y_decision_fn_scores_auc = grid_clf_auc.predict(X_test) 

    best_param[i] = grid_clf_auc.best_params_
    best_score.append(grid_clf_auc.best_score_)
best_param
best_score<jupyter_output><empty_output><jupyter_text>Create the Random Forest classifier and predict the accuracy on the test dataframe<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(titanic_train, titanic_target)
clf_forest=RandomForestClassifier(n_estimators=50).fit(X_train, y_train)

y_predict=clf_forest.predict(X_test)
accuracy_score(y_test, y_predict)<jupyter_output><empty_output><jupyter_text>Prepare output for submission<jupyter_code>Y_pred = clf_forest.predict(titanic_test)
submission = pd.DataFrame({
        "PassengerId": test_df["PassengerId"],
        "Survived": Y_pred
    })
submission
submission.to_csv('submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1472697994437125, 'fraction_numerical': 0.014931927975406236, 'mean_word_length': 4.409342834520982, 'pattern_counts': {'":': 2, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '637562', 'license': 'MIT', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2296, 'n_words': 520, 'path': 'Titanic/src/titanic.ipynb', 'repo_name': 'stemarcotti/data-science', 'size': '6831'}