绘制分类器的决策边界,值错误:X每个样本有2个特征;应为908430”

2024-04-27 01:04:24 发布

您现在位置:Python中文网/ 问答频道 /正文

基于scikit学习文档http://scikit-learn.org/stable/auto_examples/svm/plot_iris.html#sphx-glr-auto-examples-svm-plot-iris-py。 我试图绘制分类器的决策边界,但它发送了一个错误消息“ValueError:X每个样本有2个特性;预期值为908430”,代码为“Z=预测clf(np.c_[二十、拉威尔(), yy.拉威尔()])”

clf = SGDClassifier().fit(step2, index)  
X=step2
y=index
h = .02
colors = "bry"
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                 np.arange(y_min, y_max, h))

Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.axis('off')

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)

“索引”是一个标签,包含注释的大约[98579 X 1]标签,包括正面、自然和负面

^{pr2}$

“step2”是由Countvectorizer函数形成的[98579x908430]numpy矩阵,它是关于注释数据的

<98579x908430 sparse matrix of type '<type 'numpy.float64'>'
with 3168845 stored elements in Compressed Sparse Row format>

Tags: irisautoindexplotnppltscikitmin
1条回答
网友
1楼 · 发布于 2024-04-27 01:04:24

问题是,对于非2维的数据,不能为分类器绘制决策边界。你的数据显然是高维的,它有908430维(我假设是NLP任务)。对于这样的模型,没有办法绘制实际的决策边界。您正在使用的示例是基于2D数据(简化虹膜)进行训练的,这是他们能够绘制它的唯一原因。在

相关问题 更多 >