Multiclass classificaton
In [20]:
Copied!
import numpy as np
import keras
from sklearn import datasets
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
# add categorical module for multiclass cross entropy
from keras.utils.np_utils import to_categorical
import numpy as np
import keras
from sklearn import datasets
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
# add categorical module for multiclass cross entropy
from keras.utils.np_utils import to_categorical
In [21]:
Copied!
# numbers of points
n_pts = 500
# coordinators of center of blob cluster
# cluster index or positions: [0, 1, 2, 3, 4] for the output label
# centers = [[-1, 1], [-1, -1], [1, -1], [1, 1], [0, 0]]
centers = [[-1, 1], [-1, -1], [1, -1]]
"""
- make blobs() fun : Generate isotropic Gaussian blobs for clustering.
- n_samples :
- random_state :
- centers : center coordinators position
- cluster_std : distance between points clustered
"""
X, y = datasets.make_blobs(n_samples=n_pts, random_state = 123, centers=centers, cluster_std=0.4)
# print(X)
# print(y)
# plot the dataset cluster of the 3 classes
# plot the data of coordinator index [0] i.e : [-1, 1]
plt.scatter(X[y==0, 0], X[y==0, 1])
# plot the data of coordinator index [1] i.e : [-1, -1]
plt.scatter(X[y==1, 0], X[y==1, 1])
# plot the data of coordinator index [2] i.e : [1, -1]
plt.scatter(X[y==2, 0], X[y==2, 1])
# plot the data of coordinator index [3] i.e : [1, 1]
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plot the data of coordinator index [4] i.e : [0, 0]
# plt.scatter(X[y==4, 0], X[y==4, 1])
# numbers of points
n_pts = 500
# coordinators of center of blob cluster
# cluster index or positions: [0, 1, 2, 3, 4] for the output label
# centers = [[-1, 1], [-1, -1], [1, -1], [1, 1], [0, 0]]
centers = [[-1, 1], [-1, -1], [1, -1]]
"""
- make blobs() fun : Generate isotropic Gaussian blobs for clustering.
- n_samples :
- random_state :
- centers : center coordinators position
- cluster_std : distance between points clustered
"""
X, y = datasets.make_blobs(n_samples=n_pts, random_state = 123, centers=centers, cluster_std=0.4)
# print(X)
# print(y)
# plot the dataset cluster of the 3 classes
# plot the data of coordinator index [0] i.e : [-1, 1]
plt.scatter(X[y==0, 0], X[y==0, 1])
# plot the data of coordinator index [1] i.e : [-1, -1]
plt.scatter(X[y==1, 0], X[y==1, 1])
# plot the data of coordinator index [2] i.e : [1, -1]
plt.scatter(X[y==2, 0], X[y==2, 1])
# plot the data of coordinator index [3] i.e : [1, 1]
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plot the data of coordinator index [4] i.e : [0, 0]
# plt.scatter(X[y==4, 0], X[y==4, 1])
Out[21]:
<matplotlib.collections.PathCollection at 0x7f6ce3691810>
In [22]:
Copied!
# printing actual labels
print(y)
# reformate the label into hot encoding form
"""
to_categorical(y, arg2)
- y : actual label
- arg2 : if empty => 3 classes , others the number of specified classes
"""
y_cat = to_categorical(y, 3)
print(y_cat)
# printing actual labels
print(y)
# reformate the label into hot encoding form
"""
to_categorical(y, arg2)
- y : actual label
- arg2 : if empty => 3 classes , others the number of specified classes
"""
y_cat = to_categorical(y, 3)
print(y_cat)
[2 2 2 0 1 0 0 2 2 2 1 1 0 0 1 2 2 2 2 0 2 0 1 2 1 2 0 0 2 1 1 0 0 2 1 0 2 0 2 0 0 2 1 0 2 0 1 2 0 2 0 1 1 0 1 2 2 0 1 2 0 2 0 2 1 0 1 1 1 1 2 1 1 1 1 1 2 1 1 2 0 1 2 2 1 0 2 1 1 2 0 0 2 1 2 2 0 1 2 0 2 1 1 2 2 0 0 1 2 1 1 2 0 0 1 0 2 2 2 2 1 2 1 2 0 2 2 0 1 0 0 0 0 2 0 1 0 2 2 0 2 2 2 2 1 2 0 0 0 2 1 0 2 0 0 1 1 0 2 0 0 1 1 2 2 0 2 1 2 2 2 1 1 1 2 0 0 1 0 1 2 2 1 1 2 1 1 0 0 2 2 1 0 0 0 0 2 2 0 2 1 1 2 0 0 2 2 0 1 0 2 0 2 2 0 0 2 1 2 0 2 0 1 1 2 0 0 0 2 1 2 2 2 0 0 1 0 2 0 2 0 2 1 1 1 2 2 1 1 1 0 1 2 1 0 1 2 2 0 0 0 1 1 1 2 1 1 2 1 1 2 0 0 0 2 0 2 0 0 0 2 1 1 0 2 1 0 0 1 1 1 1 2 1 1 0 0 0 2 1 1 1 0 0 0 0 0 0 0 1 2 1 2 1 0 2 0 0 1 2 0 1 2 2 2 2 2 0 1 2 0 2 1 0 0 1 2 0 2 0 1 1 0 2 1 0 1 1 1 0 2 0 2 0 1 1 0 1 1 1 2 2 0 2 0 2 2 1 1 0 2 2 2 1 1 1 2 2 0 1 2 1 2 0 1 2 2 2 0 1 1 1 2 0 2 0 0 2 1 1 1 2 0 1 0 0 1 1 2 0 0 1 2 1 1 0 2 0 1 0 2 0 0 0 1 1 1 2 0 1 1 0 2 1 2 1 1 1 1 0 0 0 0 0 2 2 1 1 0 1 0 2 2 2 2 0 0 1 1 0 0 1 1 1 2 2 1 1 1 1 0 0 2 0 1 0 1 0 1 1 2 2 0 2 2 1 0 1 2 0 2 0 2 1 0 2 0 1 1 0] [[0. 0. 1.] [0. 0. 1.] [0. 0. 1.] ... [0. 1. 0.] [0. 1. 0.] [1. 0. 0.]]
In [23]:
Copied!
# create NN model
model = Sequential()
# create the layers with 'softmax' for multiclass classfication
# model.add(Dense(5, input_shape=(2,), activation='softmax'))
# height of the layer : 3 (2 inputs values + 1 bias)
model.add(Dense(3, input_shape=(2,), activation='softmax'))
model.compile(Adam(lr=0.1), 'categorical_crossentropy', metrics=['accuracy'])
# create NN model
model = Sequential()
# create the layers with 'softmax' for multiclass classfication
# model.add(Dense(5, input_shape=(2,), activation='softmax'))
# height of the layer : 3 (2 inputs values + 1 bias)
model.add(Dense(3, input_shape=(2,), activation='softmax'))
model.compile(Adam(lr=0.1), 'categorical_crossentropy', metrics=['accuracy'])
In [24]:
Copied!
# training the labels with hot coding processes
# one hot encode output
history = model.fit(X, y_cat, verbose=1, batch_size = 50, epochs=100)
# training the labels with hot coding processes
# one hot encode output
history = model.fit(X, y_cat, verbose=1, batch_size = 50, epochs=100)
Epoch 1/100 10/10 [==============================] - 0s 1ms/step - loss: 0.9412 - accuracy: 0.6378 Epoch 2/100 10/10 [==============================] - 0s 1ms/step - loss: 0.2560 - accuracy: 0.9814 Epoch 3/100 10/10 [==============================] - 0s 2ms/step - loss: 0.1176 - accuracy: 0.9902 Epoch 4/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0898 - accuracy: 0.9886 Epoch 5/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0661 - accuracy: 0.9933 Epoch 6/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0697 - accuracy: 0.9846 Epoch 7/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0626 - accuracy: 0.9893 Epoch 8/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0538 - accuracy: 0.9897 Epoch 9/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0448 - accuracy: 0.9913 Epoch 10/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0503 - accuracy: 0.9861 Epoch 11/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0483 - accuracy: 0.9899 Epoch 12/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0468 - accuracy: 0.9905 Epoch 13/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0455 - accuracy: 0.9908 Epoch 14/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0463 - accuracy: 0.9908 Epoch 15/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0399 - accuracy: 0.9908 Epoch 16/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0388 - accuracy: 0.9915 Epoch 17/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0369 - accuracy: 0.9907 Epoch 18/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0317 - accuracy: 0.9949 Epoch 19/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0388 - accuracy: 0.9878 Epoch 20/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0369 - accuracy: 0.9901 Epoch 21/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0378 - accuracy: 0.9891 Epoch 22/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0366 - accuracy: 0.9925 Epoch 23/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0332 - accuracy: 0.9916 Epoch 24/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0373 - accuracy: 0.9883 Epoch 25/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0315 - accuracy: 0.9916 Epoch 26/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0367 - accuracy: 0.9885 Epoch 27/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0359 - accuracy: 0.9848 Epoch 28/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0336 - accuracy: 0.9899 Epoch 29/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0423 - accuracy: 0.9847 Epoch 30/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0283 - accuracy: 0.9931 Epoch 31/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0317 - accuracy: 0.9897 Epoch 32/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0234 - accuracy: 0.9946 Epoch 33/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0314 - accuracy: 0.9889 Epoch 34/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0249 - accuracy: 0.9935 Epoch 35/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0241 - accuracy: 0.9947 Epoch 36/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0314 - accuracy: 0.9904 Epoch 37/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0292 - accuracy: 0.9894 Epoch 38/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0307 - accuracy: 0.9896 Epoch 39/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0254 - accuracy: 0.9919 Epoch 40/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0307 - accuracy: 0.9890 Epoch 41/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0386 - accuracy: 0.9876 Epoch 42/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0275 - accuracy: 0.9924 Epoch 43/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0253 - accuracy: 0.9938 Epoch 44/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0212 - accuracy: 0.9930 Epoch 45/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0293 - accuracy: 0.9908 Epoch 46/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0224 - accuracy: 0.9953 Epoch 47/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0316 - accuracy: 0.9863 Epoch 48/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0244 - accuracy: 0.9946 Epoch 49/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0449 - accuracy: 0.9825 Epoch 50/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0300 - accuracy: 0.9896 Epoch 51/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0195 - accuracy: 0.9949 Epoch 52/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0427 - accuracy: 0.9828 Epoch 53/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0159 - accuracy: 0.9969 Epoch 54/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0375 - accuracy: 0.9817 Epoch 55/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0161 - accuracy: 0.9955 Epoch 56/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0398 - accuracy: 0.9857 Epoch 57/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0270 - accuracy: 0.9911 Epoch 58/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0293 - accuracy: 0.9885 Epoch 59/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0266 - accuracy: 0.9895 Epoch 60/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0224 - accuracy: 0.9940 Epoch 61/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0405 - accuracy: 0.9813 Epoch 62/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0211 - accuracy: 0.9928 Epoch 63/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0208 - accuracy: 0.9935 Epoch 64/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0232 - accuracy: 0.9929 Epoch 65/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0201 - accuracy: 0.9949 Epoch 66/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0220 - accuracy: 0.9928 Epoch 67/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0267 - accuracy: 0.9893 Epoch 68/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0177 - accuracy: 0.9948 Epoch 69/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0172 - accuracy: 0.9950 Epoch 70/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0268 - accuracy: 0.9878 Epoch 71/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0203 - accuracy: 0.9942 Epoch 72/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0275 - accuracy: 0.9884 Epoch 73/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0381 - accuracy: 0.9864 Epoch 74/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0241 - accuracy: 0.9885 Epoch 75/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0278 - accuracy: 0.9853 Epoch 76/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0280 - accuracy: 0.9913 Epoch 77/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0236 - accuracy: 0.9931 Epoch 78/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0285 - accuracy: 0.9888 Epoch 79/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0330 - accuracy: 0.9864 Epoch 80/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0291 - accuracy: 0.9879 Epoch 81/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0273 - accuracy: 0.9918 Epoch 82/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0305 - accuracy: 0.9899 Epoch 83/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0338 - accuracy: 0.9869 Epoch 84/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0242 - accuracy: 0.9918 Epoch 85/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0369 - accuracy: 0.9849 Epoch 86/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0247 - accuracy: 0.9891 Epoch 87/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0274 - accuracy: 0.9885 Epoch 88/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0338 - accuracy: 0.9835 Epoch 89/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0217 - accuracy: 0.9915 Epoch 90/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0219 - accuracy: 0.9939 Epoch 91/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0357 - accuracy: 0.9860 Epoch 92/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0185 - accuracy: 0.9939 Epoch 93/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0208 - accuracy: 0.9910 Epoch 94/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0221 - accuracy: 0.9932 Epoch 95/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0234 - accuracy: 0.9907 Epoch 96/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0267 - accuracy: 0.9923 Epoch 97/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0280 - accuracy: 0.9916 Epoch 98/100 10/10 [==============================] - 0s 3ms/step - loss: 0.0256 - accuracy: 0.9882 Epoch 99/100 10/10 [==============================] - 0s 1ms/step - loss: 0.0260 - accuracy: 0.9862 Epoch 100/100 10/10 [==============================] - 0s 2ms/step - loss: 0.0280 - accuracy: 0.9879
In [25]:
Copied!
# ploting decision boundaries
def plot_multiclass_decision_boundary(X, y, model):
x_span = np.linspace(min(X[:,0]) - 1, max(X[:,0]) + 1)
y_span = np.linspace(min(X[:,1]) - 1, max(X[:,1]) + 1)
xx, yy = np.meshgrid(x_span, y_span)
grid = np.c_[xx.ravel(), yy.ravel()]
pred_func = model.predict_classes(grid)
z = pred_func.reshape(xx.shape)
plt.contourf(xx, yy, z)
# ploting decision boundaries
def plot_multiclass_decision_boundary(X, y, model):
x_span = np.linspace(min(X[:,0]) - 1, max(X[:,0]) + 1)
y_span = np.linspace(min(X[:,1]) - 1, max(X[:,1]) + 1)
xx, yy = np.meshgrid(x_span, y_span)
grid = np.c_[xx.ravel(), yy.ravel()]
pred_func = model.predict_classes(grid)
z = pred_func.reshape(xx.shape)
plt.contourf(xx, yy, z)
In [26]:
Copied!
plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plt.scatter(X[y==4, 0], X[y==4, 1])
# adding twice for new point classification
plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plt.scatter(X[y==4, 0], X[y==4, 1])
x = -0.5
y = -0.5
# adding twice for new point classification
point = np.array([[x, y]])
prediction = model.predict_classes(point)
plt.plot([x], [y], marker='o', markersize=10, color="red")
print("Prediction is: ", prediction)
# 0 : violet zone
# 1 : blue
# 2 : yellow
# So model is working great!!!
plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plt.scatter(X[y==4, 0], X[y==4, 1])
# adding twice for new point classification
plot_multiclass_decision_boundary(X, y_cat, model)
plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.scatter(X[y==2, 0], X[y==2, 1])
# plt.scatter(X[y==3, 0], X[y==3, 1])
# plt.scatter(X[y==4, 0], X[y==4, 1])
x = -0.5
y = -0.5
# adding twice for new point classification
point = np.array([[x, y]])
prediction = model.predict_classes(point)
plt.plot([x], [y], marker='o', markersize=10, color="red")
print("Prediction is: ", prediction)
# 0 : violet zone
# 1 : blue
# 2 : yellow
# So model is working great!!!
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py:450: UserWarning: `model.predict_classes()` is deprecated and will be removed after 2021-01-01. Please use instead:* `np.argmax(model.predict(x), axis=-1)`, if your model does multi-class classification (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`, if your model does binary classification (e.g. if it uses a `sigmoid` last-layer activation).
warnings.warn('`model.predict_classes()` is deprecated and '
Prediction is: [1]
In [26]:
Copied!