# ML使用SVM对非线性数据集执行分类

2021年5月4日22:58:16 发表评论 903 次浏览

``````# importing libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_circles
from mpl_toolkits.mplot3d import Axes3D

# generating data
X, Y = make_circles(n_samples = 500 , noise = 0.02 )

# visualizing data
plt.scatter(X[:, 0 ], X[:, 1 ], c = Y, marker = '.' )
plt.show()``````

``````# adding a new dimension to X
X1 = X[:, 0 ].reshape(( - 1 , 1 ))
X2 = X[:, 1 ].reshape(( - 1 , 1 ))
X3 = (X1 * * 2 + X2 * * 2 )
X = np.hstack((X, X3))

# visualizing data in higher dimension
fig = plt.figure()
axes = fig.add_subplot( 111 , projection = '3d' )
axes.scatter(X1, X2, X1 * * 2 + X2 * * 2 , c = Y, depthshade = True )
plt.show()``````

``````# create support vector classifier using a linear kernel
from sklearn import svm

svc = svm.SVC(kernel = 'linear' )
svc.fit(X, Y)
w = svc.coef_
b = svc.intercept_

# plotting the separating hyperplane
x1 = X[:, 0 ].reshape(( - 1 , 1 ))
x2 = X[:, 1 ].reshape(( - 1 , 1 ))
x1, x2 = np.meshgrid(x1, x2)
x3 = - (w[ 0 ][ 0 ] * x1 + w[ 0 ][ 1 ] * x2 + b) /w[ 0 ][ 2 ]

fig = plt.figure()
axes2 = fig.add_subplot( 111 , projection = '3d' )
axes2.scatter(X1, X2, X1 * * 2 + X2 * * 2 , c = Y, depthshade = True )
axes1 = fig.gca(projection = '3d' )
axes1.plot_surface(x1, x2, x3, alpha = 0.01 )
plt.show()``````