Last active
January 14, 2017 17:24
-
-
Save clausd/d36e943ab4626c6e1c15186ed587a406 to your computer and use it in GitHub Desktop.
Just the code from Keras Helloworld
This file contains 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 | |
import seaborn as sns | |
import numpy as np | |
from sklearn.cross_validation import train_test_split | |
from sklearn.linear_model import LogisticRegressionCV | |
from keras.models import Sequential | |
from keras.layers.core import Dense, Activation | |
from keras.utils import np_utils | |
iris = sns.load_dataset("iris") | |
iris.head() | |
sns.pairplot(iris, hue='species'); | |
X = iris.values[:, :4] | |
y = iris.values[:, 4] | |
train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.5, random_state=0) | |
lr = LogisticRegressionCV() | |
lr.fit(train_X, train_y) | |
print("Accuracy = {:.2f}".format(lr.score(test_X, test_y))) | |
def one_hot_encode_object_array(arr): | |
'''One hot encode a numpy array of objects (e.g. strings)''' | |
uniques, ids = np.unique(arr, return_inverse=True) | |
return np_utils.to_categorical(ids, len(uniques)) | |
train_y_ohe = one_hot_encode_object_array(train_y) | |
test_y_ohe = one_hot_encode_object_array(test_y) | |
model = Sequential() | |
model.add(Dense(16, input_shape=(4,))) | |
model.add(Activation('sigmoid')) | |
model.add(Dense(3)) | |
model.add(Activation('softmax')) | |
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"]) | |
time model.fit(train_X, train_y_ohe, nb_epoch=100, batch_size=1, verbose=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment