SVM绘制二维示意图贺三维示意图

SVM绘制二维示意图贺三维示意图,第1张

背景

        没有图片不够直观,所以需要绘制图片向别人展示

二维情况,比较容易
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.svm import SVC



"""生成样本点"""
centers = [[-1, -1]]
X, labels = make_blobs(n_samples = 1500, centers = centers,cluster_std = 0.5, random_state = 0)  
centers = [[1, 1]]
X1, labels1 = make_blobs(n_samples = 2000, centers = centers,cluster_std = 0.5, random_state = 0) 
labels1 = labels1 +1


def plot_point(dataArr, labelArr, Support_vector_index, W, b):
    for i in range(np.shape(dataArr)[0]):
        if labelArr[i] == 0:
            plt.scatter(dataArr[i][0], dataArr[i][1], c='b', s=20,marker='D')
        else:
            plt.scatter(dataArr[i][0], dataArr[i][1], c='y', s=20,marker="*")

    for j in Support_vector_index:
        plt.scatter(dataArr[j][0], dataArr[j][1], s=100, c='r', alpha=0.5, linewidth=1.5, edgecolor='red')

    x = np.arange(-3, 3, 0.01)
    y = (W[0][0] * x + b) / (-1 * W[0][1])
    plt.scatter(x, y, s=5, marker='h')
    plt.show()

clf = SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
          decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
          max_iter=-1, probability=False, random_state=None, shrinking=True,
          tol=0.001, verbose=False)
# fit训练数据
dataArr = np.r_[X,X1]
labelArr = np.r_[labels,labels1]
clf.fit(dataArr, labelArr)

# 获取模型返回值
n_Support_vector = clf.n_support_  # 支持向量个数
Support_vector_index = clf.support_  # 支持向量索引
W = clf.coef_  # 方向向量W
b = clf.intercept_  # 截距项b
# 绘制分类超平面
plt.figure(num=3,figsize=(8, 6))
plot_point(dataArr, labelArr, Support_vector_index, W, b)

三维版本
import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.svm import SVC


"""生成样本点"""
plt.figure(num=1,figsize=(8, 6))

centers = [[3,0,0]]
X, labels = make_blobs(n_samples = 1500, centers = centers,cluster_std = 0.5, random_state = 0)  
ax1 = plt.axes(projection='3d')
ax1.scatter3D(X[:, 0], X[:, 1],X[:, 2], 'r',marker='o',label='label_1')

centers = [[3, 3, 2]]
X1, labels1 = make_blobs(n_samples = 2000, centers = centers,cluster_std = 0.5, random_state = 0)  
ax1.scatter3D(X1[:, 0], X1[:, 1],X1[:, 2], 'g',marker='*',label='label_2')
labels1 = labels1 +1

centers = [[0, 6, 4]]
X2, labels2 = make_blobs(n_samples = 2000, centers = centers,cluster_std = 0.5, random_state = 0)  
ax1.scatter3D(X2[:, 0], X2[:, 1],X2[:, 2], 'b',marker='^',label='label_3')
labels2 = labels2 +2


clf = SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
          decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
          max_iter=-1, probability=False, random_state=None, shrinking=True,
          tol=0.001, verbose=False)
# fit训练数据
dataArr = np.r_[X,X1,X2]
labelArr = np.r_[labels,labels1,labels2]
clf.fit(dataArr, labelArr)
#计算超平面方程wx+b=0
b=clf.intercept_
w=clf.coef_
xx = np.arange(-2,6)
yy = np.arange(0,6)
X, Y = np.meshgrid(xx, yy)
Z1= -w[0,0]/w[0,2]*X-w[0,1]/w[0,2]*Y-b[0]/w[0,2]
Z2 = -w[1,0]/w[1,2]*X-w[1,1]/w[1,2]*Y-b[1]/w[1,2]
Z3 = -w[2,0]/w[2,2]*X-w[2,1]/w[2,2]*Y-b[2]/w[2,2]

ax1.plot_surface(X,Y,Z1,alpha=0.6)
# ax.plot_surface(X,Y,Z2,alpha=0.6,)
ax1.plot_surface(X,Y,Z3,alpha=0.6)
#设置图例
ax1.legend(loc='best')

plt.show()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/916991.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存