Last active
May 29, 2018 00:14
-
-
Save ghego/921294d4d9a92e1d41f42a088ca0f0ba to your computer and use it in GitHub Desktop.
keras_model.py
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 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])) |
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 numpy as np | |
| import pandas as pd | |
| np.random.seed(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