How to implement Perceptron from scratch with Python

AssemblyAI · Intermediate ·📐 ML Fundamentals ·3y ago

Key Takeaways

The video demonstrates how to implement the Perceptron algorithm from scratch using Python, covering the Perceptron update rule, weights, and bias, and providing a practical example with code using numpy, matplotlib, and sklearn.

Full Transcript

welcome to another video of the machine learning from scratch course presented by assembly ai in this series we implement popular machine learning algorithms using only built in python functions and numpy in this lesson we learn about the perceptron algorithm so we start with a short theory section and then we jump to the code so let's get started so the perceptron is an algorithm for supervised learning of binary classifiers it can be seen as a single unit of an artificial neural network and is also known as the prototype for neural nets so if we use only a single unit then we say this is a single layer perceptron and this can learn only linearly separable patterns on the other hand if you use a multi-layer perceptron then it can learn more complex patterns but in this lesson we focus on the single layer perceptron and this is inspired by neurons so it's a simplified model of a biological neuron and it simulates the behavior of one cell so let's say here we have our neuron and then it gets an input signal and then the signal travels along the way and if it reaches a certain threshold then this cell will activate so we say that the cell fires and then it gives an output signal so if we transfer this to our mathematical representation then we say it's a single layer neural network with the unit step function as activation function so we have the inputs and then we multiply this with some weights then we sum this up and then we have the actuation function and then we have the output and in this case the activation function is either one if the cell fires or zero if it doesn't fire and in our case one is the class label one and zero is the class label zero so now let's put this into a mathematical model so here we have the inputs times the weights so we have a simple linear model we approximate f of x with w times x plus a bias speed so these are the weights times x plus the bias b and then we also want the actuation function and in this case this is the unit step function which is very simple so this is one if the input reaches a third and threshold and zero otherwise and this is the whole concept so with this we approximate the class label with the linear model w times x plus b and then apply the actuation function the unit step function and now we need to learn the weights w and the bias b and we do this with the perceptron update rule and this is a super cool intuitive rule i will show you what i mean in a moment so for each training sample x i we say we update the weights we say the weights plus delta w and for the buyers the same the bias plus delta bias so now what is the delta so for the w it's alpha times y i minus y hat i times x i and for the bias it's the same except this part so and alpha is the learning rate so this is between 0 and 1 and this basically decides how far we go into this direction and then y i is the actual label and y hat i is the approximation um with this formula so let's explain what this part here means let's look at the different cases so we have the class labels one and zero so our binary classifier so if both y and the approximation y hat are one then this is correct right and then y minus y hat is zero so there is no change and the same if the um actual label is zero and the prediction is zero so again we have the correct prediction then we have y minus y hat is zero so again no change and if our actual class is one but the prediction is zero then the difference is one so this means that the um the prediction is too low so we need to increase the weights and the other way around if the label is zero and the prediction is one then this means that the prediction is too high so that the weights are too high and we need to decrease this so basically the weights are pushed towards the positive or negative target class in case of a misclassification and if the classification is correct then we don't need to update the weights and this is the beautiful perceptron update rule a very intuitive model and this is all that we need so let's summarize the steps so in the training part we want to learn the weight so we initialize the weights and then for each sample we calculate the approximation with the linear model and the unit step actuation function then we apply the update rule so the delta w is alpha times y minus y hat times x and delta bias is alpha times y minus y hat and then we learn the weights and then we in the prediction part with the test data we simply calculate y hat as again this linear model with the actuation function with which then gets one or zero so this is all that we have to do so let's jump to the code so first let's import numpy s and p and then let's create our perceptron class this gets an init function and here we give itself and then the parameters are the learning rate and let's give it a default of point zero and then we also give it an iter so the number of iterations for the optimization algorithm and let's say this is 000 in the beginning then let's store this and say self lr equals the learning rate and self dot n iters equals n headers then we also want to store the activation function and for this let's say this is the unit step function and for this we create a global function for example this could also be in a utility module so we say define unit step func and this gets the input x and then we can do this in one line so we say numpy where x is greater than zero we return one and otherwise zero then we also want to get the weights so in the beginning these are none and also self dot bias and this is also none in the beginning then we want to implement the to fit and predict functions so define fit with self and x and y so the training data and then we do define predict with self and also x so this is now the test data so let's start with the fit function so in here first we get the number of samples and the number of features from the training data and this is x dot shape so here we assume that x is a numpy and d array and then the first thing to do is to init on the parameters so for this let's say self.weights equals numpy zeroes with the shape and features so this is the simplest way to do this this is actually not the best way a better way to do this is to randomly initialize them so i challenge you to do this on your own instead of nump zeros try to use a random initialization but um in our case in our test example this still works pretty well so we can also do this and for the bias self the bias this is zero um this is also zero in the beginning and now we want to make sure that the class labels are one and zero and that for not for example one and minus one so we say y underscore equals and then we can again use this numpy where so we say this is numpy where y is greater than 0 then this is one and otherwise zero and then we do the optimization or let's say learn weights and now we say for underscore because we don't need this in range self dot n iters and then we iterate over all the samples so we say for index and x i in and numerate and then here x so enumerate gives us both the index and the sample and then we approximate this so we say linear output equals numpy dot of x i and self dot weight so w times x and then plus self dot bias then we say y predicted or y hat equals self dot actuation function of this linear output and now we want to apply the perceptron update rule and for this let's have a look at the formula again so the update rule is the delta w is alpha times y minus y hat times x and the delta bias is alpha times y minus y hat so this part is the same so we call this let's call this update equals self.lr times and then we say self and not self y underscore of the current index minus y predicted so this is the update part and then we say self dot weights plus equals the update times x i for the dot weights and self dot bias simply plus equals the update part and this is all that we need so now we are done with the fit method and now for the predict method we simply again do the linear output so we can now actually copy this so we say linear output equals numpy dot and here we can put in the whole x and then we do the actuation function so again y predicted equals self actuation function and then we return y predicted and now this is all that we need in order to implement the perceptron so now let's test this so i already prepared the code for the testing you can find this on github i put the link in the description so let's go over this very quickly so we import matpotlib and then train test split and data sets from sklearn then we create a helper function for the accuracy then we create a test set by saying data sets make blobs with 150 samples and two features then we split this into training and testing then we create our perceptron with a learning rate and the number of iterations and call perceptron fit with the training data and then predict with the test data and these are our predictions so then we print the accuracy by calling accuracy with the actual labels and the predictions and now i also want to plot the decision boundary here so for this you can use this code which uses the weights and the bias to create at the decision boundary so if we run this then we should see the plots so yeah these are our two blobs and this is the decision boundary so in this case it perfectly learned a decision boundary and now we can see the accuracy is one 100 so everything was correct so our code is working and this is how we implement the perceptron i hope you enjoyed this lesson and then i hope to see you in the next one [Music]

