Implementation
For our implementation part of using the perceptron for binary classification, we will be using the the Iris flower dataset. Our goal over here is to classify the Iris flowers into two categories: Setosa and Versicolor. For this purpose we will be using Python as our programming language and Scikit-Learn to implement and train the perceptron.
Import necessary libraries
Python3
from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import Perceptron from sklearn.metrics import accuracy_score |
- load_iris is used to load the built-in Iris dataset.
- train_test_split to split the dataset into training and testing sets.
- Perceptron is to create our perceptron.
- accuracy_score is used to calculate the accuracy of our classifier.
Load the Iris dataset
Python3
#loading dataset data = load_iris() |
Split the dataset into training and testing sets
Python3
#Splitting the dataset to train data and test data X, y = data.data[: 100 , :], data.target[: 100 ] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2 , random_state = 42 ) |
This line of code extracts the first 100 samples (rows) from a dataset named data, along with the matching labels named data.target. Using train_test_split from Scikit-Learn and a fixed random seed (random_state) for reproducibility, it divides these samples and labels into training (80%) and testing (20%) sets.
Create a perceptron classifier
Python3
#Making a perceptron classifier perceptron = Perceptron(max_iter = 100 , eta0 = 0.1 , random_state = 42 ) perceptron.fit(X_train, y_train) |
This code starts a Perceptron classifier with a maximum of 100 iterations, a fixed random seed for reproducibility (random_state), and an initial learning rate (eta0) of 0.1. Following that, it applies the fit method to fit the classifier to the training data (X_train, y_train).
Make predictions on the test data and calculate the accuracy
Python3
#Making prediction on test data y_pred = perceptron.predict(X_test) #Finding accuracy accuracy = accuracy_score(y_test, y_pred) print (f 'Accuracy: {accuracy}' ) |
Output:
Accuracy: 1.0
This code predicts labels for the test data X_test using the trained Perceptron classifier (perceptron), and it keeps those predictions in the variable y_pred. It then computes the predictions’ accuracy by comparing them to the actual labels in the y_test dataset, and publishes the accuracy score.
Classification Report
Python3
# Generate a classification report class_report = classification_report(y_test, y_pred) print ( "Classification Report:\n" , class_report) |
Output:
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 12
1 1.00 1.00 1.00 8
accuracy 1.00 20
macro avg 1.00 1.00 1.00 20
weighted avg 1.00 1.00 1.00 20
This code compares the actual labels (y_test) and predicted labels (y_pred) to provide a classification report that includes multiple classification metrics such as precision, recall, and F1-score. The report provides a thorough assessment of the model’s functionality using the test set of data.
Perceptron class in Sklearn
Machine learning is a prominent technology in this modern world and as years go by it is growing immensely. There are several components involved in Machine Learning that make it evolve and solve various problems and one such crucial component that exists is the Perceptron. In this article, we will be learning about what a perceptron is, the history of perceptron, and how one can use the same with the help of the Scikit-Learn, library which is arguably one of the most popular machine learning libraries in Python.
Frank Rosenblatt led the development of perceptron in the late 1950s. It is said that this was one of the earliest supervised learning algorithms that did exist. The primary reason behind developing a perceptron was to classify the given data into two categories. So we are confident enough to claim that a perceptron is a type of artificial neural network, that is actually based on real-life biological neurons which in turn makes it a binary classifier.
Table of Content
- Understanding Perceptron
- Concepts Related to the Perceptron
- Mathematical Foundation
- Parameters
- Variants of the Perceptron Algorithm
- Implementation
- Advantages
- Disadvantages
- Conclusion