簡體   English   中英

這是可視化SVM決策邊界的代碼。 我無法發現錯誤。 請看一看

[英]Here is the code to visualize SVM decision boundary. I am unable to spot the error. Please have a look

**注意:變量x包含5個維度的特征向量的30個元組。 這些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

該錯誤是很直截了當的,您正在嘗試分配過多的內存。 使用大於0.1的步長,調查矩陣的大小,可能您在xx,yy中生成了很大的一個。 此外-為什么循環中的所有內容都圍繞樣本? 您似乎打了30次電話,這似乎不太合理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM