<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
from sklearn import metrics 
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

data_raw = pd.read_csv('https://raw.githubusercontent.com/straylight77/data-sci-learning/master/titanic/train.csv')
data = data_raw.copy(deep=True)

data.sample(5)

####################################################################
# fill in missing data
data['Age'].fillna( data['Age'].median(), inplace=True )
data['Embarked'].fillna( data['Embarked'].mode()[0], inplace=True )
data['Fare'].fillna( data['Fare'].median(), inplace=True )

data.info()
# feature engineering 
data['Relatives'] = data['SibSp'] + data['Parch']

data['IsAlone'] = data['Relatives'].apply(lambda x: 0 if x > 0 else 1) 

# qcut vs cut: https://stackoverflow.com/questions/30211923/what-is-the-difference-between-pandas-qcut-and-pandas-cut
# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.qcut.html
data['FareBin'] = pd.qcut(data['Fare'], 4)

# https://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html
data['AgeBin'] = pd.cut(data['Age'].astype(int), 5)

# extract the passenger's title from their name (Mr, Miss, Dr, etc.)
data['Title'] = data['Name'].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]

# replace rare Titles (< stat_min) with 'Other'
stat_min = 10 
title_names = (data['Title'].value_counts() < stat_min)
data['Title'] = data['Title'].apply(lambda x: 'Other' if title_names.loc[x] == True else x)

data.info()
# convert types to work with the model
from sklearn import preprocessing

label = preprocessing.LabelEncoder()
data['Sex_Code'] = label.fit_transform(data['Sex'])
data['Embarked_Code'] = label.fit_transform(data['Embarked'])
data['Title_Code'] = label.fit_transform(data['Title'])
data['AgeBin_Code'] = label.fit_transform(data['AgeBin'])
data['FareBin_Code'] = label.fit_transform(data['FareBin'])

data.info()
#####################################################################
# prepare training data 
y = data.Survived

features = ['Pclass', 'Relatives', 'Sex_Code', 'Title_Code', 'AgeBin_Code', 'FareBin_Code' ]
X = data[features]

# split into validation and training data
#   train_X = data to be used to train a model (with labels in train_y)
#   val_X = data to be used to measure accuracy (compare predictions with val_y) 
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
print("training data rows: ", len(train_X))
print(" testing data rows: ", len(val_X))
print("original data rows: ", len(X))
if len(train_X) + len(val_X) == len(X):
    print("Good to go!") 
# create a model, train it and make predictions using the split validation data
model = RandomForestClassifier(random_state=1)
model.fit(train_X, train_y)

y_preds = model.predict(val_X)
print("Predicted rows: ", len(y_preds))
# measure accuracy and print
accuracy_pct = metrics.accuracy_score(val_y, y_preds) * 100
print(f"Accuracy: {accuracy_pct}%")

# measure the relative importance of each feature and plot 
stats = pd.DataFrame({
    "Feature": features,
    "Importance": model.feature_importances_
})
stats.sort_values(by='Importance', ascending=False, inplace=True)
stats.plot.bar(x='Feature', y='Importance');

# PREVIOUS ITERATIONS
# 66.816 - ['Pclass', 'SibSp', 'Parch', 'Fare'* ]
# 64.125 - ['Pclass', 'SibSp', 'Parch', 'Fare'*, 'Age'* ]
# 80.717 - ['Pclass'*, 'Relatives'*, 'IsAlone', 'Sex_Code'*, 'Embarked_Code', 'Title_Code'*, 'AgeBin_Code'*, 'FareBin_Code'* ]
# 82.062 - ['Pclass', 'Relatives', 'Sex_Code', 'Title_Code', 'AgeBin_Code', 'FareBin_Code' ] - 

# get predictions for competition data and prepare output file for submission
data_pred = pd.read_csv('https://raw.githubusercontent.com/straylight77/data-sci-learning/master/titanic/test.csv')

# do the same data processing that we did earlier

# fill in missing data
data_pred['Age'].fillna( data_pred['Age'].median(), inplace=True )
data_pred['Embarked'].fillna( data_pred['Embarked'].mode()[0], inplace=True )
data_pred['Fare'].fillna( data_pred['Fare'].median(), inplace=True )

# add new engineered features
data_pred['Relatives'] = data_pred['SibSp'] + data_pred['Parch']
data_pred['FareBin'] = pd.qcut(data_pred['Fare'], 4)
data_pred['AgeBin'] = pd.cut(data_pred['Age'].astype(int), 5)

# extract the passenger's title from their name (Mr, Miss, Dr, etc.)
data_pred['Title'] = data_pred['Name'].str.split(", ", expand=True)[1].str.split(".", expand=True)[0]

# replace rare Titles (< stat_min) with 'Other'
stat_min = 10 
title_names = (data_pred['Title'].value_counts() < stat_min)
data_pred['Title'] = data_pred['Title'].apply(lambda x: 'Other' if title_names.loc[x] == True else x)

label = preprocessing.LabelEncoder()
data_pred['Sex_Code'] = label.fit_transform(data_pred['Sex'])
data_pred['Embarked_Code'] = label.fit_transform(data_pred['Embarked'])
data_pred['Title_Code'] = label.fit_transform(data_pred['Title'])
data_pred['AgeBin_Code'] = label.fit_transform(data_pred['AgeBin'])
data_pred['FareBin_Code'] = label.fit_transform(data_pred['FareBin'])

data_pred.info()
#####################################################################
# okay, now we can create a new model based on all training data
model = RandomForestClassifier(random_state=1)
model.fit(X, y)

X_predict = data_pred[features]
predictions = model.predict(X_predict)

output_df = pd.DataFrame({'PassengerId': data_pred.PassengerId, 'Survived': predictions})

output_df

output_df.to_csv('my_submission.csv', index=False)
print("Your submission was successfully saved!")<jupyter_output>Your submission was successfully saved!
{'fraction_non_alphanumeric': 0.19550601556970984, 'fraction_numerical': 0.009907997169143666, 'mean_word_length': 6.851388888888889, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '243653', 'license': 'MIT', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1915, 'n_words': 412, 'path': 'titanic/titanic4.ipynb', 'repo_name': 'straylight77/data-sci-learning', 'size': '5652'}