SVM (Support Vector Machine) एक शक्तिशाली classification algorithm है जो high-dimensional spaces में भी शानदार performance देता है।
यह algorithm कोशिश करता है कि classes के बीच सबसे चौड़ा margin (boundary) बने।
🔶 क्या है SVM?
SVM एक ऐसा मॉडल है जो अलग-अलग class के data points के बीच सबसे best decision boundary (hyperplane) बनाता है।
🎯 उद्देश्य:
Class 0 और Class 1 को इस तरह से अलग करना कि उनके बीच का फासला (margin) अधिकतम हो।
📊 उदाहरण:
Data Point | Feature 1 | Feature 2 | Class |
---|---|---|---|
A | 2 | 3 | 0 |
B | 4 | 5 | 1 |
C | 3 | 4 | 0 |
SVM इसे इस तरह से classify करता है कि class boundaries के पास के points (Support Vectors) अधिकतम दूर हों।
🧠 महत्वपूर्ण Concepts:
Term | Meaning |
---|---|
Hyperplane | Decision boundary जो classes को अलग करता है |
Margin | Hyperplane से सबसे नजदीक के points तक की दूरी |
Support Vectors | वही points जो margin को define करते हैं |
Kernel Trick | Non-linear data को linear बनाने की तकनीक |
🔄 Linear vs Non-Linear SVM:
Type | Use Case |
---|---|
Linear SVM | जब data साफ़-साफ़ linearly separable हो |
Non-Linear SVM | जब data का pattern complex हो — इसे kernels से solve करते हैं (RBF, Polynomial etc.) |
🔧 Scikit-learn में SVM का Implementation:
✅ Linear SVM (with linearly separable data):
from sklearn.svm import SVC
model = SVC(kernel='linear')
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
✅ Non-Linear SVM (with RBF Kernel):
model = SVC(kernel='rbf') # Gaussian kernel
model.fit(X_train, y_train)
🔬 Kernel Functions:
Kernel | Description |
---|---|
Linear | Straight line boundary |
Polynomial | Polynomial-based curved boundary |
RBF (Gaussian) | Smooth, flexible boundary for complex data |
Sigmoid | Similar to neural network activation |
📊 SVM vs अन्य Algorithms:
Feature | SVM | Decision Tree | Logistic Regression |
---|---|---|---|
Works in High Dim | ✅ Yes | ❌ No | ❌ No |
Handles Non-linearity | ✅ via kernel | ✅ with tuning | ❌ Not naturally |
Explainability | ❌ Difficult | ✅ Yes | ✅ Yes |
Overfitting | ❌ Less prone | ✅ More prone | ✅ Moderate |
✅ Visualization (2D Example with Python)
rom sklearn import datasets
from sklearn.svm import SVC
import matplotlib.pyplot as plt
import numpy as np
# Load dummy data
X, y = datasets.make_blobs(n_samples=100, centers=2, random_state=6)
# Train SVM
clf = SVC(kernel='linear')
clf.fit(X, y)
# Plot
plt.scatter(X[:, 0], X[:, 1], c=y)
ax = plt.gca()
# Plot decision boundary
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xx = np.linspace(xlim[0], xlim[1])
yy = np.linspace(ylim[0], ylim[1])
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.decision_function(xy).reshape(XX.shape)
ax.contour(XX, YY, Z, levels=[-1, 0, 1], linestyles=['--', '-', '--'])
plt.title("SVM Decision Boundary")
plt.show()
📌 कब उपयोग करें SVM?
✅ जब:
- Feature dimension बहुत ज़्यादा हो
- Classes के बीच separation जरूरी हो
- Small dataset हो लेकिन complex pattern हो
📝 Practice Questions:
- SVM का मुख्य उद्देश्य क्या होता है?
- Support Vectors क्या होते हैं और उनका क्या role है?
- Kernel trick कैसे काम करती है?
- Linear और Non-linear SVM में क्या अंतर है?
- Decision Tree और SVM में क्या अंतर है?