# Perceptron algorithm and its implementation in Python

**Python | Perceptron algorithm**: In this tutorial, we are going to learn about the perceptron learning and its implementation in Python.

Submitted by Anuj Singh, on July 04, 2020

**Perceptron Algorithm** is a classification machine learning algorithm used to linearly classify the given data in two parts. It could be a line in 2D or a plane in 3D. It was firstly introduced in the 1950s and since then it is one of the most popular algorithms for binary classification. Mathematically, it is the simplest algorithm and also has an application in Deep Learning.

This figure shows that data can be classified into two classes by a line. Therefore, this is a linearly separable and perceptron algorithm that can be used for classification. In machine learning language, this line is called the decision boundary. It is defined as: *f( θ.x + θ _{0}) = 0*

The algorithm is based on this decision boundary as it separates the plane into two regions such that:

* f(θ⋅ x + θ₀) > 0*, for positively labelled data and

*, for negatively labelled datapoint.*

**f(θ⋅ x + θ₀) < 0**If the given dataset is linearly separable, then there must exist a linear classifier such that * y⁽ⁱ ⁾ (θ⋅ x⁽ⁱ ⁾ + θ₀) > 0* satisfying all data points

*, where*

**x**^{(i)}*is the label.*

**y⁽ⁱ ⁾**The figure 2 shows a case when the data is not linearly separable and there cannot exist a linear classifier * θ.x + θ_{0} = 0* such that

*satisfying all data points*

**y**^{(i)}(θ⋅ x^{(i)}+ θ₀) > 0*, where*

**x**^{(i)}*is the label. The Perceptron algorithm can be used only for binary classification and it is the simplest one.*

**y**^{(i)}**Note:** If the classifier passes through the origin, i.e. no offset then the value * θ_{0}= 0*.

**f( θ.x ) = 0****Algorithm:**

Initialization 𝛳 = 0, a vector 𝛳_{0}= 0, a scalar Iterate 1,2,3 .... T Iterate through data points i = 1,2,3 .... n if Y^{(i)}.(𝛳.X^{(i)}+ 𝛳0) <= 0 𝛳 = 𝛳 + Y^{(i)}.X^{(i)}𝛳_{0}= 𝛳_{0}+ Y^{(i)}Return 𝛳, 𝛳_{0}

**Note:** Sometime, when all the points are not linearly separable but applying perceptron will result in a classification with some error or can be said as **Loss.**

**Loss function:**

When the linear classifier classifies data points with some points negative classified i.e. * y^{(i)} (θ⋅ x^{(i)} + θ₀) <= 0* (for points

*), then the simplest loss function can be defined as the number of negatively classified points. In other words, the number of*

**i <= n***in the above expression is called a loss function.*

**i****Note:** The loss function can be further modified but this is the simplest one and it does not take into account the error magnitude (i.e. distance of a miss classified point from the classifier).

## Python Function for Perceptron Algorithm

# Machine Learning # Perceptron Algorithm Pyhton function import numpy as np def perceptron_single_step_update( feature_vector, label, current_theta, current_theta_0): theta = current_theta theta_0 = current_theta_0 if label*(np.matmul(current_theta, feature_vector) + current_theta_0) <= 0: theta = theta + label*feature_vector theta_0 = theta_0 + label return (theta, theta_0) def perceptron(feature_matrix, labels, T): [m,n] = np.shape(feature_matrix) tt = np.zeros(n) tt_0 = 0 for t in range(T): for i in range(m): vec = feature_matrix[i] (tt, tt_0) = perceptron_single_step_update(vec, labels[i], tt, tt_0) return (tt, tt_0)

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.