In [ ]:
%matplotlib inline

In [ ]:
import numpy as np
import pandas as pd
import sklearn
from ailib.ml.datasets.binary_classification_toy_problems import gen_2d_samples

In [ ]:
from sklearn import datasets

Keras Toy 2D binary classification

Install Keras

https://keras.io/#installation

Install dependencies

Install TensorFlow backend: https://www.tensorflow.org/install/

pip install tensorflow

Insall h5py (required if you plan on saving Keras models to disk): http://docs.h5py.org/en/latest/build.html#wheels

pip install h5py

Install pydot (used by visualization utilities to plot model graphs): https://github.com/pydot/pydot#installation

pip install pydot

Install Keras

pip install keras

Import packages and check versions


In [ ]:
import tensorflow as tf
tf.__version__

In [ ]:
import keras
keras.__version__

In [ ]:
import h5py
h5py.__version__

In [ ]:
import pydot
pydot.__version__

Make the dataset


In [ ]:
df_train = gen_2d_samples(n_samples=200)

x_train = df_train[['x1', 'x2']].values
y_train = df_train.y.values

ax = df_train.loc[df_train.y == 0].plot.scatter(x='x1', y='x2', color="r")
df_train.loc[df_train.y == 1].plot.scatter(x='x1', y='x2', ax=ax);

In [ ]:
df_test = gen_2d_samples(n_samples=200)

x_test = df_test[['x1', 'x2']].values
y_test = df_test.y.values

ax = df_test.loc[df_test.y == 0].plot.scatter(x='x1', y='x2', color="r")
df_test.loc[df_test.y == 1].plot.scatter(x='x1', y='x2', ax=ax);

Make the classifier


In [ ]:
model = keras.models.Sequential()

model.add(keras.layers.Dense(units=2, activation='relu', input_dim=2))
model.add(keras.layers.Dense(units=1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.summary()

In [ ]:
print(model.get_config())

In [ ]:
hist = model.fit(x_train, y_train, batch_size=100, epochs=10, verbose=False)

In [ ]:
plt.plot(hist.history['loss']);

In [ ]:
model.evaluate(x_test, y_test)

In [ ]:
y_predicted = model.predict(x_test)

In [ ]:
predicted_data = np.concatenate([x_test, y_predicted], axis=1)
predicted_df = pd.DataFrame(predicted_data, columns=['x1', 'x2', 'y'])

ax = predicted_df.loc[predicted_df.y <= 0.5].plot.scatter(x='x1', y='x2', color="r")
predicted_df.loc[predicted_df.y > 0.5].plot.scatter(x='x1', y='x2', ax=ax);

Bonnus: plot the regressor


In [ ]:
from keras.utils import plot_model

plot_model(model, show_shapes=True, to_file="model.png")