-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitanicSurvivalPrediction.py
118 lines (102 loc) · 3.82 KB
/
TitanicSurvivalPrediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
## Titanic Survival Prediction---Supervised Learning/Decision Tree
# import libraries
import numpy as np
import pandas as pd
# allow use of display
from IPython.display import display
# import supplementary visualizations code
import visuals as vs
# pretty display for notebooks
%matplotlib inline
# load dataset
in_file = 'titanic_data.csv'
full_data = pd.read_csv(in_file)
# print first few entries
display (full_data.head())
# Store the 'Survived' feature in a new variable and remove it from the dataset
outcomes = full_data['Survived']
data = full_data.drop('Survived', axis = 1)
# Show the new dataset with 'Survived' removed
display(data.head())
def accuracy_score(truth, pred):
""" Returns accuracy score for input truth and predictions. """
# Ensure that the number of predictions matches number of outcomes
if len(truth) == len(pred):
# Calculate and return the accuracy as a percent
return "Predictions have an accuracy of {:.2f}%.".format((truth == pred).mean()*100)
else:
return "Number of predictions does not match number of outcomes!"
# Test the 'accuracy_score' function
predictions = pd.Series(np.ones(5, dtype = int))
print accuracy_score(outcomes[:5], predictions)
'''Prediction 0'''
def predictions_0(data):
""" Model with no features. Always predicts a passenger did not survive. """
predictions = []
for passenger in data.iterrows():
# Predict the survival of 'passenger'
predictions.append(0)
# Return our predictions
return pd.Series(predictions)
# Make the predictions
predictions = predictions_0(data)
#check the accuracy
print accuracy_score(outcomes, predictions)
'''visual a feature'''
# Visual the possible factors(dataframe, target, field, [condition])
vs.survival_stats(data, outcomes, 'Sex')
'''prediction 1'''
def predictions_1(data):
""" Model with one feature:
- Predict a passenger survived if they are female. """
predictions = []
for passenger in data.iterrows():
if passenger[1]['Sex'] == 'female':
predictions.append(1)
else:
predictions.append(0)
# Return our predictions
return pd.Series(predictions)
# Make the predictions
predictions = predictions_1(data)
print accuracy_score(outcomes, predictions)
'''vs 2'''
vs.survival_stats(data, outcomes, 'Age', ["Sex == 'male'"])
'''prediction 2'''
def predictions_2(data):
""" Model with two features:
- Predict a passenger survived if they are female.
- Predict a passenger survived if they are male and younger than 10. """
predictions = []
for passenger in data.iterrows():
if passenger[1]['Age']<10 and passenger[1]['Sex']=='male':
predictions.append(1)
elif passenger[1]['Sex']=='female':
predictions.append(1)
else:
predictions.append(0)
# Return our predictions
return pd.Series(predictions)
# Make the predictions
predictions = predictions_2(data)
print accuracy_score(outcomes, predictions)
'''vs 3'''
vs.survival_stats(data, outcomes, 'SibSp', ["Sex == 'female'"])
'''prediction 3'''
def predictions_3(data):
""" Model with multiple features. Makes a prediction with an accuracy of at least 80%. """
predictions = []
for passenger in data.iterrows():
if passenger[1]['Fare'] > 300:
predictions.append(1)
elif passenger[1]['Age']<10 and passenger[1]['Sex']=='male':
predictions.append(1)
elif passenger[1]['Sex']== 'female' and passenger[1]['SibSp']<5:
predictions.append(1)
else:
predictions.append(0)
# Return our predictions
return pd.Series(predictions)
# Make the predictions
predictions = predictions_3(data)
print accuracy_score(outcomes, predictions)