Skip to content

Instantly share code, notes, and snippets.

@auser
Last active April 16, 2020 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save auser/be59271599524815a5c1b9b0ec6487df to your computer and use it in GitHub Desktop.
Save auser/be59271599524815a5c1b9b0ec6487df to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
model = Sequential()
model.add(Dense(10, input_dim=X_train.shape[1], activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=Adam(lr=0.01),
loss='binary_crossentropy',
metrics=['accuracy'])
h = model.fit(X_train, y_train, verbose=0, epochs=200, shuffle=True)
plt.plot(h.history['loss'])
results = model.evaluate(X_test, y_test, verbose=0)
print("The Accuracy score on the Test set is:\t{:0.3f}".format(results[1]))
# train comes from the titantic dataset provided by
# kaggle (https://www.kaggle.com/c/titanic/data)
df = pd.read_csv('./train.csv')
def preprocess(raw_data):
# Preprocess data
# Convert to binary fields
dummy_fields = ['Pclass', 'Embarked', 'Sex']
dummies = pd.get_dummies(raw_data[dummy_fields])
data = pd.concat([raw_data, dummies], axis=1)
# drop other fields
fields_to_drop = ['PassengerId', 'Ticket', 'Parch',
'Name', 'Cabin', 'Fare', 'Pclass',
'Embarked', 'Sex', 'Sex_male']
data = data.drop(fields_to_drop, axis=1)
mean, std = data['Age'].mean(), data['Age'].std()
data.loc[:, 'Age'] = (data['Age'] - mean) / std
data = data.fillna(0)
data = data.sample(frac=1).reset_index(drop=True)
X = data.drop('Survived', axis=1).values
y = data[['Survived']].values
return X, y
train = df.sample(frac=0.8, random_state=200)
test = df.drop(train.index)
X_train, y_train = preprocess(train)
X_test, y_test = preprocess(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment