How does a Support Vector Machine (SVM) work?
- Authors
- Name
- Amit Shekhar
- Published on
A Support Vector Machine (SVM) is a Machine Learning method that separates things into groups by drawing the boundary that keeps the groups as far apart as possible.
In this blog, we will learn about Support Vector Machines (SVM). We will also see how an SVM draws a decision boundary, why we want a large margin, what support vectors are, how the soft margin handles overlapping data, how the kernel trick handles data that cannot be separated by a straight line, and where SVM works well and where it fails.
We will cover the following:
- What is a Support Vector Machine?
- What does it mean to separate data?
- Drawing a line: the decision boundary
- Many lines are possible, which one is best?
- The margin and why we want it large
- Support Vectors: the most important points
- A numeric example
- What if the data overlaps? (Soft Margin)
- What if the data cannot be separated by a straight line? (The Kernel Trick)
- Common kernels
- Seeing it in code
- Advantages and disadvantages
- Where SVM is used
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
What is a Support Vector Machine?
A Support Vector Machine is a Machine Learning method that learns to separate things into groups by drawing the boundary that keeps the groups as far apart as possible.
A Vector is a point described by numbers. For example, a fruit can be described by two numbers, its weight and the roughness of its skin. So that fruit becomes a point in a chart. A Support Vector is a special point that helps hold the boundary in place. We will understand this soon.
So, a Support Vector Machine is a system that uses a few important points to decide the best boundary between groups.
Let's say we have a basket of apples and oranges. We want a machine that looks at a new fruit and tells us, "This is an apple" or "This is an orange." An SVM learns the boundary between apples and oranges from past examples, and then uses that boundary to classify any new fruit.
This is the core idea of an SVM.
What does it mean to separate data?
Before jumping into the boundary, we must understand what our data looks like.
In Machine Learning, we describe every item using numbers called features. A feature is one measurable property of the item.
Let's say we want to separate apples from oranges. We can use two features:
- Weight of the fruit
- Roughness of the skin
Now, every fruit becomes a point on a chart. The weight is the position on the left-right axis, and the roughness is the position on the up-down axis.
So, our fruits become points like below:
roughness
^
| O O
| O O
| O
| A
| A A
| A A
+-------------------------> weight
O = orange, A = apple
Here, we can see that oranges (O) gather in one area and apples (A) gather in another area. They form two separate clusters.
To separate the data means to find a clean way to split these two groups. If we can draw a line that keeps all oranges on one side and all apples on the other side, the data is separated.
This is what an SVM tries to do.
To learn Supervised and Unsupervised Learning and the rest of the Machine Learning Fundamentals from the ground up, check out our AI and Machine Learning Program at Outcome School.
Drawing a line: the decision boundary
Now, the most natural way to split two groups on a chart is to draw a straight line between them.
This dividing line is called the decision boundary.
A decision boundary is the line that an SVM uses to decide which group a point belongs to.
Once the boundary is drawn, the rule is easy. If a new point falls on the orange side, we call it an orange. If it falls on the apple side, we call it an apple.
Let's see the boundary like below:
roughness
^
| O O \
| O O \
| O \
| \ A
| \ A A
| \ A A
+--------------------------------> weight
O = orange, A = apple, \ = decision boundary
Here, we can see a straight line drawn between the two groups. All oranges sit on the left of the line, and all apples sit on the right. So, for any new fruit, we just check which side of the line it lands on.
Note: We are using two features, so our boundary is a line on a flat chart. If we use three features, the boundary becomes a flat sheet in 3D, called a plane. For more than three features, it is called a hyperplane. A hyperplane is the general word for this boundary in any number of dimensions. We cannot draw it, but the idea stays exactly the same.
Many lines are possible, which one is best?
There is not just one line that separates the two groups. There are many lines that can do the job.
Let's see a few possible lines like below:
roughness
^
| O O \ | /
| O O \ | /
| O \ | /
| \|/ A
| X A A
| /|\ A A
+-------------------------------> weight
Here, we can see that several different lines all separate the oranges from the apples. Each one keeps oranges on one side and apples on the other. So, all of them look correct.
But, the following question arises. If all these lines separate the data, which line must we choose?
A line that passes very close to the points is risky. A new fruit that is only slightly different can land on the wrong side and get the wrong label. So, a line squeezed tightly against one group is a bad choice. We need a line that stays away from both groups.
So, how can we pick the best line? The answer is the margin.
The margin and why we want it large
Now, let's understand the most important idea in an SVM, the margin.
The margin is the empty gap between the decision boundary and the closest points of each group.
An SVM does not pick just any separating line. An SVM picks the line that has the largest possible margin.
Why do we want the largest margin? Because a wide gap gives us safety. If the boundary sits far from both groups, then a new point can move around a bit and still stay on the correct side. A wide margin makes the SVM more reliable on new, unseen data.
Let's see the margin like below:
roughness
^
| O O : | :
| O O : | : A
| O O | : A
| O : | A A
| O O : | : A A
+--------------------------------------> weight
<----------->
the margin
Here, we can see the solid line | in the middle is the decision boundary. The two dotted lines : on either side mark the edge of the margin. The margin is the empty space between these dotted lines, with the boundary exactly in the middle. An SVM widens this gap as much as possible while still keeping the two groups on their correct sides.
So, the best line is not the one that just separates the data. It is the one that separates the data with the widest gap.
This is why an SVM is also called a maximum margin classifier.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Support Vectors: the most important points
Now, let's understand where the name "Support Vector" comes from.
To find the widest margin, the SVM does not care about every single point equally. Most points are far away from the boundary and do not affect it at all. Only the points sitting right at the edge of the margin matter.
These edge points are called the Support Vectors.
A Support Vector is a data point that sits on the edge of the margin and holds the boundary in place.
The support vectors are the closest points to the boundary from each group. They are the points that "support" the dividing line.
Let's see the support vectors like below:
roughness
^
| O O : | :
| O O : | : A
| O [O] | : A
| O : | [A] A
| O O : | : A A
+--------------------------------------> weight
Here, we can see that only a few points, marked with square brackets, touch the dotted margin edges. These are the support vectors. The other points sit away from the boundary and do not change it.
If we remove a faraway point, the boundary does not change at all. But if we move a support vector, the boundary shifts. So, only the support vectors decide the boundary.
This is why the method is called a Support Vector Machine. The few support points carry the whole decision, and this is also why the final model stays compact.
A numeric example
The best way to learn this is by taking an example. Let's keep it very small and use just one feature.
Suppose we are sorting fruits using only one number, the weight in hundreds of grams. Light fruits are oranges, and heavy fruits are apples.
Assume that we have these training points:
- Oranges: weight 2 and weight 3
- Apples: weight 7 and weight 8
Let's place them on a number line like below:
oranges apples
O O A A
----2---3-----------|-----------7---8----------> weight
^
boundary at 5
Here, we can see that the oranges sit at 2 and 3, and the apples sit at 7 and 8. The closest orange to the apples is at 3. The closest apple to the oranges is at 7. These two closest points are our support vectors, because they decide where the boundary goes.
Now, let's find the best boundary. The widest-margin boundary sits exactly in the middle of the two closest points.
Step 1: Find the two closest points across the groups. They are 3 (orange) and 7 (apple).
Step 2: Put the boundary right in the middle. The middle of 3 and 7 is (3 + 7) / 2 = 5.
Step 3: Measure the margin. The gap from the boundary at 5 to each support vector is 5 - 3 = 2 on one side, and 7 - 5 = 2 on the other side. So the margin is 2 on each side.
So, our rule becomes simple. If a new fruit weighs less than 5, it is an orange. If it weighs more than 5, it is an apple.
Let's test it. A new fruit weighs 4. Since 4 is less than 5, the SVM says it is an orange. It works perfectly.
This is the complete SVM idea done by hand. The boundary sits as far as possible from both groups, and only the closest points decided where it goes.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
What if the data overlaps? (Soft Margin)
Real data is rarely so clean. Sometimes a few oranges sneak into the apple area, or a few apples sit among the oranges.
Let's say one orange is unusually heavy and lands very close to, or even inside, the apple group. If we insist on a perfect separation with zero mistakes, the SVM must squeeze the margin very narrow, or twist the boundary into a strange shape when it can draw curves, just to handle that one odd point. This makes the boundary fit the noise instead of the real pattern.
This problem of fitting the noise instead of the real pattern is called overfitting. An overfitted model does well on the training data but fails on new data.
So, here comes the Soft Margin to the rescue. The strict version that allows zero mistakes is called a hard margin, and the flexible version that allows a few mistakes is called a soft margin.
A Soft Margin allows the SVM to make a few small mistakes on purpose, so that it can keep a wide and sensible boundary.
The SVM can let a few points sit on the wrong side, if doing so gives a much wider margin overall.
Let's see a soft margin like below:
roughness
^
| O O : | :
| O O : | : A
| O O | : A
| O : | A O
| O O : | : A A
+--------------------------------------> weight
Here, we can see one orange has crossed over into the apple side. Instead of moving the boundary to chase that single point, the SVM accepts the small error and keeps the margin wide.
Now, how much error do we allow? This is controlled by a setting called C.
- A large C tells the SVM to avoid mistakes as much as possible. The margin becomes narrow, and the model fits the training data tightly. This raises the risk of overfitting.
- A small C tells the SVM to allow more mistakes for a wider margin. The model becomes more general and usually handles new data better.
So, we tune the value of C based on our use case. We have a detailed blog on how the Bias-Variance Tradeoff works that explains this balance between a too simple and a too complex model step by step.
What if the data cannot be separated by a straight line? (The Kernel Trick)
Now, here is a harder problem. Sometimes no straight line can separate the two groups, no matter how we draw it.
Let's say one group sits in the center and the other group surrounds it like a ring. Consider the data like below:
O O O
O A A A O
O A A A O
O A A A O
O O O
Here, we can see that the apples are in the middle and the oranges form a circle around them. No straight line can split these two groups. Any straight line we draw will cut through both groups.
So, what is the solution to this problem? Answer: the Kernel Trick.
The Kernel Trick is a method that lets the SVM work as if the data had an extra dimension, so that a straight boundary becomes possible.
In simple words, if we cannot separate the points on a flat chart, we lift them up into a higher space where a straight boundary works.
Let's understand this with our ring example. Imagine we push the center apples upward and keep the outer oranges low, based on how far each point is from the center. Now the apples float above and the oranges stay below.
Let's see this lift like below:
Before (one flat chart, no straight line works):
O A A O
After lifting into a new height dimension:
height
^
| A A A
| A A A
| ......................
| O O O O
+-----------------------------> position
Here, we can see that once we lift the apples into a new height dimension, a flat boundary, shown by the dotted line, can slide between the high apples and the low oranges. The problem that was impossible on a flat chart becomes easy in the higher space.
Now, the important part. Lifting every point into a higher space and doing all that math would be very slow and expensive. The kernel trick avoids this completely. A kernel computes the result as if the points were lifted into the higher space, without ever actually moving them there. It gives us the benefit of the higher space at a very low cost.
So, the kernel trick lets an SVM draw curved boundaries on the original chart while only doing cheap calculations.
Common kernels
A kernel is the function that decides how the SVM measures similarity between points and what kind of boundary it can draw. Different kernels give different boundary shapes. Let me tabulate the common kernels for your better understanding.
| Kernel | What it does | When to use it |
|---|---|---|
| Linear | Draws a straight boundary | When the data is already separable by a straight line |
| Polynomial | Draws curved boundaries | When the groups need a gentle curve to separate |
| RBF (Radial Basis Function) | Draws flexible, rounded boundaries | The most popular choice for complex, non-linear data |
Here, we can see that the Linear kernel is the simplest and fastest, and we use it when a straight line is enough. The RBF kernel, also called the Gaussian kernel, is the most common default choice because it can fit many kinds of patterns. We pick the kernel depending upon our use-cases.
Note: When we use the RBF kernel, there is one more setting called gamma. A small gamma gives a smooth, simple boundary, and a large gamma gives a very wiggly boundary that hugs the points closely. A very large gamma can lead to overfitting, so we tune it carefully.
We have a complete program on Hyperparameter Tuning and Model Development and Training - check out our AI and Machine Learning Program at Outcome School to master them in depth.
Seeing it in code
Now, let's see a small SVM in code. We will use Python with the scikit-learn library. Let's see the code as below:
from sklearn import svm
# Training data: each point has two features
X_train = [[2, 1], [3, 2], [7, 8], [8, 7]]
# Labels: 0 = orange, 1 = apple
y_train = [0, 0, 1, 1]
# Create an SVM with an RBF kernel
model = svm.SVC(kernel="rbf", C=1.0, gamma="scale")
# Train the SVM on our data
model.fit(X_train, y_train)
# Predict the group of a new fruit
prediction = model.predict([[4, 3]])
print(prediction)
Here, we have done the following:
X_trainholds our training points, where each point has two features, such as weight and roughness.y_trainholds the correct labels, where0means orange and1means apple.svm.SVC(kernel="rbf", C=1.0, gamma="scale")creates the SVM. Here,kernel="rbf"chooses the flexible RBF kernel,C=1.0sets the soft margin balance, andgamma="scale"lets the library pick a sensible gamma for us.model.fit(...)trains the SVM, which means it finds the support vectors and the best boundary.model.predict(...)takes a new fruit and tells us which group it belongs to.
This will print the following:
[0]
Here, we can see the output is 0, which means the SVM classified the new fruit with features [4, 3] as an orange. The library handled the support vectors, the margin, and the kernel trick behind the scenes.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
Advantages and disadvantages
Now, let's understand the strengths and weaknesses of an SVM.
Advantages:
- Works well with clear gaps: When the two groups are well separated, an SVM finds a very clean and reliable boundary.
- Handles many features: An SVM works well even when each item has a very large number of features.
- Handles curved boundaries: Thanks to the kernel trick, an SVM can separate data that is not separable by a straight line.
- Uses only a few points: The boundary depends only on the support vectors, so the final model is compact.
Disadvantages:
- Slow on very large datasets: When we have a huge number of training points, an SVM can become slow to train.
- Needs careful tuning: We must choose the kernel, the value of
C, and the gamma carefully, and a wrong choice hurts the results. - Hard to read the result: With a complex kernel, it is not easy to explain in plain words why the model made a certain decision.
- Sensitive to feature scale: If one feature has very large numbers and another has very small numbers, the SVM can get confused, so we usually scale the features first.
So, an SVM is a strong choice for clean, medium-sized problems, and we weigh these trade-offs based on our use case.
When we need the reasons behind a model's decision, we have a detailed blog on Explainable AI that covers this end to end.
Where SVM is used
Now, let's see where SVMs are used in the real world.
- Spam detection: Sorting emails into spam and not-spam.
- Image classification: Telling whether an image contains a cat, a dog, a car, and etc.
- Face detection: Finding whether a region of an image is a face or not.
- Text categorization: Sorting documents into topics like sports, politics, and technology.
- Medical diagnosis: Helping decide whether a tumor is harmful or harmless based on its measurements.
- Handwriting recognition: Recognizing handwritten letters and digits.
Now we must have understood how a Support Vector Machine works.
Frequently Asked Questions
What does the C parameter do in an SVM?
C controls how many mistakes the SVM is allowed to make on the training data. A large C tells the SVM to avoid mistakes as much as possible, which gives a narrow margin and raises the risk of overfitting. A small C allows more mistakes for a wider margin, so the model becomes more general and usually handles new data better.
What does gamma do in an SVM with the RBF kernel?
Gamma controls how smooth or wiggly the boundary is when we use the RBF kernel. A small gamma gives a smooth, simple boundary, and a large gamma gives a very wiggly boundary that hugs the points closely. A very large gamma can lead to overfitting, so we tune it carefully.
Why should we scale the features before training an SVM?
We should scale the features because an SVM is sensitive to feature scale. If one feature has very large numbers and another has very small numbers, the SVM can get confused. So, we usually scale the features first, before we train the SVM.
Is SVM a good choice for very large datasets?
No. An SVM can become slow to train when we have a huge number of training points. It is a strong choice for clean, medium-sized problems, and it works well even when each item has a very large number of features.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
