Implementing Binary Classification using Perceptron
Letâs consider the few examples to understand the classification using Sklearn. In this example, we identify tumors as malignant or benign using the Breast Cancer Wisconsin dataset and a variety of characteristics, including mean radius, mean texture, and others.
Step 1: Importing necessary libraries
Python3
from sklearn.datasets import load_breast_cancer from sklearn.linear_model import Perceptron from sklearn.model_selection import train_test_split from sklearn import metrics from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix |
Step 2: Importing Breast Cancer Dataset
Using Scikit-Learnâs load_breast_cancer function, we load the Breast Cancer dataset. The features of breast cancer tumors are included in this dataset, along with the labels that correlate to them (0 for malignant and 1 for benign).
Python3
# Load the Breast Cancer dataset data = load_breast_cancer() X = data.data y = data.target print (X.shape) print (y.shape) |
Output:
(569, 30)
(569,)
Step3: Splitting the dataset in train and test set.
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 ) print (X_train.shape) print (X_test.shape) print (y_train.shape) print (y_test.shape) |
Output:
(398, 30)
(171, 30)
(398,)
(171,)
Step 4: Creating Perceptron
Python3
# Create a Perceptron model clf = Perceptron(max_iter = 1000 , eta0 = 0.1 ) |
Here max_iter defines the number of times the madel are used to train on the same dataset. and eta0 is the learning parameter.
Step 5 : Training the 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
# Train the model clf.fit(X_train, y_train) |
Step 6: Prediction
For binary classification of tumour, we use the trained Perceptron model to make predictions on the testing data after training.
Python3
# Make predictions y_pred = clf.predict(X_test) print (y_pred) |
Output:
[0 0 0 1 1 0 0 0 1 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0
1 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 0 1 1 0 0 1 1
1 1 1 0 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 0 0
1 1 0 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0
0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1]
Step 7: Evaluation
We assess the modelâs accuracy in identifying malignant or benign tumors.
Python3
# Evaluate accuracy accuracy = accuracy_score(y_test, y_pred) print ( "Accuracy:" , accuracy) |
Output:
Accuracy: 0.935672514619883
Step 8: Confusion Matrix
Python3
# Calculate confusion matrix conf_matrix = confusion_matrix(y_test, y_pred) cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = conf_matrix, display_labels = [ False , True ]) cm_display.plot() plt.show() |
Output:
Confusion Matrix shows the performance of the model where True negatives, false positives, false negatives, True positive values respectively.
Beyond Binary Classification:
While this article has mostly focused on binary classification, it is important to highlight that the Perceptron may also be extended to multiclass classification problems. It distributes instances to one of two classes in binary classification, but in multiclass classification, it may be altered to discriminate between several classes. This adaptation is frequently accomplished using tactics such as one-vs-all (OvA) or one-vs-one (OvO) strategies.
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