-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_heuristic.py
More file actions
76 lines (59 loc) · 3.03 KB
/
complex_heuristic.py
File metadata and controls
76 lines (59 loc) · 3.03 KB
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
import numpy as np
import pandas as pd
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 algorithm
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.
Here's the algorithm, predict the passenger survived if:
1) If the passenger is female or
2) if his/her socioeconomic status is high AND if the passenger is under 18
Otherwise, your algorithm should predict that 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://s3.amazonaws.com/content.udacity-data.com/courses/ud359/titanic_data.csv
'''
predictions = []
df.column = ["PassengerId", "Survived", "Pclass", "Name", "Sex", "Age", "SibSp", "Parch", "Ticket", "Fare", "Cabin",
"Embarked"]
for passenger_index, passenger in df.iterrows():
if (passenger.Sex == "female" or (passenger.Pclass==1 and passenger.Age<18)):
passenger_id = passenger['PassengerId']
associated_value = 1
predictions.append([passenger_id, associated_value])
# your code here
# for example, assuming that passengers who are male
# and older than 18 surived:
# if passenger['Sex'] == 'male' or passenger['Age'] < 18:
# predictions[passenger_id] = 1
#
return predictions
if __name__ == '__main__':
df = pd.read_csv('titanic_data.csv')
predictions = complex_heuristic(df)
print(predictions)