Implementing Multiclass Classification using Perceptron
In this example, our goal is to create a model based on pixel values that can identify handwritten digits (0-9) in text. The MNIST dataset is a well-known dataset for this job, which is a classic machine learning problem called handwritten digit recognition.
Step 1 : Importing necessary libraries
We start by loading the necessary libraries, including the MNIST dataset from sklearn.datasets and Scikit-Learn for machine learning operations.
Python3
from sklearn.datasets import fetch_openml from sklearn.linear_model import Perceptron from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score import seaborn as sns |
Step 2 : Loading dataset
Using the fetch_openml method from Scikit-Learn, we import the MNIST dataset. The MNIST dataset comprises of handwritten digits (0–9) and their related labels in 28×28 pixel pictures.
Python3
# Load the MNIST dataset mnist = fetch_openml( 'mnist_784' ) X = mnist.data y = mnist.target.astype( int ) print (X.shape) print (y.shape) |
Output:
(70000, 784)
(70000,)
The dataset is loaded as a dictionary-like object in which the digit labels for each image’s pixels are located in mnist.target and its pixel values are stored in mnist.data.
Step 3: Splitting the Dataset into Test and Training set
Utilizing the train_test_split function of Scikit-Learn, we divided the dataset into training and testing sets. In this instance, 30% of the data are set aside for testing.
Python3
# Split the dataset into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3 , random_state = 42 ) |
Step 4: Creating Perceptron Model
Using the Perceptron class from Scikit-Learn, we build a Perceptron model. We give the training’s maximum iteration count (max_iter) as well as the learning rate (eta0).
Python3
# Create a Perceptron model perceptron = Perceptron(max_iter = 1000 , eta0 = 0.1 ) |
Step 4: Model Training
Using the training set of data, we train the Perceptron model. Based on the pixel values of the images, the model develops the ability to differentiate between several digit classes.
Python3
# Train the model perceptron.fit(X_train, y_train) |
Step 5: Prediction
In order to forecast the digit labels for the test images, we use the trained Perceptron model to make predictions on the testing data after training.
Python3
# Make predictions y_pred = perceptron.predict(X_test) print (y_pred) |
Output:
['8' '4' '8' ... '2' '9' '5']
Step 6: Evaluation
We determine the accuracy of the model’s predictions to evaluate its performance.
Python3
# Evaluate accuracy accuracy = accuracy_score(y_test, y_pred) print ( "Accuracy:" , accuracy) |
Output:
Accuracy: 0.87
Step 7: Confusion Matrix
Python3
# Calculate confusion matrix conf_matrix = confusion_matrix(y_test, y_pred) sns.heatmap(conf_matrix, annot = True , cmap = 'Blues' ) plt.ylabel( 'True' ) plt.xlabel( 'False' ) plt.title( 'Confusion Matrix' ) plt.show() |
Output:
We calculate the confusion matrix using confusion_matrix(y_test, y_pred), where y_test contains the true labels, and y_pred contains the predicted labels. We use matplotlib and seaborn to create a heatmap visualization of the confusion matrix, making it easier to interpret. The heatmap is annotated with the actual numerical values from the confusion matrix. The x-axis represents the predicted labels, and the y-axis represents the true labels.
Perceptron Algorithm for Classification using Sklearn
Assigning a label or category to an input based on its features is the fundamental task of classification in machine learning. One of the earliest and most straightforward machine learning techniques for binary classification is the perceptron. It serves as the framework for more sophisticated neural networks. This post will examine how to use Scikit-Learn, a well-known Python machine-learning toolkit, to conduct binary classification using the Perceptron algorithm.
Contact Us