Original Description

In the 8th lesson of the Machine Learning from Scratch course, we will learn how to implement the Perceptron algorithm. You can find the code here: https://github.com/AssemblyAI-Examples/Machine-Learning-From-Scratch Previous lesson: https://youtu.be/Rjr62b_h7S4 Next lesson: https://youtu.be/T9UcK-TxQGw Welcome to the Machine Learning from Scratch course by AssemblyAI. Thanks to libraries like Scikit-learn we can use most ML algorithms with a couple of lines of code. But knowing how these algorithms work inside is very important. Implementing them hands-on is a great way to achieve this. And mostly, they are easier than you’d think to implement. In this course, we will learn how to implement these 10 algorithms. We will quickly go through how the algorithms work and then implement them in Python using the help of NumPy. ▬▬▬▬▬▬▬▬▬▬▬▬ CONNECT ▬▬▬▬▬▬▬▬▬▬▬▬ 🖥️ Website: https://www.assemblyai.com/?utm_source=youtube&utm_medium=referral&utm_campaign=scratch08 🐦 Twitter: https://twitter.com/AssemblyAI 🦾 Discord: https://discord.gg/Cd8MyVJAXd ▶️ Subscribe: https://www.youtube.com/c/AssemblyAI?sub_confirmation=1 🔥 We're hiring! Check our open roles: https://www.assemblyai.com/careers ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ #MachineLearning #DeepLearning
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from AssemblyAI · AssemblyAI · 0 of 60

← Previous Next →
1 Python Speech Recognition in 5 Minutes
Python Speech Recognition in 5 Minutes
AssemblyAI
2 Python Click Part 1 of 4
Python Click Part 1 of 4
AssemblyAI
3 Python Click Part 2 of 4
Python Click Part 2 of 4
AssemblyAI
4 Python Click Part 3 of 4
Python Click Part 3 of 4
AssemblyAI
5 Python Click Part 4 of 4
Python Click Part 4 of 4
AssemblyAI
6 Deep learning in 5 minutes | What is deep learning?
Deep learning in 5 minutes | What is deep learning?
AssemblyAI
7 How to make a web app that transcribes YouTube videos with Streamlit | Part 1
How to make a web app that transcribes YouTube videos with Streamlit | Part 1
AssemblyAI
8 How to make a web app that transcribes YouTube videos with Streamlit | Part 2
How to make a web app that transcribes YouTube videos with Streamlit | Part 2
AssemblyAI
9 Batch normalization | What it is and how to implement it
Batch normalization | What it is and how to implement it
AssemblyAI
10 Real-time Speech Recognition in 15 minutes with AssemblyAI
Real-time Speech Recognition in 15 minutes with AssemblyAI
AssemblyAI
11 Regularization in a Neural Network | Dealing with overfitting
Regularization in a Neural Network | Dealing with overfitting
AssemblyAI
12 Add speech recognition to your Streamlit apps in 5 minutes
Add speech recognition to your Streamlit apps in 5 minutes
AssemblyAI
13 Transformers for beginners | What are they and how do they work
Transformers for beginners | What are they and how do they work
AssemblyAI
14 Automatic Chapter Detection With AssemblyAI | Python Tutorial
Automatic Chapter Detection With AssemblyAI | Python Tutorial
AssemblyAI
15 Deep Learning Series Part 1 - What is Deep Learning?
Deep Learning Series Part 1 - What is Deep Learning?
AssemblyAI
16 Deep Learning Series part 2 - Why is it called “Deep Learning”?
Deep Learning Series part 2 - Why is it called “Deep Learning”?
AssemblyAI
17 Activation Functions In Neural Networks Explained | Deep Learning Tutorial
Activation Functions In Neural Networks Explained | Deep Learning Tutorial
AssemblyAI
18 Deep Learning Series part 3 - Deep Learning vs. Machine Learning
Deep Learning Series part 3 - Deep Learning vs. Machine Learning
AssemblyAI
19 Deep Learning Series part 4 - Why is Deep Learning better for NLP?
Deep Learning Series part 4 - Why is Deep Learning better for NLP?
AssemblyAI
20 Intro to Batch Normalization Part 1
Intro to Batch Normalization Part 1
AssemblyAI
21 Intro to Batch Normalization Part 2
Intro to Batch Normalization Part 2
AssemblyAI
22 Intro to Batch Normalization Part 3 - What is Normalization?
Intro to Batch Normalization Part 3 - What is Normalization?
AssemblyAI
23 Intro to Batch Normalization Part 4
Intro to Batch Normalization Part 4
AssemblyAI
24 Intro to Batch Normalization Part 5
Intro to Batch Normalization Part 5
AssemblyAI
25 Sentiment Analysis for Earnings Calls with AssemblyAI
Sentiment Analysis for Earnings Calls with AssemblyAI
AssemblyAI
26 Summarizing my favorite podcasts with Python
Summarizing my favorite podcasts with Python
AssemblyAI
27 Introduction to Regularization
Introduction to Regularization
AssemblyAI
28 How/Why Regularization in Neural Networks?
How/Why Regularization in Neural Networks?
AssemblyAI
29 Getting Started With Torchaudio | PyTorch Tutorial
Getting Started With Torchaudio | PyTorch Tutorial
AssemblyAI
30 Types of Regularization
Types of Regularization
AssemblyAI
31 Tuning Alpha in L1 and L2 Regularization
Tuning Alpha in L1 and L2 Regularization
AssemblyAI
32 Dropout Regularization
Dropout Regularization
AssemblyAI
33 What is GPT-3 and how does it work? | A Quick Review
What is GPT-3 and how does it work? | A Quick Review
AssemblyAI
34 Backpropagation For Neural Networks Explained | Deep Learning Tutorial
Backpropagation For Neural Networks Explained | Deep Learning Tutorial
AssemblyAI
35 Jupyter Notebooks Tutorial | How to use them & tips and tricks!
Jupyter Notebooks Tutorial | How to use them & tips and tricks!
AssemblyAI
36 Best Free Speech-To-Text APIs and Open Source Libraries
Best Free Speech-To-Text APIs and Open Source Libraries
AssemblyAI
37 Regularization - Early stopping
Regularization - Early stopping
AssemblyAI
38 Regularization - Data Augmentation
Regularization - Data Augmentation
AssemblyAI
39 Bias and Variance for Machine Learning | Deep Learning
Bias and Variance for Machine Learning | Deep Learning
AssemblyAI
40 Recurrent Neural Networks (RNNs) Explained - Deep Learning
Recurrent Neural Networks (RNNs) Explained - Deep Learning
AssemblyAI
41 What is BERT and how does it work? | A Quick Review
What is BERT and how does it work? | A Quick Review
AssemblyAI
42 Introduction to Transformers
Introduction to Transformers
AssemblyAI
43 Transformers | What is attention?
Transformers | What is attention?
AssemblyAI
44 Transformers | how attention relates to Transformers
Transformers | how attention relates to Transformers
AssemblyAI
45 Transformers | Basics of Transformers
Transformers | Basics of Transformers
AssemblyAI
46 Supervised Machine Learning Explained For Beginners
Supervised Machine Learning Explained For Beginners
AssemblyAI
47 Transformers | Basics of Transformers Encoders
Transformers | Basics of Transformers Encoders
AssemblyAI
48 Transformers | Basics of Transformers I/O
Transformers | Basics of Transformers I/O
AssemblyAI
49 How to evaluate ML models | Evaluation metrics for machine learning
How to evaluate ML models | Evaluation metrics for machine learning
AssemblyAI
50 Unsupervised Machine Learning Explained For Beginners
Unsupervised Machine Learning Explained For Beginners
AssemblyAI
51 Weight Initialization for Deep Feedforward Neural Networks
Weight Initialization for Deep Feedforward Neural Networks
AssemblyAI
52 Q-Learning Explained - Reinforcement Learning Tutorial
Q-Learning Explained - Reinforcement Learning Tutorial
AssemblyAI
53 Should You Use PyTorch or TensorFlow in 2022?
Should You Use PyTorch or TensorFlow in 2022?
AssemblyAI
54 What is Layer Normalization? | Deep Learning Fundamentals
What is Layer Normalization? | Deep Learning Fundamentals
AssemblyAI
55 I created a Python App to study FASTER
I created a Python App to study FASTER
AssemblyAI
56 How to create your FIRST NEURAL NETWORK with TensorFlow!
How to create your FIRST NEURAL NETWORK with TensorFlow!
AssemblyAI
57 Neural Networks Summary: All hyperparameters
Neural Networks Summary: All hyperparameters
AssemblyAI
58 Getting Started with OpenAI API and GPT-3 | Beginner Python Tutorial
Getting Started with OpenAI API and GPT-3 | Beginner Python Tutorial
AssemblyAI
59 Convert Speech-To-Text In Python in 60 seconds!
Convert Speech-To-Text In Python in 60 seconds!
AssemblyAI
60 Gradient Clipping for Neural Networks | Deep Learning Fundamentals
Gradient Clipping for Neural Networks | Deep Learning Fundamentals
AssemblyAI

