분류경계면을 찾을 때 Generalization(일반화)가 잘 이뤄지는지, 즉 다시 말해 Validation error가 최소인지를 확인해봐야 한다.
SVM의 주의할 점은 아래와 같다.
💡 Hard Margin: 두 클래스가 하나의 선으로 완벽하게 나눠지는 경우에만 적용 가능함
💡 Soft Margin: 일부 샘플들이 분류 경계선의 분류 결과에 반하는 경우를 일정 수준 허용하는 방법
scaler = StandardScaler()
svm_clf1 = LinearSVC(C=1, loss="hinge", random_state=42)
svm_clf2 = LinearSVC(C=100, loss="hinge", random_state=42)
scaled_svm_clf1 = Pipeline([
("scaler", scaler),
("linear_svc", svm_clf1),
])
scaled_svm_clf2 = Pipeline([
("scaler", scaler),
("linear_svc", svm_clf2),
])
scaled_svm_clf1.fit(X, y)
scaled_svm_clf2.fit(X, y)
📍 Polynominal Kernel: 다항식의 차수를 조절할 수 있는 효율적인 계산 방법
📍 Guassian RBF Kernel: 무한대 차수를 갖는 다항식으로 차원을 확장시키는 효과
👉 C 페널티와 더불어 함께 써준다면 좋은 성능을 낼 수 있다.
from sklearn.svm import SVC # SVM Classifier
from sklearn import datasets
# iris데이터
iris = datasets.load_iris()
X = iris["data"][:, (2, 3)] # petal length, petal width
y = iris["target"]
# 클래스구분
setosa_or_versicolor = (y == 0) | (y == 1)
X = X[setosa_or_versicolor]
y = y[setosa_or_versicolor]
# SVM Classifier model
# kernel="linear": 특별한 패턴을 고려하지 않고 linear한 classification을 만들겠다
# C=float("inf"): hard margins classifier를 학습하고자 한다.
svm_clf = SVC(kernel="linear", C=float("inf"))
svm_clf.fit(X, y)
# Non-linear clafssification
# **Polynominal Kernel로 그릴 수 있음
from sklearn.svm import SVC
poly_kernel_svm_clf = Pipeline([
("scaler", StandardScaler()),
("svm_clf", SVC(kernel="poly", degree=3, coef0=1, C=5))
])
poly100_kernel_svm_clf = Pipeline([
("scaler", StandardScaler()),
("svm_clf", SVC(kernel="poly", degree=10, coef0=100, C=5))
])
fig, axes = plt.subplots(ncols=2, figsize=(10.5, 4), sharey=True)
plt.sca(axes[0])
plot_predictions(poly_kernel_svm_clf, [-1.5, 2.45, -1, 1.5])
plot_dataset(X, y, [-1.5, 2.4, -1, 1.5])
plt.title(r"$d=3, r=1, C=5$", fontsize=18)
plt.sca(axes[1])
plot_predictions(poly100_kernel_svm_clf, [-1.5, 2.45, -1, 1.5])
plot_dataset(X, y, [-1.5, 2.4, -1, 1.5])
plt.title(r"$d=10, r=100, C=5$", fontsize=18)
plt.ylabel("")
save_fig("moons_kernelized_polynomial_svc_plot")
plt.show()**
# regression
from sklearn.svm import LinearSVR
svm_reg = LinearSVR(epsilon=1.5, random_state=42)
svm_reg.fit(X, y)
[머신러닝] Ensemble, Bagging, Boosting (0) | 2022.11.24 |
---|---|
[KMOOC-실습으로배우는머신러닝] 7.Ensemble Learning (0) | 2022.11.22 |
[KMOOC-실습으로배우는머신러닝]4. Machine Learning with Optimization: Gradient Descent, Learning rate, Stochastic Gradient Descent, Momentum (0) | 2022.11.22 |
[KMOOC-실습으로배우는머신러닝]2. Machine Learning Pipeline (0) | 2022.11.21 |
[KMOOC-실습으로배우는머신러닝] 1. Introduction to Machine Learning (0) | 2022.11.21 |