-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtitanic.py
More file actions
147 lines (115 loc) · 4.84 KB
/
Copy pathtitanic.py
File metadata and controls
147 lines (115 loc) · 4.84 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_val_score
import pickle
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
# Importing the dataset
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")
# Age categories.
def process_age(df, cut_points, label_names):
df["Age"] = df["Age"].fillna(-0.5)
df["AgeCategory"] = pd.cut(df["Age"], cut_points, labels=label_names)
return df
cut_points = [-1, 0, 5, 12, 18, 35, 60, 100]
label_names = ["Missing", "Infant", "Child", "Teenager", "YoungAdult", "Adult", "Senior"]
train = process_age(train, cut_points, label_names)
test = process_age(test, cut_points, label_names)
# Feature engineering
## Family size.
train["FamilySize"] = train["SibSp"] + train["Parch"] + 1
test["FamilySize"] = test["SibSp"] + test["Parch"] + 1
## Title
def titles_in_name(name: str, titles: list):
for title in titles:
if title in name:
return title
return np.nan
title_list=["Mrs", "Mr", "Master", "Miss", "Major", "Rev",
"Dr", "Ms", "Mlle","Col", "Capt", "Mme", "Countess",
"Don", "Jonkheer"]
train["Title"] = train["Name"].map(lambda x: titles_in_name(x, title_list))
test["Title"] = test["Name"].map(lambda x: titles_in_name(x, title_list))
def categorize_titles(person):
title = person["Title"]
if title in ["Don", "Major", "Capt", "Jonkheer", "Rev", "Col"]:
return "Mr"
elif title in ["Countess", "Mme"]:
return "Mrs"
elif title in ["Mlle", "Ms"]:
return "Miss"
elif title in ["Dr"]:
if person["Sex"] == "Male":
return "Mr"
else:
return "Mrs"
else:
return title
train["Title"] = train.apply(categorize_titles, axis=1)
test["Title"] = test.apply(categorize_titles, axis=1)
# Encode categorical values
def add_encoded_columns(df, column):
dummies = pd.get_dummies(df[column], prefix = column)
df = pd.concat([df, dummies], axis = 1)
return df
categorical_features = ["AgeCategory", "Sex", "Embarked", "Pclass", "Title"]
for feature in categorical_features:
train = add_encoded_columns(train, feature)
test = add_encoded_columns(test, feature)
# Make sure there are no missing values
train["Fare"] = train["Fare"].fillna((train["Fare"].mean()))
test["Fare"] = test["Fare"].fillna((test["Fare"].mean()))
# Prepare train/test set.
columns = ["Fare", "AgeCategory_Infant", "AgeCategory_Child", "AgeCategory_Teenager", "AgeCategory_YoungAdult", "AgeCategory_Adult", "AgeCategory_Senior", "Sex_female", "Sex_male", "Embarked_C", "Embarked_Q", "Embarked_S", "Pclass_1", "Pclass_2", "Pclass_3", "FamilySize", "Title_Mr", "Title_Mrs", "Title_Miss", "Title_Master"]
X_all = train[columns]
y_all = train["Survived"]
# Prepare classifiers.
classifiers = {
"Logistic Regression": LogisticRegression(random_state = 0, solver="lbfgs", max_iter = 10000),
"KNN": KNeighborsClassifier(n_neighbors = 5, metric = "minkowski", p = 2),
"SVM": SVC(kernel = "linear", random_state = 0),
"Kernel SVM": SVC(kernel = "rbf", random_state = 0),
"Gaussian Naive Bayes": GaussianNB(),
"Decision Tree": DecisionTreeClassifier(criterion = "entropy", random_state = 0),
"Random Forest": RandomForestClassifier(criterion = "entropy", n_estimators = 100, random_state = 0),
"Gradient Boost": GradientBoostingClassifier()
}
holdout = test
holdout_predictions = {}
best_accuracy = 0
best_model = None
# Fit, predict and output.
for type, classifier in classifiers.items():
print(f"\n--- {type} ---")
scores = cross_val_score(classifier, X_all, y_all, cv = 10)
accuracy = np.mean(scores)
min = np.min(scores)
max = np.max(scores)
if accuracy > best_accuracy:
best_accuracy = accuracy
best_model = (type, classifier)
print(f"\nAccuracy: {accuracy}\nMin: {min}\nMax: {max}\n")
print(f"Fitting on all data, predicting test data...\n")
classifier.fit(X_all, y_all)
holdout_predictions[type] = classifier.predict(holdout[columns])
holdout_ids = holdout["PassengerId"]
submission_df = {"PassengerId": holdout_ids,
"Survived": holdout_predictions[type]}
submission = pd.DataFrame(submission_df)
submission.to_csv(f"predictions/titanic_{type}.csv", index=False)
print(f"\n...creating models and calculating predictions done.")
print(f"\n\nSaving best model for later use:")
print(f"\n{best_model[0]}")
pickle.dump(
best_model[1],
open(f"model/{best_model[0].lower().replace(' ', '_')}_classifier.model",
"wb")
)