<jupyter_start><jupyter_text>Titanic Machine Learning  Libraries and Tools<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import Imputer
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.metrics import classification_report
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>Open Data<jupyter_code># Open data files and read into dataframes
train = (pd.read_csv('train.csv')).set_index('PassengerId')
X_score = (pd.read_csv('test.csv')).set_index('PassengerId')
# Split training features from class
X_train = train[list(X_score)]
y_train = train[['Survived']]<jupyter_output><empty_output><jupyter_text>Data Cleaning and Feature Selection  Imputation of Missing Values<jupyter_code>## Use median imputation for age
median_age = np.median(X_train['Age'].dropna())
X_train = X_train.fillna({'Age': median_age})
X_score = X_score.fillna({'Age': median_age})
# Only use the letter for the cabin
def cabin_clean(row):
    if not pd.isnull(row['Cabin']):
        return (row['Cabin'])[0]
X_train.Cabin = X_train.apply(cabin_clean, axis=1)
X_score.Cabin = X_score.apply(cabin_clean, axis=1)
# Fill any NaNs with most common cabin letter from training set
cabin_imp_val = X_train.Cabin.value_counts().index[0]
X_train = X_train.fillna({'Cabin': cabin_imp_val})
X_score = X_score.fillna({'Cabin': cabin_imp_val})
# Fill NaNs with most common categorical value
embarked_imp_val = X_train.Embarked.value_counts().index[0]
X_train = X_train.fillna({'Embarked': embarked_imp_val})
X_score = X_score.fillna({'Embarked': embarked_imp_val})
# Use median impuation for fare
median_fare = np.median(X_train['Fare'].dropna())
X_train = X_train.fillna({'Fare': median_fare})
X_score = X_score.fillna({'Fare': median_fare})<jupyter_output><empty_output><jupyter_text>Encode Categorical Features<jupyter_code># Features with presumably no predictive power hsould be dropped rather than encoded
X_train = X_train.drop(['Name', 'Ticket'], axis=1)
X_score = X_score.drop(['Name', 'Ticket'], axis=1)
# One-hot-encode remaining categorical features
X_train = pd.get_dummies(X_train, columns=['Sex', 'Cabin', 'Embarked'])
X_score = pd.get_dummies(X_score, columns=['Sex', 'Cabin', 'Embarked'])
# Add empty column for 'Cabin_T' which was not in score
X_score['Cabin_T'] = 0
# Ensure columns are ordered the same for scikit-learn
X_score = X_score[list(X_train.columns)]<jupyter_output><empty_output><jupyter_text>Model Analysis  Split Known Data for Training and Testing<jupyter_code>X_train_split, X_test_split, y_train_split, y_test_split = train_test_split(
    X_train, y_train, random_state=42
)<jupyter_output><empty_output><jupyter_text>Perform Cross Validation<jupyter_code>classifiers = [
    
]

classifier_names = [
    '',
    ''
]
test_pipeline = Pipeline([
    ('grd', GradientBoostingClassifier(n_estimators=500))
])
test_pipeline.fit(X_train_split, y_train_split.values.ravel())
y_pred_split = test_pipeline.predict(X_test_split)
print(classification_report(y_test_split, y_pred_split))
print(metrics.accuracy_score(y_test_split, y_pred_split))<jupyter_output>precision    recall  f1-score   support

          0       0.83      0.82      0.82       134
          1       0.73      0.74      0.74        89

avg / total       0.79      0.79      0.79       223

0.789237668161<jupyter_text>Train the Gradient Boosting Forest On Full Training Set<jupyter_code># Create the pipeline with a PCA decomp step
pipeline = Pipeline([
    ('pca', PCA()),
    ('grd', GradientBoostingClassifier(n_estimators=500))
])
# Fit the pipeline to the training set
pipeline.fit(X_train, y_train.values.ravel())<jupyter_output><empty_output><jupyter_text>Predict the Unknowns<jupyter_code>X_score['Survived'] = pipeline.predict(X_score)
X_score[['Survived']].to_csv('submission.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11013320647002854, 'fraction_numerical': 0.015937202664129402, 'mean_word_length': 6.3130434782608695, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '500618', 'license': 'MIT', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1460, 'n_words': 314, 'path': 'titanic-ml.ipynb', 'repo_name': 'DylanGreene/titanic_ml', 'size': '4204'}