目录
代码:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_circles
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
# 生成线性不可分的虚拟数据集
X, y = make_circles(n_samples=100, noise=0.1, factor=0.5, random_state=42)
# 绘制数据集
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')
plt.title("Non-linearly separable dataset")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
# 利用多项式特征进行分类
poly_svm = Pipeline([
("scaler", StandardScaler()),
("poly_features", PolynomialFeatures(degree=3)),
("svm_clf", SVC(kernel="linear", C=10))
])
poly_svm.fit(X, y)
# 绘制决策边界
def plot_decision_boundary(model, X, y, title):
h = .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 = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o', s=100, linewidth=1, cmap=plt.cm.Paired)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title(title)
plt.show()
plot_decision_boundary(poly_svm, X, y, 'SVM with Polynomial Features')
# 利用高斯核函数进行分类
rbf_svm = SVC(kernel="rbf", gamma=5, C=0.001)
rbf_svm.fit(X, y)
# 绘制决策边界
plot_decision_boundary(rbf_svm, X, y, 'SVM with RBF Kernel')