text_html_output_1.png
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

def prepareFeatuers(df):
    df['CabinPrefix'] = [str(cabinname)[0] for cabinname in df.Cabin]
    df.loc[df.Cabin.isnull(), 'CabinPrefix'] = 'None'
    df['CabinKnown'] = [value for value in df.Cabin.isnull()]
    df['TicketSplitLen'] = [len(t.split()) for t in df.Ticket]
    df['Sex_Ind'] = -1
    df.loc[df.Sex == 'female', 'Sex_Ind'] = 1
    df.loc[df.Sex == 'male', 'Sex_Ind'] = 2
    df['Age'] = df.Age.fillna(0)
    df['Fare'] = df.Fare.fillna(0)
    return df
train = prepareFeatuers(train)
test = prepareFeatuers(test)

cols = ['Pclass','Sex', 'SibSp', 'Parch', 'Embarked', 'CabinPrefix', 'TicketSplitLen', 'CabinKnown']
for col in cols:
    q = train.groupby(col).Survived.sum()
    # 
    t = train.groupby(col).Survived.sum() + train.groupby(col).Survived.count()
    fig, ax = plt.subplots()
    pos = [i for i,name in enumerate(q.index)]
    vals = [name for i,name in enumerate(q.index)]
    ax.barh(pos, t, color='r', label='died')
    ax.barh(pos, q, label='survived')
    ax.set_yticks(pos)
    ax.set_yticklabels(vals)
    ax.set_ylabel(col)
    ax.legend()

for col in ['Pclass', 'SibSp', 'Parch', 'TicketSplitLen', 'Sex', 'CabinPrefix']:
    unique_vals = np.array(train[col].unique())
    unique_vals.sort()
    for unique_value in unique_vals:
        for df in [train, test]:
            df.loc[df[col] == unique_value, f'{col} {unique_value}'] = 1
            df.loc[df[col] != unique_value, f'{col} {unique_value}'] = 0

feature_cols = ['Age', 'Fare', 'Pclass 1', 'Pclass 2', 'Pclass 3', 'SibSp', 'Parch', 'TicketSplitLen 1', 'TicketSplitLen 2', 'TicketSplitLen 3', 'Sex female', 'Sex male', 'CabinPrefix A', 'CabinPrefix B', 'CabinPrefix C', 'CabinPrefix D', 'CabinPrefix E', 'CabinPrefix F', 'CabinPrefix G', 'CabinPrefix None']

from sklearn.model_selection import train_test_split
X = np.array(train[feature_cols])
y = np.array(train.Survived)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train.shape
X_submission = np.array(test[feature_cols])

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
lr_model = LogisticRegression()
lr_model.fit(X_train, y_train)
predictions = lr_model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
precision = precision_score(y_test, predictions)
recall = recall_score(y_test, predictions)
f1 = f1_score(y_test, predictions)
parameters = lr_model.coef_
comparison = pd.DataFrame([['LR', accuracy, precision, recall, f1]], columns=['Model', 'Accuracy', 'Precision', 'Recall', 'F1'])
print(f'Accuracy with LR: {accuracy}')
print(f'Precision with LR: {precision}')
print(f'Recall with LR: {recall}')
print(f'F1 with LR: {f1}')