在 SVM scikit-learn 中可视化 2D/3D 决策面 [英] Visualize 2D / 3D decision surface in SVM scikit-learn

查看:61
本文介绍了在 SVM scikit-learn 中可视化 2D/3D 决策面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让 sklearn svm 分类器工作.我只是将 2 个选项分类为 0 或 1使用特征向量.它工作正常.

我想使用图表在

案例 2:3 个特征的 3D 绘图并使用 iris 数据集

from sklearn.svm import SVC将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt从 sklearn 导入支持向量机,数据集从 mpl_toolkits.mplot3d 导入 Axes3D虹膜 = datasets.load_iris()X = iris.data[:, :3] # 我们只取前三个特征.Y = 虹膜.目标#使它成为二分类问题X = X[np.logical_or(Y==0,Y==1)]Y = Y[np.logical_or(Y==0,Y==1)]模型 = svm.SVC(kernel='线性')clf = model.fit(X, Y)# 分离平面的方程由所有 x 给出,因此 np.dot(svc.coef_[0], x) + b = 0.# 求解 w3 (z)z = lambda x,y: (-clf.intercept_[0]-clf.coef_[0][0]*x -clf.coef_[0][1]*y)/clf.coef_[0][2]tmp = np.linspace(-5,5,30)x,y = np.meshgrid(tmp,tmp)fig = plt.figure()ax = fig.add_subplot(111, 投影='3d')ax.plot3D(X[Y==0,0], X[Y==0,1], X[Y==0,2],'ob')ax.plot3D(X[Y==1,0], X[Y==1,1], X[Y==1,2],'sr')ax.plot_surface(x, y, z(x,y))ax.view_init(30, 60)plt.show()

I made sklearn svm classifier work. I simply classify 2 options 0 or 1 using feature vectors. It works fine.

I want to visualize it on page using graphs.

Problem is that my vector is 512 item length, so hard to show on x,y graph.

Is there any way to visualize classification hyperplane for a long vector of features like 512?

解决方案

You cannot visualize the decision surface for a lot of features. This is because the dimensions will be too many and there is no way to visualize an N-dimensional surface.

However, you can use 2 features and plot nice decision surfaces as follows.

I have also written an article about this here: https://towardsdatascience.com/support-vector-machines-svm-clearly-explained-a-python-tutorial-for-classification-problems-29c539f3ad8?source=friends_link&sk=80f72ab272550d76a0cc3730d7c8af35

Case 1: 2D plot for 2 features and using the iris dataset

from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features.
y = iris.target

def make_meshgrid(x, y, h=.02):
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    return xx, yy

def plot_contours(ax, clf, xx, yy, **params):
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out

model = svm.SVC(kernel='linear')
clf = model.fit(X, y)

fig, ax = plt.subplots()
# title for the plots
title = ('Decision surface of linear SVC ')
# Set-up grid for plotting.
X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
ax.set_ylabel('y label here')
ax.set_xlabel('x label here')
ax.set_xticks(())
ax.set_yticks(())
ax.set_title(title)
ax.legend()
plt.show()

Case 2: 3D plot for 3 features and using the iris dataset

from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from mpl_toolkits.mplot3d import Axes3D

iris = datasets.load_iris()
X = iris.data[:, :3]  # we only take the first three features.
Y = iris.target

#make it binary classification problem
X = X[np.logical_or(Y==0,Y==1)]
Y = Y[np.logical_or(Y==0,Y==1)]

model = svm.SVC(kernel='linear')
clf = model.fit(X, Y)

# The equation of the separating plane is given by all x so that np.dot(svc.coef_[0], x) + b = 0.
# Solve for w3 (z)
z = lambda x,y: (-clf.intercept_[0]-clf.coef_[0][0]*x -clf.coef_[0][1]*y) / clf.coef_[0][2]

tmp = np.linspace(-5,5,30)
x,y = np.meshgrid(tmp,tmp)

fig = plt.figure()
ax  = fig.add_subplot(111, projection='3d')
ax.plot3D(X[Y==0,0], X[Y==0,1], X[Y==0,2],'ob')
ax.plot3D(X[Y==1,0], X[Y==1,1], X[Y==1,2],'sr')
ax.plot_surface(x, y, z(x,y))
ax.view_init(30, 60)
plt.show()

这篇关于在 SVM scikit-learn 中可视化 2D/3D 决策面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