《机器学习(周志华)》笔记--支持向量机(5)--SVM算法实验代码记录:线性SVM、非线性SVM
五、SVM算法实验代码
1、线性SVM
import numpy as np from sklearn.datasets import load_iris import matplotlib.pyplot as plt data = load_iris() x = data.data y = data.target x = x[y<2,:2] y = y[y<2] plt.scatter(x[y==0,0],x[y==0,1],color='red') plt.scatter(x[y==1,0],x[y==1,1],color='blue') plt.legend(labels=['c1','c2'],loc='upper left') plt.xlabel('x') plt.ylabel('y') plt.show()
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() scaler.fit(x) x = scaler.transform(x) from sklearn.svm import LinearSVC svc = LinearSVC(C=0.01) svc.fit(x,y) print(svc.coef_) print(svc.intercept_)
def plot_decision_boundary1(model,axis): x0,x1=np.meshgrid( np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)).reshape(-1,1), np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)).reshape(-1,1) ) x_new=np.c_[x0.ravel(),x1.ravel()] y_predict=model.predict(x_new) zz=y_predict.reshape(x0.shape) from matplotlib.colors import ListedColormap custom_cmap=ListedColormap(['#EF9A9A','#FFF59D','#90CAF9']) plt.contourf(x0,x1,zz,linewidth=5,cmap=custom_cmap) w = model.coef_[0] b = model.intercept_[0] plot_x = np.linspace(axis[0],axis[1],200) up_y = -w[0]/w[1]*plot_x - b/w[1] + 1/w[1] down_y = -w[0]/w[1]*plot_x - b/w[1] - 1/w[1] up_index = (up_y>=axis[2]) & (up_y<=axis[3]) down_index = (down_y>=axis[2]) & (down_y<=axis[3]) plt.plot(plot_x[up_index],up_y[up_index],c='black') plt.plot(plot_x[down_index],down_y[down_index],c='black') plot_decision_boundary1(svc,axis=[-3,3,-3,3]) plt.scatter(x[y==0,0],x[y==0,1],c='r') plt.scatter(x[y==1,0],x[y==1,1],c='b') plt.show()
2、非线性SVM
#-------------异或问题----------------- import numpy as np from sklearn.svm import LinearSVC import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D X = np.array([[0,0,0],[1,1,1],[0,1,0],[1,0,0]]) y = np.array([0,0,1,1]) svc=LinearSVC(C=1e9) svc.fit(X,y) x_plot = np.linspace(0,1,100) y_plot = np.linspace(0,1,100) x_plot,y_plot = np.meshgrid(x_plot,y_plot) A=svc.coef_[0][0] B=svc.coef_[0][1] C=svc.coef_[0][2] D=svc.intercept_[0] z_plot=(-D-A*x_plot-B*y_plot)/C fig = plt.figure(figsize=(12, 8)) ax = Axes3D(fig) ax.plot_surface(x_plot,y_plot,z_plot) ax.scatter([0,1],[0,1],[0,1],c='red',s=100) ax.scatter([0,1],[1,0],[0,0],c='blue',s=100) plt.show() print(svc.coef_) print(svc.intercept_)
#=======非线性通过特征映射转换为线性问题======== import numpy as np from sklearn import datasets import matplotlib.pyplot as plt X,y = datasets.make_moons(noise=0.15,random_state=666) plt.scatter(X[y==0,0],X[y==0,1],c='red') plt.scatter(X[y==1,0],X[y==1,1],c='blue') plt.show() from sklearn.preprocessing import PolynomialFeatures,StandardScaler from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline def PolynomialSVC(degree,C=1.0): return Pipeline([ ('poly',PolynomialFeatures(degree=degree)), ('std_scaler',StandardScaler()), ('linearSVC',LinearSVC(C=1e9)) ]) poly_svc = PolynomialSVC(degree=3) poly_svc.fit(X,y) plot_decision_boundary(poly_svc,axis=[-1.5,2.5,-1.0,1.5]) plt.scatter(X[y==0,0],X[y==0,1],c='red') plt.scatter(X[y==1,0],X[y==1,1],c='blue') plt.show()
#---------------------非线性SVM----------------- from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.pipeline import Pipeline def plot_decision_boundary(model,axis): x0,x1=np.meshgrid( np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)).reshape(-1,1), np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100)).reshape(-1,1) ) x_new=np.c_[x0.ravel(),x1.ravel()] y_predict=model.predict(x_new) zz=y_predict.reshape(x0.shape) from matplotlib.colors import ListedColormap custom_cmap=ListedColormap(['#EF9A9A','#FFF59D','#90CAF9']) plt.contourf(x0,x1,zz,linewidth=5,cmap=custom_cmap) def RBFKernelSVC(gamma=1.0): return Pipeline([ ('std_scaler',StandardScaler()), ('svc',SVC(kernel='rbf',gamma=gamma)) ]) svc = RBFKernelSVC(gamma=100) svc.fit(X,y) plot_decision_boundary(svc,axis=[-1.5,2.5,-1.0,1.5]) plt.scatter(X[y==0,0],X[y==0,1],c='red') plt.scatter(X[y==1,0],X[y==1,1],c='blue') plt.show() import numpy as np from sklearn.svm import LinearSVC X = np.array([[1,1],[1,2],[2,1],[0,0],[1,0],[0,1]]) y = np.array([0,0,0,1,1,1]) svc=LinearSVC(C=1e9) svc.fit(X,y) import numpy as np X0=np.array([[1,1],[1,2],[2,1]]) X1=np.array([[0,0],[1,0],[0,1]]) import matplotlib.pyplot as plt plt.grid() plt.scatter(X0[:,0],X0[:,1],c='red',marker='o',s=100) plt.scatter(X1[:,0],X1[:,1],c='blue',marker='s',s=100) plt.show() poly = PolynomialFeatures(degree=3) X= poly.fit_transform(np.array([[3,4]])) print(X)