f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
202 lines
5.6 KiB
Markdown
202 lines
5.6 KiB
Markdown
---
|
||
id: wiki-2026-0508-kernel-methods-and-svms
|
||
title: Kernel Methods and SVMs
|
||
category: 10_Wiki/Topics
|
||
status: verified
|
||
canonical_id: self
|
||
aliases: [SVM, kernel methods, RBF kernel, kernel trick, support vector machine]
|
||
duplicate_of: none
|
||
source_trust_level: A
|
||
confidence_score: 0.95
|
||
verification_status: applied
|
||
tags: [machine-learning, svm, kernel, classical-ml, scikit-learn]
|
||
raw_sources: []
|
||
last_reinforced: 2026-05-10
|
||
github_commit: pending
|
||
tech_stack:
|
||
language: Python
|
||
framework: scikit-learn / libsvm
|
||
---
|
||
|
||
# Kernel Methods and SVMs
|
||
|
||
## 매 한 줄
|
||
> **"매 implicit feature space 의 의 의 의 inner product"**. 매 kernel trick — 매 high-dim transform 의 explicit X. 매 SVM (Vapnik) 의 의 의 dominant pre-DL. 매 modern: 매 small data 의 still 매 strong baseline. 매 GP 의 covariance 도 kernel.
|
||
|
||
## 매 핵심
|
||
|
||
### 매 SVM
|
||
- **Hard-margin**: 매 separable.
|
||
- **Soft-margin** (slack): 매 misclassify allow.
|
||
- **Dual form**: 매 의 의 의 의 kernel trick.
|
||
|
||
### 매 kernel
|
||
- **Linear**: K(x, y) = x·y.
|
||
- **Polynomial**: (γ x·y + r)^d.
|
||
- **RBF / Gaussian**: exp(-γ ||x-y||²).
|
||
- **Sigmoid**: tanh(γ x·y + r).
|
||
- **String, Graph kernels** (specialized).
|
||
|
||
### 매 응용
|
||
1. **Small-data classification**.
|
||
2. **Text classification** (TF-IDF + linear SVM 의 historically strong).
|
||
3. **Anomaly** (1-class SVM).
|
||
4. **Regression** (SVR).
|
||
5. **Bioinformatics**.
|
||
|
||
## 💻 패턴
|
||
|
||
### Basic SVM (sklearn)
|
||
```python
|
||
from sklearn.svm import SVC
|
||
clf = SVC(kernel='rbf', C=1.0, gamma='scale').fit(X_train, y_train)
|
||
preds = clf.predict(X_test)
|
||
```
|
||
|
||
### Linear SVM (large-scale)
|
||
```python
|
||
from sklearn.svm import LinearSVC
|
||
clf = LinearSVC(C=1.0).fit(X, y) # 매 fast for large N
|
||
```
|
||
|
||
### CV-tune C and gamma
|
||
```python
|
||
from sklearn.model_selection import GridSearchCV
|
||
params = {'C': [0.1, 1, 10, 100], 'gamma': [0.001, 0.01, 0.1, 1]}
|
||
grid = GridSearchCV(SVC(kernel='rbf'), params, cv=5)
|
||
grid.fit(X, y)
|
||
```
|
||
|
||
### SVR (regression)
|
||
```python
|
||
from sklearn.svm import SVR
|
||
model = SVR(kernel='rbf', C=1.0, epsilon=0.1).fit(X, y)
|
||
```
|
||
|
||
### One-class SVM (anomaly)
|
||
```python
|
||
from sklearn.svm import OneClassSVM
|
||
clf = OneClassSVM(gamma='auto').fit(X_normal)
|
||
anomalies = clf.predict(X_test) == -1
|
||
```
|
||
|
||
### Custom kernel
|
||
```python
|
||
def my_kernel(X, Y):
|
||
return X @ Y.T + 1 # 매 example
|
||
|
||
clf = SVC(kernel=my_kernel).fit(X, y)
|
||
```
|
||
|
||
### Kernel trick (manual feature mapping vs)
|
||
```python
|
||
import numpy as np
|
||
def rbf_kernel(x, y, gamma=1.0):
|
||
return np.exp(-gamma * np.linalg.norm(x - y) ** 2)
|
||
|
||
def poly_kernel(x, y, d=2, gamma=1.0, r=0):
|
||
return (gamma * x @ y + r) ** d
|
||
```
|
||
|
||
### TF-IDF + linear SVM (text classic)
|
||
```python
|
||
from sklearn.feature_extraction.text import TfidfVectorizer
|
||
from sklearn.pipeline import Pipeline
|
||
|
||
pipe = Pipeline([('tfidf', TfidfVectorizer(max_features=20000)), ('svm', LinearSVC(C=1.0))])
|
||
pipe.fit(train_texts, train_labels)
|
||
```
|
||
|
||
### Multi-class (one-vs-rest)
|
||
```python
|
||
from sklearn.multiclass import OneVsRestClassifier
|
||
clf = OneVsRestClassifier(SVC()).fit(X, y)
|
||
```
|
||
|
||
### Calibrated probabilities
|
||
```python
|
||
from sklearn.calibration import CalibratedClassifierCV
|
||
clf = CalibratedClassifierCV(SVC(kernel='rbf'), cv=5).fit(X, y)
|
||
probs = clf.predict_proba(X_test)
|
||
```
|
||
|
||
### Kernel approximation (large data)
|
||
```python
|
||
from sklearn.kernel_approximation import RBFSampler
|
||
rbf_feature = RBFSampler(gamma=1, n_components=100, random_state=0)
|
||
X_features = rbf_feature.fit_transform(X)
|
||
# 매 매 linear SVM 의 의 OK
|
||
clf = LinearSVC().fit(X_features, y)
|
||
```
|
||
|
||
### String kernel (text)
|
||
```python
|
||
from sklearn.feature_extraction.text import CountVectorizer
|
||
def n_gram_kernel(X, Y, n=3):
|
||
vec = CountVectorizer(analyzer='char', ngram_range=(n, n))
|
||
Xv = vec.fit_transform(X).toarray()
|
||
Yv = vec.transform(Y).toarray()
|
||
return Xv @ Yv.T
|
||
```
|
||
|
||
### Graph kernel (Weisfeiler-Lehman)
|
||
```python
|
||
from grakel.kernels import WeisfeilerLehman
|
||
from grakel import GraphKernel
|
||
gk = GraphKernel(kernel='weisfeiler_lehman', n_iter=5)
|
||
K_train = gk.fit_transform(graphs_train)
|
||
K_test = gk.transform(graphs_test)
|
||
clf = SVC(kernel='precomputed').fit(K_train, y_train)
|
||
```
|
||
|
||
### Plot decision boundary (2D)
|
||
```python
|
||
import matplotlib.pyplot as plt
|
||
def plot_boundary(clf, X, y):
|
||
h = 0.02
|
||
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
||
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
||
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
|
||
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
|
||
plt.contourf(xx, yy, Z, alpha=0.3)
|
||
plt.scatter(X[:, 0], X[:, 1], c=y)
|
||
```
|
||
|
||
## 매 결정 기준
|
||
| 상황 | Approach |
|
||
|---|---|
|
||
| Small data | RBF SVM |
|
||
| High-dim text | Linear SVM (TF-IDF) |
|
||
| Anomaly | One-class SVM |
|
||
| Graph data | WL graph kernel |
|
||
| Large-scale | Linear SVM or kernel approx |
|
||
| Modern DL data | Use DL instead |
|
||
|
||
**기본값**: 매 small N + tabular = RBF SVM. 매 text = Linear + TF-IDF. 매 large = kernel approximation. 매 modern era — DL win on most.
|
||
|
||
## 🔗 Graph
|
||
- 부모: [[Machine-Learning]]
|
||
- 변형: [[SVM]]
|
||
- 응용: [[Anomaly-Detection]]
|
||
- Adjacent: [[Gaussian-Processes]]
|
||
|
||
## 🤖 LLM 활용
|
||
**언제**: 매 small data. 매 baseline. 매 anomaly.
|
||
**언제 X**: 매 large data (DL win).
|
||
|
||
## ❌ 안티패턴
|
||
- **No scaling**: 매 RBF 의 의 의 critical.
|
||
- **RBF on huge N**: 매 O(N²) 의 fail.
|
||
- **Default C / gamma**: 매 always tune.
|
||
- **No probability calibration**: 매 SVM 의 raw decision 의 not probability.
|
||
|
||
## 🧪 검증 / 중복
|
||
- Verified (Vapnik 1995, Schölkopf, scikit-learn docs).
|
||
- 신뢰도 A.
|
||
|
||
## 🕓 Changelog
|
||
| 날짜 | 변경 |
|
||
|---|---|
|
||
| 2026-05-08 | Phase 1 |
|
||
| 2026-05-10 | Manual cleanup — kernels + 매 SVM / SVR / OCSVM / approx / graph code |
|