How to implement SVM (Support Vector Machine) from scratch with Python

AssemblyAI · Intermediate ·📐 ML Fundamentals ·3y ago

Key Takeaways

The video demonstrates how to implement a Support Vector Machine (SVM) from scratch using Python, covering the concepts of linear models, decision boundaries, and margin maximization. It utilizes tools such as numpy, sklearn, and matplotlib for implementation and visualization.

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 lecture we learn about support Vector machine or short svm so as always we start with a short Theory section and then we jump to the code so let's start so the idea behind svm is to use a linear model and try to find a linear decision boundary also called hyperplane that best separates the data and the best hyperplane is the one that yields the largest separation or margin between both classes so we choose the hyperplane so that the distance from it to the nearest data point on each side is maximized and this is the whole idea so let's look at an example to make this more clear so here we have our feature vectors in 2D with two classes blue and green and we want to find a linear our decision boundaries so in this case this is just a line that has the largest margin between both classes so the distance from this decision boundary to the nearest point on each side is maximized and these nearest points are also called the support vectors so if we describe this in a mathematical way then here we have this line equation W Times x minus B and this should be 0 at the point of the decision boundary and one and on this side where the class is plus one and then minus one on this side where the class is -1 and then this margin here this distance here can be calculated with 2 over the magnitude of w and W are the weight so these are what we have to learn during training so now if we put this into a mathematical equation we can say W Times x minus B should be greater or equal than one if y equals one so for the right labels we want to lie on this side of the decision boundary and for the green for -1 we want to be on this side so here it should be smaller or equal than minus one and if we put this in only one equation then we can multiply this with the label so we say y i times W Times x minus B should be greater or equal than one and our y's should be should have the labels minus one and plus one so not zero and one in this case so now we have to find the weights and for this we Define a loss function so one part of the loss function is the hinge loss and the hinge loss is calculated as the maximum of zero or one minus and here we have this equation y i times W Times x minus B so in other words this is zero if we are greater than one or this term otherwise so this means if we fulfill this term if we are on the correct side of the decision boundary for here or here then our loss is zero and otherwise it's this term so it's if we plot this then it looks like this the blue line so the further we are away from the decision boundary on the incorrect side the higher our loss gets so this is one part and then we also add a regularization term and now this is our cost function Lambda times the magnitude of w to the power of 2 plus and then here this is our hinge loss so so basically this is a trade-off between minimizing this loss here and maximizing the distance to both sides so if we have a look here then we see this distance is 2 over the magnitude of w so this should be maximized and in the cost function this should be minimized so we switch this around and then here we have this Lambda parameter that controls how important this part here is so then we can differentiate between if we are on the correct side of the decision boundary then our cost function is only the first part so Lambda times w and on the other hand um we have Lambda times W plus and then here we have the hinge loss so this is only for one component I so here we can get rid of the sum and now we have to find the weights and the bias so we calculate the the derivative of the cost function with respect to W and to B so let's calculate the derivative or the gradients here again we make this differentiation so we check if we are on the correct side and then the gradients of the cost function with respect to W is 2 times Lambda times W and the gradients with respect to B is zero so no change in this case and in the other case then this is our gradient with respect to the weights and this with respect to B so yeah please double check the math for yourself and now know when we have the gradients we want to put this into our update rules so here we say the gradient equals the gradient minus Alpha minus the learning rate times the gradient and then we plug this in so in this case the gradient is this and then for the bias in this case there's no change and in the other case then we have this formula so this is what we need and now to summarize the steps in the training part with the training data we first initialize the weights then also make sure that our class labels are -1 and 1 and then we apply the update rules that we've just seen for the number of iterations that we specify as parameter and then we learn the weights and then for the prediction we simply apply this linear function and say W Times x minus B and then we decide for the sine so if we are greater than zero then we say this is class 1 and if we are smaller than zero then we say this is class minus one and yeah this is all we have to do so now let's jump to the code so first let's import numpy S and P and then let's create our class svm this gets an init function and here we give it the parameters so this should get a learning rate and we can initialize this with 0.001 for example then we also want to give it the Lambda parameter and a good starting point for example is point zero one and then we also want to give it the number of iterations and let's say this is one thousand by default then we want to store this so we say self dot LR equals learning rate self dot Lambda param equals Lambda param self Dot N errors equals n eaters and then we also want to store the weight self dot w equals none in the beginning and self dot b the bias is also none then we want to define the fit method which gets the training data X and Y and then we also want to have a predict method which gets only the test samples X so let's start with fit so here we say the number of samples and the number of features is x dot shape and here we assume that X and Y are already numpy and D arrays then we want to make sure that the classes have the values minus one or plus one so we say y underscore equals and then we can use numpy where and then we check where Y is smaller or equal than zero then we say this is -1 and otherwise plus one then we want to init the weights so we say self dot w equals numpy zeros with the shape and features and self dot bias equals zero so this is the simplest way to initialize this and it will work but this is actually not the best way so it would be better to randomly initialize the values here but for this I challenge you to do this on your own but yeah as I said it works um it still works so let's go on and now we want to learn the weights with the update rule so we say four underscore because we don't need this in range self Dot and headers and then for index and x i in enumerate and enumerate X so over all the samples and then we check the condition equals and now let's have a look back at the formula so this is the condition y times W Times x minus B should be greater or equal than one so y underscore of the current index times and now we can use the dot product numpy Dot and here we say x i and self dot w minus self dot b and this should be greater or equal than one and if the condition if the condition is true then we let's have a look again here we apply those different update rules depending on the different gradients so for this let's say self dot w minus equals self dot learning rate LR times 2 times self dot Lambda param times self dot w so let's check again W minus the learning rate times 2 Lambda W and for the bias so this has no update in this case because the gradient is zero so now we can check the other case so in the other case we say else the self dot w minus equals self dot l r times and then here this part is 2 times self dot Lambda param times self dot w minus and then we have the second part so in this case minus y i times x i so here we say y underscore or actually we can use the numpy dot product to make the multiplication so here we say x i and y underscore of the current index and then self dot B minus equals let's check again minus equals the learning rate times y i so minus equals self dot LR times y underscore of the index and this is all that we need in the fit method so then we have learned the weights and now in the predict method we can do the approximation by saying numpy Dot and then we apply X and here we have self dot w minus self dot bias and then we return numpy sine of the approximation so this is either plus one or minus one and this is all that we need so now we can test this so for testing I already prepared the code and let's go quickly over this you can can also find all the code on GitHub so here we import train test split and data sets from sklearn and matplotlib then we create an example data set with two blobs so 50 samples and two features then we make sure that the classes are -1 and plus one then we split this into training and testing then we can set up our svm and fit this with the training data then we call classifier predict with the test data then we also calculate and print the accuracy and then I prepared some helper codes to also plot the decision boundary and also plot the other two hyperplanes at the offset minus one and plus one so if we run this then hopefully this should work so yeah here we have our two blobs and this is perfectly separated so this is our decision boundary and these are the two hyper planes at plus one and minus 1 and now if we print the accuracy so this is 100 in this case so this worked perfectly so our own svm class is working and yeah this is how we can Implement svm from scratch I hope you enjoyed this lesson and then I hope to see you in the next one bye [Music]

Original Description

In the 9th lesson of the Machine Learning from Scratch course, we will learn how to implement the SVM (Support Vector Machine) algorithm. You can find the code here: https://github.com/AssemblyAI-Examples/Machine-Learning-From-Scratch Previous lesson: https://youtu.be/aOEoxyA4uXU Next lesson: https://youtu.be/6UF5Ysk_2gk 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=scratch09 🐦 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 a Support Vector Machine (SVM) from scratch using Python, covering key concepts such as linear models, decision boundaries, and margin maximization. It provides a step-by-step guide on how to implement SVM using numpy, sklearn, and matplotlib. By watching this video, viewers can learn how to train an SVM model, make predictions, and evaluate its performance.

Key Takeaways
  1. Define a loss function
  2. Calculate the hinge loss
  3. Add a regularization term to the cost function
  4. Minimize the cost function to find the optimal values of W and B
  5. Initialize weights and bias as None
  6. Make sure class labels are -1 and 1
  7. Apply update rules to training data for a specified number of iterations
  8. Update weights using the formula W = W - Alpha * (2 * Lambda * W)
  9. Update bias using the formula B = B - Alpha * 0
  10. Define the condition for SVM classification
💡 The video highlights the importance of margin maximization in SVM implementation and demonstrates how to calculate the decision boundary and hyperplanes using gradient descent.

Related Reads

📰
How I Built a Retail Product Recommendation System That Could Generate £311K Annual Business Value
Learn how to build a retail product recommendation system that can generate significant annual business value using machine learning
Medium · Machine Learning
📰
How I Built a Retail Product Recommendation System That Could Generate £311K Annual Business Value
Learn how to build a retail product recommendation system that can generate significant annual business value
Medium · Data Science
📰
Normal Distribution — A Complete Guide for Beginners
Learn the basics of the normal distribution and its importance in statistics and data science
Medium · AI
📰
Normal Distribution — A Complete Guide for Beginners
Learn the basics of the normal distribution and its importance in machine learning and statistics
Medium · Machine Learning
Up next
Dropout in Deep Learning
AnuTech-CH
Watch →