Perceptron Explained Using Python Example - Data Analytics

In this post, you will learn about Perceptrons with the help of a Python example. It is very important for data scientists to understand the concepts related to Perceptron as a good understanding lays the foundation of learning advanced concepts of neural networks including deep neural networks (deep learning).

What Is a Perceptron?

Perceptron is a machine learning algorithm which mimics how a neuron in the brain works. It is also called as single layer neural network, as the output is decided based on the outcome of just one activation function which represents a neuron. 

Let's first understand how a neuron works. The diagram below represents a neuron in the brain. The input signals (x1, x2, ...) of different strength (observe weights, w1, w2 ...) is fed into the neuron cell via dendrites. The net input (weighted sum) is processed by the neuron and output signal (observer signal in AXON) is appropriately fired. In case the combined signal strength is not appropriate based on decision function within neuron cell (observe activation function), the neuron does not fire any output signal.

Fig 1. Neuron in Human Brain
Fig 1. Neuron in Human Brain

The perceptron when represented as a line diagram would look like the following:


Fig 2. Perceptron - Single-layer Neural Network
Fig 2. Perceptron - Single-layer Neural Network


Pay attention to some of the following in relation to what's shown in the above diagram representing a neuron:

Fig 3. Weight update rule of Perceptron learning algorithm
Fig 3. Weight update rule of Perceptron learning algorithm

Pay attention to some of the following in above equation vis-a-vis Perceptron learning algorithm:

Perceptron Python Code Example

In this section, we will look each of the steps described in previous section and understand the implementation with the Python code:

Python


Python


Python


Python


Here is how the entire Python code for Perceptron implementation would look like. This implementation is used to train the binary classification model that could be used to classify the data in one of the binary classes. Pay attention to all the methods that are explained previously. Also, pay attention to the score method which is used to measure the accuracy of the model.

Python


Here is the Python code which could be used to train the model using CustomPerceptron algorithm shown above. Note that SKlean breast cancer data is used for training the model in order to classify / predict the breast cancer.

Python

Conclusions

Here is the summary of what you learned about the Perceptron algorithm with help of Python implementation:

 

 

 

 

Top