首页 文章

这是可视化SVM决策边界的代码 . 我无法发现错误 . 请看一看

提问于
浏览
0

注意:变量x包含30个5维特征向量元组 . 这些x的值被转移到x_train.x可以想象成x = [[1.0,2.0,3,0,4.0,5.0],[11.0,12.0,13.0,14.0,15.0],[21.0, 22.0,23.0,24.0,25,0],.. .. ..]和y =标签= [1,1,1,2,2,2,3,3,3 ...]我希望申请PCA在x上并减少到两个维度,然后绘制决策边界 . 我可以绘制积分,但无法绘制决策边界

x_train = x 
y_train =labels 
pca = PCA(n_components=2).fit(x_train) 
pca_2d = pca.transform(x_train)
clf = svm.SVC(kernel='linear',C = 3)
clf.fit(pca_2d, y_train)
for i in range(1, pca_2d.shape[0]):
    if y_train[i] == 1:
     c1 = pl.scatter(pca_2d[i,0],pca_2d[i,1],c='r',    s=50,marker='+')
    elif y_train[i] == 2:
     c2 = pl.scatter(pca_2d[i,0],pca_2d[i,1],c='r',    s=50,marker='.')
    elif y_train[i] == 3:
     c3 = pl.scatter(pca_2d[i,0],pca_2d[i,1],c='r',    s=50,marker=',')
    elif y_train[i] == 4:
     c4 = pl.scatter(pca_2d[i,0],pca_2d[i,1],c='r',    s=50,marker='^')
    elif y_train[i] == 5:
     c5 = pl.scatter(pca_2d[i,0],pca_2d[i,1],c='r',    s=50,marker='v')
    elif y_train[i] == 6:

    x_min, x_max = pca_2d[:, 0].min() - 1,   pca_2d[:,0].max() + 1
    y_min, y_max = pca_2d[:, 1].min() - 1,   pca_2d[:, 1].max() + 1

    xx, yy = np.meshgrid(np.arange(x_min, x_max,  .01),np.arange(y_min,y_max, .01))  
    #************ ERROR ******#
    Z = clf.predict(np.c_[xx.ravel(),  yy.ravel()]) 
    #************ ERROR ******#
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
    pl.title('Support Vector Machine Decision Surface')
    pl.axis('off')
    pl.show()





##  The error shown is :
    Traceback (most recent call last):
    File "D:\New folder_previous.2 - Copy.right\main_pos.py", line 354, in   <module>
    Z = clf.predict(np.c_[xx.ravel(),  yy.ravel()])
    File "C:\Python27\lib\site-packages\numpy\lib\index_tricks.py", line   338, in __getitem__
    res = _nx.concatenate(tuple(objs), axis=self.axis)
    MemoryError

1 回答

  • 0

    错误非常严重,你试图分配太多内存 . 使用大于0.1的步长,研究矩阵的大小,可能是你用xx,yy生成了一个大的 . 此外 - 为什么循环中的所有内容都超过样本?你好像在说这30次,这似乎不是一个合理的想法 .

相关问题