The tutorial cover:
- Preparing the data
- Building the model
- Accuracy check
- Source code listing
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix from keras.models import Sequential from keras.layers import Dense
Preparing the data
We'll use the iris dataset as a target classification data. First, we load the dataset, then separate its feature (x) and label (y) parts, and split it into the train and test parts.
iris = load_iris() x, y = iris.data, iris.target xtrain, xtest, ytrain, ytest = train_test_split(x, y, random_state=1)
Building the model
Next, we build a keras sequential model and fit it with train data. The model is defined by the Keras Sequential() class.
model = Sequential()
Then, we'll add a Dense layer defining the input dimension. We can find out the data dimension by checking its shape. We'll set 4 into the input_dim parameter. Activation will be a relu.
print(xtrain.shape)
(112, 4)
model.add(Dense(32, input_dim = 4, activation = 'relu'))
We can also add a dropout layer to ignore 20 percent of neurons after the input layer.
model.add(Dropout(0.2))
We'll add one more Dense layer.
model.add(Dense(8, activation = 'relu'))
The final Dense layer identifies the output class number. In iris data, three classes are available, thus we set 3 and activation will be "softmax" type.
model.add(Dense(3, activation = 'softmax'))
We can compile the model with the below parameters.
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = "adam", metrics = ['accuracy'])
print(model.summary())
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_22 (Dense) (None, 32) 160 _________________________________________________________________ dropout_3 (Dropout) (None, 32) 0 _________________________________________________________________ dense_23 (Dense) (None, 8) 264 _________________________________________________________________ dense_24 (Dense) (None, 3) 27 ================================================================= Total params: 451 Trainable params: 451 Non-trainable params: 0 _________________________________________________________________
Finally, we'll fit the model with train data.
model.fit(xtrain, ytrain, epochs = 40, batch_size = 16)
After the training of the model, we can evaluate the model accuracy.
acc = model.evaluate(xtrain,ytrain) print("Loss:", acc[1], " Accuracy:", acc[1:2])
Loss: 0.9196428571428571 Accuracy: [0.9196428571428571]
Accuracy check
The model is ready, now we can predict the test data.
pred = model.predict(xtest)
To transfer pred data into the class id list, we'll use the below function.
pred_y = pred.argmax(axis=-1)
Finally, we'll check the confusion matrix table.
cm = confusion_matrix(ytest, pred_y) print(cm)
[[13 0 0] [ 0 12 4] [ 0 0 9]]
In this tutorial, we have briefly learned how to build Keras model to classify data in Python. The full source code is listed below. Thank you for reading!
Source code listing
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix from keras.models import Sequential from keras.layers import Dense, Dropout iris = load_iris() x, y = iris.data, iris.target xtrain, xtest, ytrain, ytest = train_test_split(x, y, random_state=1) print(xtrain.shape) model = Sequential() model.add(Dense(32, input_dim = 4, activation = 'relu')) model.add(Dropout(0.2)) model.add(Dense(8, activation = 'relu')) model.add(Dense(3, activation = 'softmax')) model.compile(loss = 'sparse_categorical_crossentropy', optimizer = "adam", metrics = ['accuracy']) print(model.summary()) model.fit(xtrain, ytrain, epochs = 40, batch_size = 16) acc = model.evaluate(xtrain,ytrain) print("Loss:", acc[1], " Accuracy:", acc[1:2]) pred = model.predict(xtest) pred_y = pred.argmax(axis=-1) cm = confusion_matrix(ytest, pred_y) print(cm)
No comments:
Post a Comment