We'll be working with a dataset from Kaggle's Titanic competition: data, data dictionary
Goal: Predict survival based on passenger characteristics
The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy shocked the international community and led to better safety regulations for ships.
One of the reasons that the shipwreck led to such loss of life was that there were not enough lifeboats for the passengers and crew. Although there was some element of luck involved in surviving the sinking, some groups of people were more likely to survive than others, such as women, children, and the upper-class.
In this challenge, we ask you to complete the analysis of what sorts of people were likely to survive. In particular, we ask you to apply the tools of machine learning to predict which passengers survived the tragedy.
Read the data into Pandas
In [51]:
import pandas as pd
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/titanic.csv'
titanic = pd.read_csv(url, index_col='PassengerId')
titanic.head()
Out[51]:
Create X and y
Define Pclass and Parch as the features, and Survived as the response.
In [41]:
feature_cols = ['Pclass', 'Parch']
X = titanic[feature_cols]
Y = titanic.Survived
In [42]:
import numpy as np
# Insert code here
random_sample = np.random.rand(y.shape[0])
X_train, X_test = X[random_sample<0.7], X[random_sample>=0.7]
Y_train, Y_test = Y[random_sample<0.7], Y[random_sample>=0.7]
print(Y_train.shape, Y_test.shape)
In [43]:
import numpy as np
from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
print(Y_train.shape, Y_test.shape)
In [44]:
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(C=1e9)
mod1=logreg.fit(X_train, Y_train)
In [45]:
mod1.coef_
Out[45]:
In [47]:
titanic['survive_pred'] = logreg.predict(X)
titanic.head()
Out[47]:
In [52]:
survive_pred = logreg.predict(X_test)
In [53]:
(Y_test == survive_pred).mean()
Out[53]:
In [54]:
from sklearn.metrics import confusion_matrix
confusion_matrix(Y_test, survive_pred)
Out[54]:
In [63]:
survive_pred_prob=logreg.predict_proba(X_test)[:,1]
predict2=np.where(survive_pred_prob >= 0.7, 1, 0)
confusion_matrix(Y_test, predict2)
Out[63]:
In [64]:
(Y_test == predict2).mean()
Out[64]:
In [65]:
survive_pred_prob=logreg.predict_proba(X_test)[:,1]
predict2=np.where(survive_pred_prob >= 0.3, 1, 0)
confusion_matrix(Y_test, predict2)
Out[65]:
In [66]:
(Y_test == predict2).mean()
Out[66]: