- Supervised Learning
-
Overview
- Introduction to Supervised Learning
- Linear Regression and Its Applications
- Logistic Regression for Classification
- Decision Trees and Random Forests
- Support Vector Machines (SVM)
- K-Nearest Neighbors (KNN) Algorithm
- Naïve Bayes Classifier
- Gradient Boosting (XGBoost, LightGBM)
- Overfitting and Underfitting in Models
- Bias-Variance Tradeoff
Support Vector Machines (SVM)
Add to BookmarkSupport Vector Machines (SVM) are powerful supervised learning models used for classification and regression tasks. They work by finding the optimal hyperplane that separates data points of different classes with the maximum margin.
Why Use SVM?
- Works well in high-dimensional spaces.
- Effective when the number of dimensions exceeds the number of samples.
- Robust to overfitting, especially in high-dimensional space.
1. How SVM Works
SVM tries to find the best boundary (hyperplane) that separates classes in the feature space. It focuses on the data points that are closest to the decision boundary — these are called support vectors. The algorithm then maximizes the margin between these support vectors.
- Linear SVM: Works when the classes are linearly separable.
- Kernel SVM: Used for non-linear separation using kernel tricks like RBF or polynomial.
2. Basic Example (Linear SVM)
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report
# Load dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Binary classification: Class 0 vs Class 1
X = X[y != 2]
y = y[y != 2]
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Train SVM model
model = SVC(kernel='linear')
model.fit(X_train, y_train)
# Predict and evaluate
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))
Output-
precision recall f1-score support
0 1.00 1.00 1.00 17
1 1.00 1.00 1.00 13
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
3. Example with Non-linear Kernel (RBF)
# Train with RBF kernel
model_rbf = SVC(kernel='rbf', gamma='scale')
model_rbf.fit(X_train, y_train)
# Predict and evaluate
y_pred_rbf = model_rbf.predict(X_test)
print(classification_report(y_test, y_pred_rbf))
Output-
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
4. Key Parameters of SVC
kernel
: Specifies the kernel type –'linear'
,'poly'
,'rbf'
,'sigmoid'
.C
: Regularization parameter. Smaller values specify a softer margin.gamma
: Defines how far the influence of a single training example reaches.
5. When to Use SVM
- High-dimensional datasets (e.g., text classification).
- Small-to-medium-sized datasets.
- Binary classification problems.
- When a clear margin of separation exists.
6. Limitations
- Not suitable for very large datasets due to high training time.
- Less effective when classes overlap significantly.
- Model tuning (kernel, C, gamma) can be complex.
Summary
Support Vector Machines are highly effective for classification problems, especially when the data is high-dimensional or has a clear margin of separation. With kernel tricks, SVM can even classify non-linear datasets.
Prepare for Interview
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
Random Blogs
- Top 15 Recommended SEO Tools
- Store Data Into CSV File Using Python Tkinter GUI Library
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Robotics & AI – How AI is Powering Modern Robotics
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Variable Assignment in Python
- Top 10 Knowledge for Machine Learning & Data Science Students
- Important Mistakes to Avoid While Advertising on Facebook
- 10 Awesome Data Science Blogs To Check Out
- Time Series Analysis on Air Passenger Data
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Types of Numbers in Python
- 15 Amazing Keyword Research Tools You Should Explore
- How to Start Your Career as a DevOps Engineer
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset