(5)如果将线性SVM模型看作原始特征的函数,那么它实际上已经不是线性的了。它不是一条直线,而是一个椭圆。
代码:
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
import numpy as np
from sklearn.svm import LinearSVC
from mpl_toolkits.mplot3d import Axes3D,axes3d
数据集引入并使用图像展示
X,y = make_blobs(centers=4,random_state=8)
y = y%2
mglearn.discrete_scatter(X[:,0],X[:,1],y)
plt.xlabel(“Feature 0”)
plt.ylabel(“Feature 1”)
plt.show()
#使用SVM进行分类
linear_svm = LinearSVC().fit(X,y)
mglearn.plots.plot_2d_separator(linear_svm,X)
mglearn.discrete_scatter(X[:,0],X[:,1],y)
plt.xlabel(“Feature 0”)
plt.ylabel(“Feature 1”)
#第二个特征的平方作为一个新的特征,二维变三维
X_new = np.hstack([X,X[:,1:]**2])
figure = plt.figure()
ax = Axes3D(figure, elev=-152, azim