Classification Example with Keras CNN (Conv1D) model in Python

   The convolutional layer learns local patterns of data in convolutional neural networks. It helps to extract the features of input data to provide the output. In this tutorial, we'll learn how to implement a convolutional layer to classify the Iris dataset. We'll use the Conv1D layer of Keras API. The tutorial covers:
  1. Preparing the data
  2. Defining and fitting the model
  3. Predicting and accuracy check
  4. Source code listing
We'll start by loading the required libraries for this tutorial.

from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten, MaxPooling1D
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.datasets import load_iris
from numpy import unique

Preparing the data

We'll use the Iris dataset as a target problem to classify in this tutorial. First, we'll load the dataset and check the x input dimensions.

iris = load_iris()
x, y = iris.data, iris.target
print(x.shape)
(150, 4) 

The next important step is to reshape the x input data. We'll create one-dimensional vectors from each row of x input data.

x = x.reshape(x.shape[0], x.shape[1], 1)
print(x.shape)
(150, 4, 1) 

We'll check the labels of y output data and find out the class numbers that will be defined in a model output layer.

print(unique(y))
[0 1 2] 
print(unique(y).sum())
3

Next, we'll split the data into the train and test parts.

xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)


Defining and fitting the model

We'll define the Keras sequential model and add a one-dimensional convolutional layer. Input shape becomes as it is confirmed above (4,1). We'll add Dense, MaxPooling1D, and Flatten layers into the model. The output layer contains the number of output classes and 'softmax' activation.

model = Sequential()
model.add(Conv1D(64, 2, activation="relu", input_shape=(4,1)))
model.add(Dense(16, activation="relu"))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(3, activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', 
     optimizer = "adam",               
              metrics = ['accuracy'])
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_9 (Conv1D)            (None, 3, 64)             192       
_________________________________________________________________
dense_20 (Dense)             (None, 3, 16)             1040      
_________________________________________________________________
max_pooling1d_9 (MaxPooling1 (None, 1, 16)             0         
_________________________________________________________________
flatten_10 (Flatten)         (None, 16)                0         
_________________________________________________________________
dense_21 (Dense)             (None, 3)                 51        
=================================================================
Total params: 1,283
Trainable params: 1,283
Non-trainable params: 0
_________________________________________________________________ 


Predicting and accuracy check

We'll fit the model with train data then will check the training accuracy.

model.fit(xtrain, ytrain, batch_size=16,epochs=100, verbose=0)

acc = model.evaluate(xtrain, ytrain)
print("Loss:", acc[0], " Accuracy:", acc[1])
Loss: 0.14078762528933877  Accuracy: 0.9448818902331074 

Now we can predict the test data.

pred = model.predict(xtest)
pred_y = pred.argmax(axis=-1)

Finally, we'll check the prediction accuracy with the confusion matrix.

cm = confusion_matrix(ytest, pred_y)
print(cm)
[[5 0 0]
 [0 9 0]
 [0 0 9]] 
 
   In this tutorial, we've briefly learned how to fit and classify the Iris dataset with Keras Conv1D layer model in Python. The full source code is listed below.


Source code listing

from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten, MaxPooling1D
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.datasets import load_iris
from numpy import unique

iris = load_iris()
x, y = iris.data, iris.target
print(x.shape)

x = x.reshape(x.shape[0], x.shape[1], 1)
print(x.shape)

print(unique(y))
print(unique(y).sum())

xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)

model = Sequential()
model.add(Conv1D(64, 2, activation="relu", input_shape=(4,1)))
model.add(Dense(16, activation="relu"))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(3, activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', 
     optimizer = "adam",               
              metrics = ['accuracy'])
model.summary()
model.fit(xtrain, ytrain, batch_size=16,epochs=100, verbose=0)

acc = model.evaluate(xtrain, ytrain)
print("Loss:", acc[0], " Accuracy:", acc[1])

pred = model.predict(xtest)
pred_y = pred.argmax(axis=-1)

cm = confusion_matrix(ytest, pred_y)
print(cm)
 

No comments:

Post a Comment