This video teaches how to implement the Perceptron algorithm from scratch using Python, covering the Perceptron update rule, weights, and bias, and providing a practical example with code. It's essential for understanding supervised learning and neural networks.

Key Takeaways
  1. Initialize weights and bias
  2. Calculate linear output
  3. Apply unit step activation function
  4. Update weights and bias using Perceptron update rule
  5. Apply Perceptron update rule
  6. Update weights and bias
  7. Plot decision boundary
  8. Test perceptron with accuracy
💡 The Perceptron update rule is a simple yet powerful concept for learning weights and bias in a supervised learning setting.

Related Reads

📰
Why Data Structures Still Matter When You're Doing Machine Learning
Learn why data structures are crucial for machine learning performance and efficiency, beyond just interview prep
Dev.to · Ardhansu Das
📰
Stop Feeding FX Bots Price Data Only: Add a Macro Risk Gate in Python
Learn to add a macro risk gate to your FX bots in Python to avoid trading during high-risk events
Dev.to · Robert Tidball
📰
Human-in-the-Loop: Di Titik Mana Otomatisasi Harus Berhenti
Learn when to limit automation in machine learning systems to avoid risks, and why human-in-the-loop is crucial
Medium · Machine Learning
📰
Three Python Concepts That Completely Changed How I Think About Programming
Discover three Python concepts that transformed the author's programming approach and learn how to apply them to improve your own coding skills
Medium · AI
Up next
Dropout in Deep Learning
AnuTech-CH
Watch →