SVM(核支持向量機(jī))是一種監(jiān)督學(xué)習(xí)模型,是可以推廣到更復(fù)雜模型的擴(kuò)展,這些模型無(wú)法被輸入空間的超平面定義。
線模型在低維空間中可能非常受限,因?yàn)榫€和平面的靈活性有限,但是有一種方式可以讓線性模型更加靈活,那就是添加更多特征,比如輸入特征的交互式或多項(xiàng)式。
以下面的數(shù)據(jù)集為例:
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
X,y=make_blobs(centers=4,random_state=8)
y=y%2
line_svc=LinearSVC().fit(X,y)
mglearn.plots.plot_2d_separator(line_svc,X)
mglearn.discrete_scatter(X[:,0],X[:,1],y)
plt.xlabel('特征0')
plt.ylabel('特征1')
plt.show()
用于分類的線性模型只能用一條直線來(lái)劃分?jǐn)?shù)據(jù)點(diǎn),對(duì)這個(gè)數(shù)據(jù)集無(wú)法給出較好的結(jié)果。
現(xiàn)在,對(duì)輸入特征進(jìn)行擴(kuò)展,比如添加一個(gè)特征的平方作為一個(gè)新特征,那么每個(gè)數(shù)據(jù)點(diǎn)可以表示為三維點(diǎn),而不是二維點(diǎn),這樣就可以做一個(gè)新的三維散點(diǎn)圖:
import numpy as np
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from mpl_toolkits.mplot3d import Axes3D,axes3d
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
X,y=make_blobs(centers=4,random_state=8)
y=y%2
#line_svc=LinearSVC().fit(X,y)
X_new=np.hstack([X,X[:,1:]**2])
figure=plt.figure()
#3D可視化
ax=figure.add_subplot(projection='3d')
#首先畫出所有y==0,然后畫出所有y==1的點(diǎn)
mask=y==0
ax.scatter(X_new[mask,0],X_new[mask,1],X_new[mask,2],c='blue',marker='o',cmap=mglearn.cm2,s=60)
ax.scatter(X_new[~mask,0],X_new[~mask,1],X_new[~mask,2],c='red',marker='^',cmap=mglearn.cm2,s=60)
ax.set_xlabel('特征0')
ax.set_ylabel('特征1')
ax.set_zlabel('特征1**2')
plt.show()
?
在數(shù)據(jù)新的可視化中,可以用線性模型(三維平面將這兩個(gè)類別區(qū)分開)
import numpy as np
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from mpl_toolkits.mplot3d import Axes3D,axes3d
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
X,y=make_blobs(centers=4,random_state=8)
y=y%2
X_new=np.hstack([X,X[:,1:]**2])
line_svc_3d=LinearSVC().fit(X_new,y)
coef,intercept=line_svc_3d.coef_.ravel(),line_svc_3d.intercept_
figure=plt.figure()
#3D可視化
ax=figure.add_subplot(projection='3d')
#首先畫出所有y==0,然后畫出所有y==1的點(diǎn)
xx=np.linspace(X_new[:,0].min()-2,X_new[:,0].max()+2,50)
yy=np.linspace(X_new[:,1].min()-2,X_new[:,1].max()+2,50)
XX,YY=np.meshgrid(xx,yy)
ZZ=(coef[0]*XX+coef[1]*YY+intercept)/-coef[2]
mask=y==0
ax.plot_surface(XX,YY,ZZ,rstride=8,cstride=8,alpha=0.3)
ax.scatter(X_new[mask,0],X_new[mask,1],X_new[mask,2],c='blue',marker='o',cmap=mglearn.cm2,s=60)
ax.scatter(X_new[~mask,0],X_new[~mask,1],X_new[~mask,2],c='red',marker='^',cmap=mglearn.cm2,s=60)
ax.set_xlabel('特征0')
ax.set_ylabel('特征1')
ax.set_zlabel('特征1**2')
plt.show()
如果將線性SVM模型看做原始特征的函數(shù),那么它實(shí)際上已經(jīng)不是線性的了,它不再是一條直線,而是一個(gè)橢圓:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-811201.html
import numpy as np
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from mpl_toolkits.mplot3d import Axes3D,axes3d
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
X,y=make_blobs(centers=4,random_state=8)
y=y%2
X_new=np.hstack([X,X[:,1:]**2])
line_svc_3d=LinearSVC().fit(X_new,y)
coef,intercept=line_svc_3d.coef_.ravel(),line_svc_3d.intercept_
xx=np.linspace(X_new[:,0].min()-2,X_new[:,0].max()+2,50)
yy=np.linspace(X_new[:,1].min()-2,X_new[:,1].max()+2,50)
XX,YY=np.meshgrid(xx,yy)
ZZ=YY**2
dec=line_svc_3d.decision_function(np.c_[XX.ravel(),YY.ravel(),ZZ.ravel()])
plt.contourf(XX,YY,dec.reshape(XX.shape),levels=[dec.min(),0,dec.max()],cmap=mglearn.cm2,alpha=0.5)
mglearn.discrete_scatter(X[:,0],X[:,1],y)
plt.xlabel('特征0')
plt.ylabel('特征1')
plt.show()
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-811201.html
到了這里,關(guān)于【Python機(jī)器學(xué)習(xí)】SVM——線性模型與非線性特征的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!