- 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 303. 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 304. 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 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- 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
Random Blogs
- AI is Replacing Search Engines: The Future of Online Search
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Variable Assignment in Python
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Top 10 Knowledge for Machine Learning & Data Science Students
- Understanding SQL vs MySQL vs PostgreSQL vs MS SQL vs Oracle and Other Popular Databases
- SQL vs MySQL: The Ultimate Guide for Beginners
- How to Become a Good Data Scientist ?
- How to Install Tableau and Power BI on Ubuntu Using VirtualBox
- Generative AI - The Future of Artificial Intelligence
- Data Analytics: The Power of Data-Driven Decision Making
- Loan Default Prediction Project Using Machine Learning
- AI in Marketing & Advertising: The Future of AI-Driven Strategies
- 15 Amazing Keyword Research Tools You Should Explore
- 5 Ways Use Jupyter Notebook Online Free of Cost
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- 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


