<jupyter_start><jupyter_text>Kaggle Titanic 1. Setup 1.1 Load the libraries and the dataset<jupyter_code>import numpy as np
import os
import pandas as pd

from IPython.display import display
train_url = "http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/train.csv"
test_url = "http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/test.csv"

train = pd.read_csv(train_url, index_col='PassengerId')
test = pd.read_csv(test_url, index_col='PassengerId')<jupyter_output><empty_output><jupyter_text>1.2 A brief glance at the dataLet's take a look at the data<jupyter_code>pd.DataFrame(
    np.vstack((train.shape, test.shape)),
    columns=['rows', 'columns'],
    index=['Training set', 'Test set']
)
train.head()
test.head()
train.describe()
test.describe()
train.dtypes
test.dtypes
df = train.head()<jupyter_output><empty_output><jupyter_text>1.3 Minor Cleanups 1.3.1 Split Name into First Name & Last Name Columns<jupyter_code>def split_name(df):
    new_df = df['Name'].str.split(',', expand=True)
    new_df.columns=['Last Name', 'First Name']
    df.drop(columns=['Name'], inplace=True)
    return pd.concat((df, new_df), axis=1)
df_train = split_name(train)
df_test = split_name(test)<jupyter_output><empty_output><jupyter_text>1.3.2 One Hot Encoding<jupyter_code>def one_hot_encode(df):
    df['Sex'] = (df['Sex'] == 'female')
    em = pd.get_dummies(df['Embarked']).rename(columns={
        'C': 'Embarked_C',
        'S': 'Embarked_S'
    })
    df = pd.concat((df.drop(columns=['Embarked']), em), axis=1)
    return df
df_train2 = one_hot_encode(df_train)
df_test2 = one_hot_encode(df_test)<jupyter_output><empty_output><jupyter_text>1.3.3 Get X & y<jupyter_code>def get_xy(df, target_col):
    feature_cols = [x for x in df.columns if x != target_col]
    X = df[feature_cols]._get_numeric_data()
    y = df[target_col]
    return X.values, y.values

def get_x(df):
    return df._get_numeric_data().values
X_train, y_train = get_xy(df_train2.dropna(), 'Survived')
X_test = get_x(df_test)<jupyter_output><empty_output><jupyter_text>2. Machine Learning 2.1 Logistic Regression<jupyter_code>from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
lr = model.fit(X_train, y_train)
y_pred = lr.predict(X_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1446733888645331, 'fraction_numerical': 0.011398509425690487, 'mean_word_length': 5.69208211143695, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '332382', 'license': 'MIT', 'n_tokens_mistral': 894, 'n_tokens_neox': 878, 'n_words': 160, 'path': 'machine-learning/titanic.ipynb', 'repo_name': 'nathanielng/python-snippets', 'size': '2281'}