Created
November 15, 2018 19:29
-
-
Save mikeful/603d94556cc7b2b3385b97aefc2d1872 to your computer and use it in GitHub Desktop.
Quick neural network model for predicting Titanic survival
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keras | |
tensorflow | |
pandas | |
xlrd | |
h5py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
import numpy | |
model = keras.models.load_model('titanic.hdf') | |
input_data = [1, 30, 1, 70, 1, 0, 0, 0, 1] | |
input_array = numpy.array(input_data).reshape(1, 9) | |
result = model.predict(input_array) | |
print(result[0,0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas | |
import numpy | |
import keras | |
from keras.layers import Dense, Dropout | |
data = pandas.read_excel('titanic3.xls') | |
data['Age'] = data['Age'].fillna(data['Age'].mean()) | |
data['Fare'] = data['Fare'].fillna(data['Fare'].mean()) | |
data['Family_size'] = data['SibSpo'] + data['Parents'] + 1 | |
data['Fare_per_person'] = data['Fare'] / data['Family_size'] | |
training_data = [] | |
target_data = [] | |
for row in data.itertuples(): | |
item = [] | |
item.append(row.Pclass) | |
item.append(row.Age) | |
item.append(row.Family_size) | |
item.append(row.Fare_per_person) | |
item.append(row.Embarked=='Q') | |
item.append(row.Embarked=='S') | |
item.append(row.Embarked=='C') | |
item.append(row.Sex=='male') | |
item.append(row.Sex=='female') | |
training_data.append(item) | |
target_data.append([row.Survived]) | |
training_data_in = numpy.array(training_data) | |
target_data_in = numpy.array(target_data) | |
model = keras.Sequential() | |
model.add(Dense(15, activation='relu', input_dim=9)) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10, activation='relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10, activation='relu')) | |
model.add(Dense(1, activation='sigmoid')) | |
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=['accuracy']) | |
model.summary() | |
model.fit(training_data_in, target_data_in, epochs=100, batch_size=200, validation_split=0.2) | |
model.save('titanic.hdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment