Supportive Vector Machine
π=1 πβ Ξ»π* π¦π = 0
β π₯π,π₯π πππ‘π πππππ‘π π¦π,π¦π ππππ π ππ πππ‘β πππ ππ£πππππππ ππ π πππ‘π
βͺ ππ¦ πππππ¦πππ πππ ππ’π πππ‘π πππππ‘π π€π πππ πππ‘ Ξ»π
βͺ πππ πππβ πππ‘π πππππ‘ π€π π€πππ πππ‘ Ξ»
πβπ ππππ ππ πππ€ π‘π ππππ π‘βπ πππ‘ππππ Ξ»
πβπ Ξ»π π£πππ’ππ πππ‘ππππππ π€βππβ πππ‘π πππππ‘π πππ π π’πππππ‘ π£πππ‘πππ π. π Ξ» π> 0 π=1 π β π=1 πβ Ξ» ( π , π¦ , j) (π₯π* π₯ π) π€12 +π€22 = ||π€|| = (π * π)12 =12 π€ * π€πΏ(π₯, π¦, Ξ») =12 π€ * π€ β Ξ» * π¦ * π€π + π€π( π₯)
ππ‘ππ β 1: ππππ π‘βπ ππππ π ππ πππ’ππ‘πππ π¦ π€π + π€π( * π₯) > 1π¦ = 1
ππππ π 1 πππ’ππ‘πππ π€π + π€π* π₯ > 1π¦ =β 1
ππππ π 2 πππ’ππ‘πππ π€π + π€π* π₯ < 1 ππ‘ππ β 2: πΌππππππ π π‘βπ ππππππ πππ π‘ππππ ππ π‘βπ πππ‘π πππππ‘ π‘π π‘βπ βπ¦ππππππππ π =π€1 π₯1+π€ 2π¦1+π€π | | π€ 12+π€ 22 =π€1π₯ 1+π€ 2π¦1+π€π | | ||π|| =π€π+π€ππ₯ ||π|| ππ‘ππ β 3: π·ππππππ π π‘βπ ||π€|| ππ‘ππ β 4: π·ππππππ π π‘βπ ||π€|| πππ ππ ππ ππππ π‘ππππ π¦ π€π + π€π( * π₯) > 1 πππππ¦ πππππππππ
π₯, π¦, π=(2, 2) 1(4, 4) 1(4, 0) -1(0, 0) 1
π₯, π, Ξ»=(2, 2) 0.25 (4, 4) 0 (4, 0) 0.25 (0, 0) 0
π π π π’πππππ‘ π£πππ‘πππ πππ (2, 2) πππ (4, 0)
π€ =π=1
πβ Ξ»=π , π¦ , π
π₯π = 0. 25 * 1 * (2, 2) + 0. 25 * (β 1) * (4, 0) = (0. 5, 0. 5) β (1, 0) = (0. 5 β 1, 0. 5 β 0) = (β 0. 5
π€π + π€π
π₯ = 1π€π + π€1* π₯
1 + π€2* π₯
2 = 1 π€π β 0. 5 * 2 + 0. 5 * 2
= 1 π€π β 1 + 1
= 1 π€π
= 1β 0. 5 * π₯1 + 0. 5 * π₯2 + 1
ππππ‘ β 1
1π·, 2π·, 3π· , ππ
1π· π π πππππ πππππ‘ : π₯ = 10
2π· π πΏπππ πππ’ππ‘πππ: π₯ = 10, π¦ = 20
3π· π πππππ πππ’ππ‘πππ: π₯ = 10, π¦ = 20, π§ = 30
ππ· π»π¦πππ πππππ πππ’ππ‘πππ: π₯, π¦, π§, π‘β¦
πΏπππ πππ’ππ‘πππ: π¦ = π€π + π€1
- π₯1 2π· πππππ πππ’ππ‘πππ: π¦ = π€π + π€1
- π₯1 + π€2
- π₯2 3π·
π»π¦πππ πππππ πππ’ππ‘πππ: π¦ = π€π + π€1 - π₯1 + π€2
- π₯2 + β¦ + π€π
- π₯π ππ·
πΌπ ππππ π‘πππ π€π βππ£π βπ¦πππ πππππ πππππ
πππ ππππ πππ ππ ππππππ‘ππ¦ π‘βππ βπ¦ππππππππ πππ’ππ‘πππ
πΊππππππππ¦ ππ’π ππππ πππ ππ πππππ‘πππ¦ π‘βπ πππππππππππ‘π ππ π€πππβπ‘π
ππΏπ , πΊπ·
ππ’ππππ‘πππ ππππππ‘
π¦ = π€π + π€1 - π₯1 + π€2 π₯2 + β¦ + π€π
- π₯π = π€π +π
- =1 πβ π€π π₯π
ππππ‘ππ ππππππ πππ‘ππ‘ππ
π¦ = π€π + π€1
- π₯1 + π€2
- π₯2 + β¦ + π€π
- π₯π = π€π + π * π
πππ‘πππ₯ ππππππ πππ‘ππ‘
π¦ = π€π + π€1
- π₯1 + π€2
- π₯2 + β¦ + π€π
- π₯π = π€π + π€ ππ₯ π¦ = π€π +π=1 πβ π€π π₯π (π π’ππππ‘πππ)
π¦ = π€π + π. π ( ππππ‘πππ )
π¦ = π€π + πππ (πππ‘πππ₯)
ππππ‘ β 2:
π€π πππππππ¦ ππππ€ π‘βππ‘ πππ π‘ππππ πππ‘π€πππ π₯
1, π¦1( ) π₯2, π¦2( )
π€βππ‘ ππ π‘βπ ππππππππππ’πππ πππ π‘ππππ πππ‘π€πππ π₯
1, π¦
1( ) π‘π ππ₯ + ππ¦ + π = 0
π =ππ₯1+ππ¦1| +π|
π2+π2
ππππππππ πππ’ππ‘πππ = π€π + π€1
- π₯
1 + π€2 - π₯2
πππππ‘ = (π₯1, π₯2)
π =π€1π₯1+π€2π₯2+π€π| |π€12+π€2
2
π€1π₯1+π€2
π₯2+π€π| | ||π|| =π€π+πππ||||||||π||
ππππ‘ β 3:
ππ π€π ππππ€ ππππ π‘βπ ππππππ’π πππππ‘ πππ πππ₯πππ’π πππππ‘ ππ ππ πππ’ππ‘πππ
πππ ππ₯πππππ ππππ π‘βπ ππππππ’π πππππ‘ ππ π¦ = π₯2
π€π ππππ€ βππ€ π‘π ππ
ππ’π‘ ππ π¦ππ’ π€πππ‘ ππππ ππππππ’π πππππ‘ ππ πππ₯πππ’π ππ πππ¦ πππ’ππ‘πππ πππ ππ ππ ππππ‘βππ πππ’ππ‘πππ
π(π₯, π¦) = π₯2
- π¦2 π(π₯, π¦) = π₯ + π¦ β 1 = 0
π€π π€πππ‘ π‘π ππππ πππ₯πππ’π π£πππ’π ππ π(π₯, π¦) πππ ππ ππ ππππ π‘ππππ π(π₯, π¦)
πΏππππππππ ππ’ππ‘πππππππ‘πππ π‘βπππππ
πΏ(π₯, π¦, Ξ») = π(π₯, π¦) β Ξ» * π(π₯, π¦)
Ξ» = πππππππππ ππ’ππ‘ππππππ
ππ πππππ π‘π ππππ π₯, π¦ π£πππ’ππ
βπΏ
βπ₯ = 0,
βπΏ
βπ¦ = 0,
βπΏ
βΞ» = 0
πΏ(π₯, π¦, Ξ») = π₯2
- π¦2 β Ξ» * [π₯ + π¦ β 1]
πΏ(π₯, π¦, Ξ») = π₯2 - π¦2 β Ξ»π₯ β Ξ»π¦ + Ξ»βπΏ
βπ₯ =β(π₯2+π¦2βΞ»π₯βΞ»π¦+Ξ»)
βπ₯ = 2π₯ β Ξ»βπΏ
βπ¦ =β(π₯2+π¦2βΞ»π₯βΞ»π¦+Ξ»)
βπ¦ = 2π¦ β Ξ»βπΏ
βΞ» =β(π₯2+π¦2βΞ»π₯βΞ»π¦+Ξ»)
βΞ» =β π₯ β π¦ + 1
πππ ππππ ππππ ππ ππππ π‘βπ βπ¦πππ πππππ πππ’ππ‘πππ πππ ππ ππ πππππππππ‘ πππππ‘π ππππ πππππππππ‘ ππππ π ππ
πππ ππ₯πππππ π‘βπππ π‘π€π ππππ π ππ π¦ππ πππ ππ
π€π π€πππ‘ π‘π π ππππππ‘π π‘βππ π π‘π€π ππππ π ππ πππππππ‘ππ¦
1) π»πππ π€π ππππ π‘π ππππ π‘βπ π»π¦πππ πππππ πππ’ππ‘πππ π€βππβ ππππ π πππ¦ πππππππ‘ππ¦ π‘π€π ππππ π ππ
2) πβππ‘ βπ¦ππππππππ π βππ’ππ πππππ‘πππ π πππ₯πππ’π πππ π‘ππππ ππππ ππππππ πππππ‘
3) πβππ π ππππππ πππππ‘π πππ ππππππ ππ ππ’πππππ‘ π£πππ‘πππ
ππ’π ππππ πππ’ππ‘πππ ππ π¦: π€π + π€ππ₯
π€π + π€π
π₯ = 0 π»π¦πππ πππππ πππ’ππ‘πππ
πβππ βπ¦πππ πππππ πππ£ππππ πππ‘πππ π ππππ πππ‘π π‘π€π ππππ‘π : ππππ π β 1 πππ ππππ π β 2
πππ ππ₯πππππ π₯1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π1 : π€π + π€ππ₯1 > 0
πππ ππ₯πππππ π₯1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π2 : π€π + π€ππ₯
1 < 0 πππ ππ₯πππππ π₯ 1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π 1 : π€π + π€ππ₯ 1 > 0
πππ ππ₯πππππ π₯1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π2 : π€π + π€ππ₯1 < 0
πππ ππ₯πππππ π₯1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π1 : π€π + π€ππ₯1 = 1
πππ ππ₯πππππ π₯1 ππ πππ πππππ‘ , ππ π¦ππ’ π€πππ‘ π‘π ππππ π‘βππ πππππ‘ ππ π2 : π€π + π€ππ₯1 =β 1
π€π + π€ππ₯1 = 0 ( π‘βππ π€π π€πππ‘)
π€π + π€ππ₯1 = 1
π€π + π€ππ₯1 =β 1
π¦ * π€π + π€ππ₯ ( 1) > 1π¦ = π¦ππ (+ 1) π¦ = ππ (β 1)
π¦ = 1 ====== > π€π + π€ππ₯ ( 1) > 1 (ππππ π β 1)
π¦ =β 1 ====== > π€π + π€ππ₯ ( 1) < 1 : (ππππ π β 2)
π¦ * π€π + π€ππ₯ ( 1)β₯1 ππππ βπππ ππ ππππ ππ’π‘ π‘βπ πππ‘π πππππ‘π , π€βππβ πππ π ππ‘πππ ππππ π€π + π€
ππ₯1 = 1π€π + π€ππ₯1 =β 1
Objective of SVM: SVM aims to find the hyperplane that maximizes the margin, making the classifier
as robust as possible.
ππππ : ππππ π‘βπ πππ π‘ππππ πππ‘π€πππ πππ‘π πππππ‘ π‘π π‘βπ βπ¦ππππππππ πππ’ππ‘πππ
ππππ π π’ππ π‘βπ πππ π‘ππππ π βππ’ππ ππ πππ₯πππ’π
Support Vectors:
β Definition: Support vectors are the data points that lie closest to the decision boundary
(hyperplane). These points are the most challenging to classify correctly and are the key
points that define the position and orientation of the hyperplane.
Importance:
β The support vectors are critical because they are the points that “support” the optimal
hyperplane. In fact, the SVM model is entirely defined by these support vectors. The position
of all other data points is irrelevant as long as they are correctly classified by the hyperplane.
β If you remove a support vector from the dataset, the hyperplane could shift, potentially
changing the classification of some other points. However, removing a non-support vector
point will not affect the hyperplane.
- Margin Maximization:
β Definition: The margin is the distance between the hyperplane and the nearest data points
from any class (i.e., the support vectors). In a binary classification problem, there will be a
margin on either side of the hyperplane.
β Objective of SVM: SVM aims to find the hyperplane that maximizes this margin, making the
classifier as robust as possible.
β Why Maximize the Margin?:
o Generalization: A larger margin implies that the model has more confidence in its
classification decisions. It reduces the risk of overfitting because the model is less
sensitive to slight variations in the data points.
o Robustness: A wider margin means the model is better at generalizing to unseen
data. If new data points are added, they are more likely to be classified correctly if the margin is large. - Mathematical Perspective
π·ππ π‘ππππ πππ‘π€πππ π π’πππππ‘ π£πππ‘ππ πππ‘ππππππ‘π π‘π βπ¦πππ πππππ ππ πππ£ππ ππ¦
π =π€1π₯1+π€2π¦1+π€π| |π€12+π€2
2
π€1π₯1+π€2π¦1+π€π| | ||π|| =π€π+π€ππ₯||||||||π||
ππππππ
π€π+π€ππ₯||||||||π||
ππ = ||π|| πππππ’π ππππ π‘ππππ π‘π π¦ * π€π + π€
π( π₯)β₯1
πππ‘ππππ§ππ‘πππ:
πππππππ§π π‘βπ = ||π|| πππ πππ‘β π ππππππππ‘π¦ ππ π‘ππ₯π‘ πππππ πππ‘π’ππ ππ =12 π€2
πππππππ§π π‘βπ π‘βπ 12 π€2
π π’πππππ‘ π‘π π‘βπ ππππ π‘ππππ π¦ * π€π + π€
π( π₯)πΏ(π₯, π¦, Ξ») = π(π₯, π¦) β Ξ» * π(π₯, π¦)πΏ π€π
( , π€, Ξ») = ||π|| β Ξ» * [π¦ * π€π + π€π( π₯) β 1]πΏ π
π( , π€, Ξ») = ||π|| β Ξ» * [π¦ * ππ + π€π( π₯) β 1]πΏ ππ( , π, Ξ») = ||π|| β Ξ» * [π¦ * ππ + ππ( π₯) β 1]
πΉππ ππππ¦ πππ‘ππππππ‘π
πΏ π€π( , π€, Ξ») = ||π|| βπ=1πβ Ξ»π
[π¦ππ€π + π€ * π₯π( ) β 1]πΏ π€π( , π€, Ξ») =12 π€2 βπ=1πβ Ξ»π[π¦ππ€π + π€ * π₯π( ) β 1]
- π€βπππ Ξ»π ππ π‘βπ πΏπππππππ ππ’ππ‘πππππππ ππ π πππππ‘ππ π€ππ‘β πππβ ππππ π‘πππππ‘
πΆππ π β 1:ππΏππ€ = 0
ππΏππ€ = π€ βπ=1πβ Ξ»π π¦ππ₯π = 0 - π€ =π=1πβ Ξ»ππ¦ππ₯π
πβππ π βππ€π π‘βππ‘ π‘βπ π€πππβπ‘ π£πππ‘ππ π€ ππ π ππππππ ππππππππ‘πππ ππ π‘βπ π‘πππππππ ππ₯ππππππ ,
π€βπππ π‘βπ πππππππππππ‘π πππ πππ£ππ ππ¦ π‘βπ πΏπππππππ ππ’ππ‘πππππππ Ξ»π.
πΆππ π β 2: ππΏππ€π= 0πΏ π€π( , π€, Ξ») =12 π€2 βπ=1πβ Ξ»π[π¦ππ€π + π€ * π₯π( ) β1]ππΏππ€π=βπ=1πβΞ»ππ¦π=0π=1πβΞ»ππ¦π = 0
πππ€ π π’ππ π‘ππ’π‘π π€ =π=1π βΞ»ππ¦ππ₯π
ππ πΏππππππππ πππ’ππ‘πππ
πΏ π€π( , π€, Ξ») =12 π€2 βπ=1πβ Ξ»π[π¦ππ€π + π€ * π₯π( ) β 1] πππ₯ππππ§π
- π=1πβ Ξ»π β12π=1πβπ=1πβ Ξ»πΞ»ππ¦π π¦π(π₯π,π₯π) π π’πππππ‘ π‘π
- π=1πβ Ξ»ππ¦π = 0π₯π π₯π πππ‘π πππππ‘π π¦ππ¦π ππππ π ππ πππ‘β πππ ππ£πππππππ ππ π πππ‘πππ¦ πππππ¦πππ πππ ππ’π πππ‘π πππππ‘π π€π πππ πππ‘ Ξ»π πππ πππβ πππ‘π πππππ‘ π€π π€πππ πππ‘ Ξ».
- πβπ ππππ ππ πππ€ π‘π ππππ π‘βπ πππ‘ππππ Ξ».
- πβπ Ξ»π π£πππ’ππ πππ‘ππππππ π€βππβ πππ‘π πππππ‘π πππ π π’πππππ‘ π£πππ‘πππ π. π Ξ»π 0
π=1 π βπ=1 π βΞ»π Ξ»ππ¦ππ¦π(π₯π π₯π)π€12
- π€22 = ||π€|| = (π * π)12 =12 π€ * π€
πΏ(π₯, π¦, Ξ») =12 π€ * π€ β Ξ» * π¦ * π€π + π€π( π₯)
ππ‘ππ β 1: ππππ π‘βπ ππππ π ππ πππ’ππ‘πππ π¦ π€π + π€π( * π₯) > 1
π¦ = 1 ππππ π 1 πππ’ππ‘πππ π€π + π€π
- π₯ > 1
π¦ =β 1 ππππ π 2
πππ’ππ‘πππ π€π + π€π - π₯ < 1
ππ‘ππ β 2: πΌππππππ π π‘βπ ππππππ πππ π‘ππππ ππ π‘βπ πππ‘π πππππ‘ π‘π π‘βπ βπ¦ππππππππ
π =π€1π₯1+π€2π¦1+π€π| |π€12+π€2 - π€1π₯1+π€2π¦1+π€π| | ||π|| =π€π+π€ππ₯||||||||π||
- ππ‘ππ β 3: π·ππππππ π π‘βπ ||π€||
- ππ‘ππ β 4: π·ππππππ π π‘βπ ||π€|| πππ ππ ππ ππππ π‘ππππ π¦ π€π + π€π
- ( * π₯) > 1 πππππ¦ πππππππππ
- π₯ππ¦π
(2, 2) 1
(4, 4) 1
(4, 0) -1
(0, 0) 1
π₯πΞ»
(2, 2) 0.25
(4, 4) 0
(4, 0) 0.25
(0, 0) 0
π π π π’πππππ‘ π£πππ‘πππ πππ (2, 2)πππ (4, 0)
π€ =
π=1πβ Ξ»ππ¦ππ₯
π = 0. 25 * 1 * (2, 2) + 0. 25 * (β 1) * (4, 0) = (0. 5, 0. 5) β (1, 0) = (0. 5 β 1, 0. 5 β 0(β0.5π€π+ π€ππ₯ = 1π€π + π€1
- π₯1 + π€2
- π₯2 = 1
π€π β 0. 5 * 2 + 0. 5 * 2 = 1
π€π β 1 + 1 = 1
π€π = 1β 0. 5 * π₯1 + 0. 5 * π₯2 + 1
What Is a Support Vector Machine?
Support Vector Machine is designed to find the optimal hyperplane that separates data points of different classes with the maximum margin. It’s especially effective in high-dimensional spaces and for problems where the decision boundary is not linear.
π Key Concepts
- Hyperplane: A decision boundary that separates different classes. In 2D, it’s a line; in 3D, it’s a plane.
- Support Vectors: The data points closest to the hyperplane. These are critical in defining the margin.
- Margin: The distance between the hyperplane and the support vectors. SVM aims to maximize this.
- Kernel Trick: A method to transform data into higher dimensions to make it linearly separable. Common kernels include linear, polynomial, and radial basis function (RBF).
- Hard Margin vs. Soft Margin:
- Hard Margin: Assumes perfect separation with no misclassification.
- Soft Margin: Allows some misclassification to handle noisy data better.
- Regularization (C): Controls the trade-off between maximizing the margin and minimizing classification error.
βοΈ How It Works
- SVM identifies the hyperplane that best separates the classes.
- It uses support vectors to define the margin.
- If data isn’t linearly separable, it applies the kernel trick to map it into a higher-dimensional space.
- Solves an optimization problem to find the best hyperplane.
Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or cat vs. dog.
The main goal of SVM is to maximize the margin between the two classes. The larger the margin the better the model performs on new and unseen data.


Key Concepts of Support Vector Machine
- Hyperplane: A decision boundary separating different classes in feature space and is represented by the equation wx + b = 0 in linear classification.
- Support Vectors: The closest data points to the hyperplane, crucial for determining the hyperplane and margin in SVM.
- Margin: The distance between the hyperplane and the support vectors. SVM aims to maximize this margin for better classification performance.
- Kernel: A function that maps data to a higher-dimensional space enabling SVM to handle non-linearly separable data.
- Hard Margin: A maximum-margin hyperplane that perfectly separates the data without misclassifications.
- Soft Margin: Allows some misclassifications by introducing slack variables, balancing margin maximization and misclassification penalties when data is not perfectly separable.
- C: A regularization term balancing margin maximization and misclassification penalties. A higher C value forces stricter penalty for misclassifications.
- Hinge Loss: A loss function penalizing misclassified points or margin violations and is combined with regularization in SVM.
- Dual Problem: Involves solving for Lagrange multipliers associated with support vectors, facilitating the kernel trick and efficient computation.
How does Support Vector Machine Algorithm Work?
The key idea behind the SVM algorithm is to find the hyperplane that best separates two classes by maximizing the margin between them. This margin is the distance from the hyperplane to the nearest data points (support vectors) on each side.

The best hyperplane also known as the “hard margin” is the one that maximizes the distance between the hyperplane and the nearest data points from both classes. This ensures a clear separation between the classes. So from the above figure, we choose L2 as hard margin. Let’s consider a scenario like shown below:

Here, we have one blue ball in the boundary of the red ball.
How does SVM classify the data?
The blue ball in the boundary of red ones is an outlier of blue balls. The SVM algorithm has the characteristics to ignore the outlier and finds the best hyperplane that maximizes the margin. SVM is robust to outliers.

A soft margin allows for some misclassifications or violations of the margin to improve generalization. The SVM optimizes the following equation to balance margin maximization and penalty minimization:
Objective Function=(1margin)+Ξ»βpenalty Objective Function=(margin1β)+Ξ»βpenalty
The penalty used for violations is often hinge loss which has the following behavior:
- If a data point is correctly classified and within the margin there is no penalty (loss = 0).
- If a point is incorrectly classified or violates the margin the hinge loss increases proportionally to the distance of the violation.
Till now we were talking about linearly separable data that seprates group of blue balls and red balls by a straight line/linear line.
What if data is not linearly separable?
When data is not linearly separable i.e it can’t be divided by a straight line, SVM uses a technique called kernels to map the data into a higher-dimensional space where it becomes separable. This transformation helps SVM find a decision boundary even for non-linear data.

A kernel is a function that maps data points into a higher-dimensional space without explicitly computing the coordinates in that space. This allows SVM to work efficiently with non-linear data by implicitly performing the mapping. For example consider data points that are not linearly separable. By applying a kernel function SVM transforms the data points into a higher-dimensional space where they become linearly separable.
- Linear Kernel: For linear separability.
- Polynomial Kernel: Maps data into a polynomial space.
- Radial Basis Function (RBF) Kernel: Transforms data into a space based on distances between data points.

In this case the new variable y is created as a function of distance from the origin.
Mathematical Computation of SVM
Consider a binary classification problem with two classes, labeled as +1 and -1. We have a training dataset consisting of input feature vectors X and their corresponding class labels Y. The equation for the linear hyperplane can be written as:
wTx+b=0wTx+b=0
Where:
- wwΒ is the normal vector to the hyperplane (the direction perpendicular to it).
- bbΒ is the offset or bias term representing the distance of the hyperplane from the origin along the normal vectorΒ ww.
Distance from a Data Point to the Hyperplane
The distance between a data point xixiβand the decision boundary can be calculated as:
di=wTxi+bβ£β£wβ£β£diβ=β£β£wβ£β£wTxiβ+bβ
where ||w|| represents the Euclidean norm of the weight vector w.
Linear SVM Classifier
Distance from a Data Point to the Hyperplane:
y^={1: wTx+bβ₯00: wTx+b <0y^β={10β: wTx+bβ₯0: wTx+b <0β
Where y^y^β is the predicted label of a data point.
Optimization Problem for SVM
For a linearly separable dataset the goal is to find the hyperplane that maximizes the margin between the two classes while ensuring that all data points are correctly classified. This leads to the following optimization problem:
minimizew,b12β₯wβ₯2w,bminimizeβ21ββ₯wβ₯2
Subject to the constraint:
yi(wTxi+b)β₯1fori=1,2,3,β―,myiβ(wTxiβ+b)β₯1fori=1,2,3,β―,m
Where:
- yiyiββ is the class label (+1 or -1) for each training instance.
- xixiββ is the feature vector for theΒ ii-th training instance.
- mmΒ is the total number of training instances.
The condition yi(wTxi+b)β₯1yiβ(wTxiβ+b)β₯1 ensures that each data point is correctly classified and lies outside the margin.
Soft Margin in Linear SVM Classifier
In the presence of outliers or non-separable data the SVM allows some misclassification by introducing slack variables ΞΆiΞΆiββ. The optimization problem is modified as:
minimize w,b12β₯wβ₯2+Cβi=1mΞΆiw,bminimize β21ββ₯wβ₯2+Cβi=1mβΞΆiβ
Subject to the constraints:
yi(wTxi+b)β₯1βΞΆiandΞΆiβ₯0for i=1,2,β¦,myiβ(wTxiβ+b)β₯1βΞΆiβandΞΆiββ₯0for i=1,2,β¦,m
Where:
- CCΒ is a regularization parameter that controls the trade-off between margin maximization and penalty for misclassifications.
- ΞΆiΞΆiββ are slack variables that represent the degree of violation of the margin by each data point.
Dual Problem for SVM
The dual problem involves maximizing the Lagrange multipliers associated with the support vectors. This transformation allows solving the SVM optimization using kernel functions for non-linear classification.
The dual objective function is given by:
maximize Ξ±12βi=1mβj=1mΞ±iΞ±jtitjK(xi,xj)ββi=1mΞ±iΞ±maximize β21ββi=1mββj=1mβΞ±iβΞ±jβtiβtjβK(xiβ,xjβ)ββi=1mβΞ±iβ
Where:
- Ξ±iΞ±iββ are the Lagrange multipliers associated with theΒ ithithΒ training sample.
- titiββ is the class label for theΒ ithith-th training sample.
- K(xi,xj)K(xiβ,xjβ)Β is the kernel function that computes the similarity between data pointsΒ xixiββ andΒ xjxjββ. The kernel allows SVM to handle non-linear classification problems by mapping data into a higher-dimensional space.
The dual formulation optimizes the Lagrange multipliers Ξ±iΞ±iββ and the support vectors are those training samples where Ξ±i>0Ξ±iβ>0.
SVM Decision Boundary
Once the dual problem is solved, the decision boundary is given by:
w=βi=1mΞ±itiK(xi,x)+bw=βi=1mβΞ±iβtiβK(xiβ,x)+b
Where ww is the weight vector, xx is the test data point and bb is the bias term. Finally the bias term bb is determined by the support vectors, which satisfy:
ti(wTxiβb)=1βb=wTxiβtitiβ(wTxiββb)=1βb=wTxiββtiβ
Where xixiββ is any support vector.
This completes the mathematical framework of the Support Vector Machine algorithm which allows for both linear and non-linear classification using the dual problem and kernel trick.
Types of Support Vector Machine
Based on the nature of the decision boundary, Support Vector Machines (SVM) can be divided into two main parts:
- Linear SVM:Β Linear SVMs use a linear decision boundary to separate the data points of different classes. When the data can be precisely linearly separated, linear SVMs are very suitable. This means that a single straight line (in 2D) or a hyperplane (in higher dimensions) can entirely divide the data points into their respective classes. A hyperplane that maximizes the margin between the classes is the decision boundary.
- Non-Linear SVM:Β Non-Linear SVMΒ can be used to classify data when it cannot be separated into two classes by a straight line (in the case of 2D). By using kernel functions, nonlinear SVMs can handle nonlinearly separable data. The original input data is transformed by these kernel functions into a higher-dimensional feature space where the data points can be linearly separated. A linear SVM is used to locate a nonlinear decision boundary in this modified space.Β
Implementing SVM Algorithm Using Scikit-Learn
We will predict whether cancer is Benign or Malignant using historical data about patients diagnosed with cancer. This data includes independent attributes such as tumor size, texture, and others. To perform this classification, we will use an SVM (Support Vector Machine) classifier to differentiate between benign and malignant cases effectively.
- load_breast_cancer():Β Loads the breast cancer dataset (features and target labels).
- SVC(kernel=”linear”, C=1): Creates a Support Vector Classifier with a linear kernel and regularization parameter C=1.
- svm.fit(X, y):Β Trains the SVM model on the feature matrix X and target labels y.
- DecisionBoundaryDisplay.from_estimator():Β Visualizes the decision boundary of the trained model with a specified color map.
- plt.scatter():Β Creates a scatter plot of the data points, colored by their labels.
- plt.show():Β Displays the plot to the screen.
from sklearn.datasets import load_breast_cancer import matplotlib.pyplot as plt from sklearn.inspection import DecisionBoundaryDisplay from sklearn.svm import SVC cancer = load_breast_cancer() X = cancer.data[:, :2] y = cancer.target svm = SVC(kernel="linear", C=1) svm.fit(X, y) DecisionBoundaryDisplay.from_estimator( svm, X, response_method="predict", alpha=0.8, cmap="Pastel1", xlabel=cancer.feature_names[0], ylabel=cancer.feature_names[1], ) plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolors="k") plt.show()
Output:

Advantages of Support Vector Machine (SVM)
- High-Dimensional Performance: SVM excels in high-dimensional spaces, making it suitable for image classification and gene expression analysis.
- Nonlinear Capability: Utilizing kernel functions like RBF and polynomial SVM effectively handles nonlinear relationships.
- Outlier Resilience: The soft margin feature allows SVM to ignore outliers, enhancing robustness in spam detection and anomaly detection.
- Binary and Multiclass Support: SVM is effective for both binary classification and multiclass classification suitable for applications in text classification.
- Memory Efficiency: It focuses on support vectors making it memory efficient compared to other algorithms.
Disadvantages of Support Vector Machine (SVM)
- Slow Training: SVM can be slow for large datasets, affecting performance in SVM in data mining tasks.
- Parameter Tuning Difficulty: Selecting the right kernel and adjusting parameters like C requires careful tuning, impacting SVM algorithms.
- Noise Sensitivity: SVM struggles with noisy datasets and overlapping classes, limiting effectiveness in real-world scenarios.
- Limited Interpretability: The complexity of the hyperplane in higher dimensions makes SVM less interpretable than other models.
- Feature Scaling Sensitivity: Proper feature scaling is essential, otherwise SVM models may perform poorly.