In [1]:
from keras.layers import *
from keras.models import *
from keras.optimizers import *
from keras.callbacks import *
import keras
from keras import backend as K


D:\Anaconda3\lib\site-packages\h5py\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.

In [2]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import itertools
%matplotlib inline

logistic regression

data


In [3]:
X = np.random.rand(1000, 2)
Y = np.where(X[:, 0] * X[:, 1] > 0.16, 1, 0)[:, np.newaxis]

In [4]:
plt.scatter(X[:, 0], X[:, 1], c=Y[:, 0])


Out[4]:
<matplotlib.collections.PathCollection at 0x164383c2390>

model


In [15]:
model_x = Input((2, ))
model_y = Dense(1, activation='sigmoid')(model_x)
model = Model(model_x, model_y)

In [16]:
model.compile(
    loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

In [17]:
hist = model.fit(X, Y, batch_size=50, epochs=500, verbose=0)
print(model.evaluate(X, Y, verbose=0))


[0.3667111792564392, 0.886]

predict


In [18]:
pred = model.predict(X) > 0.5
Y_pred = np.where(pred, 1, 0)

In [19]:
cond1 = np.logical_and(Y == 1, Y != Y_pred).flatten()
cond0 = np.logical_and(Y == 0, Y != Y_pred).flatten()

In [20]:
plt.scatter(X[:, 0], X[:, 1], c=Y[:, 0], marker='.')
plt.scatter(X[cond1][:, 0], X[cond1][:, 1], c='r', marker='x')
plt.scatter(X[cond0][:, 0], X[cond0][:, 1], c='g', marker='x')


Out[20]:
<matplotlib.collections.PathCollection at 0x1649db9e780>

contour


In [27]:
px, py = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))
pxy = np.vstack((px.flatten(), py.flatten())).T
pz = model.predict(pxy).reshape(50, 50)
# pz = np.where(pz > 0.5, 1, 0)

In [35]:
plt.contourf(px, py, pz, 1, cmap=plt.cm.binary_r)
# plt.pcolormesh(px, py, pz, cmap=plt.cm.binary_r)
plt.colorbar()
plt.contour(px, py, pz, [0.5], colors='k')
plt.scatter(X[:, 0], X[:, 1], c=Y[:, 0], marker='.')


Out[35]:
<matplotlib.collections.PathCollection at 0x1649f7a49e8>

In [36]:
plt.tricontourf(X[:,0], X[:,1], Y_pred[:,0], 1, cmap=plt.cm.binary_r)
plt.colorbar()
plt.tricontour(X[:,0], X[:,1], Y_pred[:,0], [0.5], colors='k')
plt.scatter(X[:, 0], X[:, 1], c=Y[:, 0], marker='.')


Out[36]:
<matplotlib.collections.PathCollection at 0x1649f8b3160>

polynomial logistic regression

data


In [3]:
X = np.random.rand(1000, 2)
Y = np.where((X[:, 0]-0.5)**2/9 + (X[:, 1]-0.5)**2/6 < 0.01 + np.random.randn(1000)/300, 1, 0)[:, np.newaxis]

In [4]:
plt.scatter(X[:, 0], X[:, 1], c=Y[:, 0])


Out[4]:
<matplotlib.collections.PathCollection at 0x186ccd59b38>

model


In [5]:
def to_polynomial(x, y, n):
    l = []
    for i in range(n+1):
        for j in range(i+1):
            if i==0:
                continue
            l.append(x**(i-j) * y**j)
    return l

In [6]:
model_x = Input((2, ))
model_y = Lambda(lambda x: K.map_fn(lambda y: K.stack(to_polynomial(y[0], y[1], 6)), x))(model_x)
model_y = Dense(1, activation='sigmoid')(model_y)
model = Model(model_x, model_y)

In [7]:
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])

In [8]:
hist = model.fit(X, Y, batch_size=50, epochs=500, verbose=0)

predict


In [9]:
pred = model.predict(X)
Y_pred = np.where(pred>0.5, 1, 0)

In [10]:
cond0 = np.logical_and(Y==0, Y!=Y_pred).flatten()
cond1 = np.logical_and(Y==1, Y!=Y_pred).flatten()

In [11]:
plt.scatter(X[:,0], X[:,1], c=Y[:,0])
plt.scatter(X[cond0][:,0], X[cond0][:,1], c='g', marker='x')
plt.scatter(X[cond1][:,0], X[cond1][:,1], c='r', marker='x')


Out[11]:
<matplotlib.collections.PathCollection at 0x186d3ddde10>

contour


In [12]:
px, py = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))
pxy = np.vstack([px.flatten(), py.flatten()]).T
pz = model.predict(pxy).reshape(50, 50)

In [24]:
plt.contourf(px, py, pz, cmap=plt.cm.binary_r)
plt.colorbar()
plt.contour(px, py, pz, [0.5], colors='k')
plt.scatter(X[:,0], X[:,1], c=Y[:,0], marker='.')


Out[24]:
<matplotlib.collections.PathCollection at 0x1871ee5a9e8>