Search code examples
pythonmatplotlibscikit-learnsvmscatter-plot

ValueError: x and y must be the same size when try to draw the SVM


I am new at machine learning , I found python code to visulastion the result of SVM modle from sklearn in python the code is

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets.samples_generator import make_blobs

X,y=make_blobs(n_samples=40,centers=2,random_state=20)

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

plt.scatter(X[:,0],X[:,1],c=y,s=30,cmap=plt.cm.Paired)

### assign new data
newData=[[3,4],[5,6]]

#print(clf.predict(newData))
#plot the deciston function
ax=plt.gca()
xlim=ax.get_xlim()
ylim=ax.get_ylim()

#creat a grid to evalute the modle
xx=np.linspace(xlim[0],xlim[1],30)
yy=np.linspace(ylim[0],ylim[1],30)
YY,XX=np.meshgrid(yy,xx)
xy=np.vstack([XX.ravel(),YY.ravel()]).T
Z=clf.decision_function(xy).reshape(XX.shape)

#plot decision bundray and margins
ax.contour(XX,YY,Z,colors='k',levels=[-1,0,1],alpha=0.5,linestyles=['--','-','--'])
# plot support vector
ax.scatter(clf.support_vectors_[:0],clf.support_vectors_[:1],s=100,linewidths=1,facecolors='none')
plt.show()

when I run above code i got this error :

File "C:/Users/Black_Swan/PycharmProjects/test/images/svm.py", line 47, in ax.scatter(clf.support_vectors_[:0],clf.support_vectors_[:1],s=100,linewidths=1,facecolors='none') File "C:\Python27\lib\site-packages\matplotlib_init_.py", line 1870, in inner return func(ax, *args, **kwargs) File "C:\Python27\lib\site-packages\matplotlib\axes_axes.py", line 4257, in scatter raise ValueError("x and y must be the same size") ValueError: x and y must be the same size

Can anyone help me find the mistake?


Solution

  • clf.support_vectors_ is a two dimensional array, so you need to do clf.support_vectors_[:,0] . Not very sure about your matplotlib version, I am on '3.4.1' so the below solution works for that version:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import svm
    from sklearn.datasets import make_blobs
    
    X,y=make_blobs(n_samples=40,centers=2,random_state=20)
    
    clf=svm.SVC(kernel='linear',C=1000)
    clf.fit(X,y)
    
    fig,ax = plt.subplots()
    ax.scatter(X[:,0],X[:,1],c=y,s=20,cmap=plt.cm.Paired)
    
    newData=[[3,4],[5,6]]
    
    ax=plt.gca()
    xlim=ax.get_xlim()
    ylim=ax.get_ylim()
    
    #creat a grid to evalute the modle
    xx=np.linspace(xlim[0],xlim[1],30)
    yy=np.linspace(ylim[0],ylim[1],30)
    YY,XX=np.meshgrid(yy,xx)
    xy=np.vstack([XX.ravel(),YY.ravel()]).T
    Z=clf.decision_function(xy).reshape(XX.shape)
    
    ax.contour(XX,YY,Z,colors='k',levels=[-1,0,1],alpha=0.5,linestyles=['--','-','--'])
    ax.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],marker='o',
               edgecolors="black",s=90,c="None",linewidths=2)
    

    enter image description here