Wednesday, 6 May 2026

The Perceptron - classifying items into two classes based on numerical input variables

I've been reading Sebastian Raschka's excellent book Machine Learning with PyTorch and Scikit-Learn about machine learning/AI using Python.

In the book Raschka talks about the Perceptron, a historically important AI algorithm for classifying individuals into two classes based on an arbitrary number of numerical input variables. The Perceptron is essentially an optimisation algorithm that iteratively adjusts weights in order to minimise classification errors for the individuals.  

The Perceptron is a simple neural network, a single-layer neural network. It can also be thought of as a linear classifier, as it calculates a linear combination of input features and weights to make predictions of one of two classes, that is, for binary classification.

Raschka taught me how to use the Perceptron to classify individual Iris flowers into two species, Iris setosa and Iris versicolor, based on their sepal length (in cm) and petal length (in cm):

1. First, I installed the necessary Python packages (numpy, scipy,  scikit-learn, matplotlib, and pandas): see https://avrilomics.blogspot.com/2026/04/installing-python-libraries-within.html

2. Then I downloaded Rachka's Python scripts https://github.com/rasbt/machine-learning-book/blob/main/python_environment_check.py and https://github.com/rasbt/machine-learning-book/blob/main/ch02/ch02.py

2. Then I started up python3 and loaded in Raschka's Python scripts, and some other necessary Python modules:
% python3
>>> import python_environment_check
>>> import chap02
>>> import os
>>> import pandas as pd 
>>> import matplotlib.pyplot as plt
>>> import numpy as np

3. Then I loaded in the Iris data set into a data frame 'df', and take a subset of the Iris data set corresponding to the 100 individual flowers belonging to either the Iris setosa and Iris versicolor species, putting the species names for the individuals into variable y (0=setosa, 1=versicolor) and their sepal lengths and petal lengths into variable X

>>> s = 'https://archive.ics.uci.edu/ml/'\
... 'machine-learning-databases/iris/iris.data'
>>> df = pd.read_csv(s,header=None,encoding='utf-8')
>>> y = df.iloc[0:100,4].values # These are the species names
>>> y = np.where(y == 'Iris-setosa', 0, 1)
>>> X = df.iloc[0:100, [0,2]].values

4. Then I trained the Perceptron algorithm on this subset of the Iris data set, using Raschka's code for the Perceptron algorithm (in Raschka's ch02.py Python script, downloaded above):
>>> ppn = chap02.Perceptron(eta=0.1, n_iter=10)
>>> ppn.fit(X, y)

5. Now I can use the Perceptron algorithm to classify a totally new flower just collected from the wild (which we assume is either Iris versicolor or Iris setosa) as either Iris versicolor/Iris setosa based on its sepal length and petal length. Say for example we have a flower with sepal length of 5.0 cm and petal length of 1.0 cm, let's classify it using the trained Perceptron algorithm:
>>> ppn.predict((5.0,1.0))
array(0)
The Perceptron has classified it as class 0, which means it has classified it as Iris setosa.
Say we have a second flower with a sepal length of 5.0 cm and a petal length of 3.0 cm, let's classify it too:
>>> ppn.predict((5.0,3.0))
array(1)
The Perceptron has classified it as class 1, which means it has classified it as Iris versicolor.

Yay! Thank you Dr Raschka.

Some caveats to know about the Perceptron algorithm are:
- it just works for two classes, not multiple classes, so for example can't predict a third species of Iris.
- it can be trained (and so the training algorithm will converge) if the two classes (Iris versicolor or Iris setosa here) can be separated by a linear hyperplane. 



No comments: