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)

import string

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

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)

cols = ['Pclass','Sex', 'SibSp', 'Parch', 'Embarked', 'CabinPrefix', 'TicketSplitLen', 'CabinKnown']
for col in cols:
    q = df.groupby(col).Survived.sum()
    t = df.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()

letter_map = {}
for i, letter in enumerate(list(string.ascii_lowercase.upper())):
    letter_map[letter] = i + 1
letter_map[None] = -1
df['CabinPrefixInd'] = [letter_map[cabin_prefix] for cabin_prefix in df.CabinPrefix]

from sklearn.model_selection import train_test_split
X = np.array(df[['Pclass', 'Age', 'SibSp', 'Parch', 'Fare', 'TicketSplitLen', 'Sex_Ind', 'CabinPrefixInd']])
y = np.array(df.Survived)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train.shape

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}')