-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
68 lines (55 loc) · 2.38 KB
/
script.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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
passengers = pd.read_csv('passengers.csv')
passengers['Sex'] = passengers['Sex'].map({'female': 1, 'male': 0})
passengers['Age'].fillna(inplace=True, value=round(passengers['Age'].mean()))
passengers['FirstClass'] = passengers['Pclass'].apply(lambda x: 1 if x == 1 else 0)
passengers['SecondClass'] = passengers['Pclass'].apply(lambda x: 1 if x == 2 else 0)
features = passengers[['Sex', 'Age', 'FirstClass', 'SecondClass']]
survival = passengers['Survived']
train_features, test_features, train_labels, test_labels = train_test_split(features, survival)
scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)
test_features = scaler.transform(test_features)
def find_gender():
gender_input = input("What is your gender? ")
if gender_input in ["Male", "male", "Man", "man", "Boy", "boy", "m", "M"]:
return 0.0
elif gender_input in ["Female", "female", "Woman", "woman", "Girl", "girl", "f", "F"]:
return 1.0
else:
print("Invalid input. Please try again.")
def find_class():
class_input = input("""
Which class would you have been in on the Titanic?
A: First Class
B: Second Class
C: Third Class
""")
if class_input in ["A", "a"]:
return (1.0, 0.0)
if class_input in ["B", "b"]:
return (0.0, 1.0)
if class_input in ["C", "c"]:
return (0.0, 0.0)
else:
print("Invalid input. Please try again.")
Jack = np.array([0.0, 20.0, 0.0, 0.0])
Rose = np.array([1.0, 17.0, 1.0, 0.0])
if __name__ == "__main__":
model = LogisticRegression()
model.fit(train_features, train_labels)
your_age = input("What is your age? ")
your_gender = find_gender()
your_class = find_class()
You = np.array([your_gender, float(your_age), your_class[0], your_class[1]])
new_passengers = np.array([Jack, Rose, You])
new_passengers_transformed = scaler.transform(new_passengers)
if model.predict(new_passengers_transformed)[2] == 0:
print("Rose survived, but unfortunately you and Jack did not.")
if model.predict(new_passengers_transformed)[2] == 1:
print("Jack did not survive, but fortunately you and Rose lived!")