# 13. Perceptron class in sklearn

## Introduction

In the previous chapter, we had implemented a simple Perceptron class using pure Python. The module sklearn contains a Perceptron class. We saw that a perceptron is an algorithm to solve binary classifier problems. This means that a Perceptron is abinary classifier, which can decide whether or not an input belongs to one or the other class. E.g. "spam" or "ham". We accomplished this by linearly combining weights with the feature vector, i.e. the input. It is amazing that the perceptron algorithm was already invented in the year 1958 by Frank Rosenblatt. The algorithm was implemented in custom-built hardware, called "Mark 1 perceptron". This hardware was designed for image recognition.

The invention has been extremely overestimated: In 1958 the New York Times wrote after a press conference with Rosenblatt: "New Navy Device Learns By Doing; Psychologist Shows Embryo of Computer Designed to Read and Grow Wiser"

What initially seemed very promising was quickly proved incapable of keeping its promises. Thes perceptrons could not be trained to recognise many classes of patterns.

Live Python training

Enjoying this page? We offer live Python training courses covering the content of this site.

Enrol here

## Example: Perceptron Class

We will create with the help of make_blobs a binary testset:

import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

n_samples = 500
data, labels = make_blobs(n_samples=n_samples,
centers=([1.1, 3], [4.5, 6.9]),
cluster_std=1.3,
random_state=0)

colours = ('green', 'orange')
fig, ax = plt.subplots()

for n_class in range(2):
ax.scatter(data[labels==n_class][:, 0],
data[labels==n_class][:, 1],
c=colours[n_class],
s=50,
label=str(n_class)) We will split our testset into a learnset and testset:

from sklearn.model_selection import train_test_split
datasets = train_test_split(data,
labels,
test_size=0.2)

train_data, test_data, train_labels, test_labels = datasets


We will use not the Perceptron class of sklearn.linear_model:

from sklearn.linear_model import Perceptron
p = Perceptron(random_state=42)
p.fit(train_data, train_labels)


### OUTPUT:

Perceptron(random_state=42)Perceptron(random_state=42)


We can calculate predictions on the learnset and testset and can evaluate the score:

from sklearn.metrics import accuracy_score

predictions_train = p.predict(train_data)
predictions_test = p.predict(test_data)
train_score = accuracy_score(predictions_train, train_labels)
print("score on train data: ", train_score)
test_score = accuracy_score(predictions_test, test_labels)
print("score on test data: ", test_score)


### OUTPUT:

score on train data:  0.9125
score on test data:  0.96

p.score(train_data, train_labels)


### OUTPUT:

0.9125


## Classifying the Iris Data with Perceptron Classifier

We want to apply the Perceptron classifier on the iris dataset, which we had already used in our chapter on k-nearest neighbor

import numpy as np


iris.target_names


### OUTPUT:

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')


We split the data into a learn and a testset:

from sklearn.model_selection import train_test_split
datasets = train_test_split(iris.data,
iris.target,
test_size=0.2)

train_data, test_data, train_labels, test_labels = datasets


Now, we create a Perceptron instance and fit the training data:

from sklearn.linear_model import Perceptron
p = Perceptron(random_state=42,
max_iter=30,
tol=0.001)
p.fit(train_data, train_labels)


### OUTPUT:

Perceptron(max_iter=30, random_state=42)Perceptron(max_iter=30, random_state=42)


Now, we are ready for predictions and we will look at some randomly chosen random X values:

import random

sample = random.sample(range(len(train_data)), 10)
for i in sample:
print(i, p.predict([train_data[i]]))


### OUTPUT:

23 
85 
20 
84 
14 
46 
118 
1 
36 
33 

from sklearn.metrics import classification_report

print(classification_report(p.predict(train_data), train_labels))


### OUTPUT:

              precision    recall  f1-score   support

0       1.00      0.48      0.65        75
1       0.05      1.00      0.09         2
2       1.00      0.93      0.96        43

accuracy                           0.65       120
macro avg       0.68      0.80      0.57       120
weighted avg       0.98      0.65      0.75       120

from sklearn.metrics import classification_report

print(classification_report(p.predict(test_data), test_labels))


### OUTPUT:

              precision    recall  f1-score   support

0       1.00      0.74      0.85        19
1       0.17      1.00      0.29         1
2       1.00      1.00      1.00        10

accuracy                           0.83        30
macro avg       0.72      0.91      0.71        30
weighted avg       0.97      0.83      0.88        30


Live Python training

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

Enrol here