-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDS101-01-PS1-Titanic-Heuristic-79-percent.py
70 lines (52 loc) · 2.68 KB
/
DS101-01-PS1-Titanic-Heuristic-79-percent.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
import numpy
import pandas
import statsmodels.api as sm
def complex_heuristic(file_path):
"""
You are given a list of Titantic passengers and their associated
information. More information about the data can be seen at the link below:
http://www.kaggle.com/c/titanic-gettingStarted/data
For this exercise, you need to write a more sophisticated heuristic
that will use the passengers' gender and their socioeconomical class and age
to predict if they survived the Titanic diaster.
You prediction should be 79% accurate or higher.
If the passenger is female or if his/her socioeconomic status is high AND
if the passenger is under 18, you should assume the passenger survived.
Otherwise, you should assume the passenger perished in the disaster.
Or more specifically in terms of coding: female or (high status and under 18)
You can access the gender of a passenger via passenger['Sex'].
If the passenger is male, passenger['Sex'] will return a string "male".
If the passenger is female, passenger['Sex'] will return a string "female".
You can access the socioeconomic status of a passenger via passenger['Pclass']:
High socioeconomic status -- passenger['Pclass'] is 1
Medium socioeconomic status -- passenger['Pclass'] is 2
Low socioeconomic status -- passenger['Pclass'] is 3
You can access the age of a passenger via passenger['Age'].
Write your prediction back into the "predictions" dictionary. The
key of the dictionary should be the Passenger's id (which can be accessed
via passenger["PassengerId"]) and the associated value should be 1 if the
passenger survived or 0 otherwise.
For example, if a passenger is predicted to have survived:
passenger_id = passenger['PassengerId']
predictions[passenger_id] = 1
And if a passenger is predicted to have perished in the disaster:
passenger_id = passenger['PassengerId']
predictions[passenger_id] = 0
You can also look at the Titantic data that you will be working with
at the link below:
https://www.dropbox.com/s/r5f9aos8p9ri9sa/titanic_data.csv
"""
predictions = {}
df = pandas.read_csv(file_path)
for passenger_index, passenger in df.iterrows():
if passenger['Sex'] == 'female':
passenger_id = passenger['PassengerId']
predictions[passenger_id] = 1
continue
passenger_id = passenger['PassengerId']
predictions[passenger_id] = 0
# exception to the rule: Child of a higher class
if passenger['Age'] < 18 and passenger['Pclass'] == 1:
predictions[passenger_id] = 1
return predictions
print complex_heuristic(r'Data\titanic_data.csv')