svm predict ValueError: X.shape[1] = 2 should be e

2019-07-29 08:05发布

I am confused as what's wrong and how to approach fixing it:

def plot_boundary(my_svm, xmin, xmax, ymin, ymax):
    """
    Function to plot the decision boundary for a trained SVM
    It works by making a grid of x1 ("xvals") and x2 ("yvals") points,
    And for each, compute whether the SVM classifies that point as
    True or False. Then, a contour is drawn with a built-in pyplot function.
    """
    # Hint: Use the following method built into the svm module
    # my_svm.predict(np.array([x1[i],x2[j]]))
    #np.array([xvals[x]), np.array(yvals[y]])
    h = 0.02 
    #xx = np.linspace(xmin, xmax, 0.02)
    #yy = np.linspace(ymin, ymax, 0.02)
    xx, yy = np.meshgrid(np.arange(xmin, xmax, h), np.arange(ymin, ymax, h))
    #xx, yy = np.mgrid[xmin:xmax:200j, ymin:ymax:200j]

    Z = my_svm.predict(np.c_[xx.ravel(), yy.ravel()])
    #Z = my_svm.predict(np.array(Xval[xx]), np.array(yvals[yy]))
    #Z = my_svm.predict(Xval, yval)

    #Z = my_svm.decision_function(np.c_[xx.ravel(), yy.ravel()])
    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    #plt.contour(xx, yy, Z)
    color_map = plt.cm.RdBu
    #ax.contourf(xx, yy, Z, cmap=color_map, alpha=.8)

    plt.contour(xx, yy, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'], levels=[-.5, 0, .5])

    plt.show(block=False)
    #my_svm.predict(np.array([Xvals[x],yvals[y]]))
    #contour = plt.contour(x1,x2, mtrx , levels=[0] )
    #contour = plt.contour( )
    plt.title("Decision Boundary")

and then I call it with:

plot_data()
plot_boundary(clf,-.5,.3,-.8,.6)

In which svm is:

from sklearn import svm

def gauss_kernel(x1, x2, gamma):
    x1 = x1.flatten()
    x2 = x2.flatten()
    sigma = math.sqrt(gamma) 
    return np.exp(-np.sum((x1-x2)**2)/(2*sigma**2))



# from @lejlot http://stackoverflow.com/a/26962861/583834
def gaussianKernelGramMatrix(X1, X2, K_function=gauss_kernel, gamma=0.1):
    """(Pre)calculates Gram Matrix K"""

    gram_matrix = np.zeros((X1.shape[0], X2.shape[0]))
    for i, x1 in enumerate(X1):
        for j, x2 in enumerate(X2):
            gram_matrix[i, j] = K_function(x1, x2, gamma)
    return gram_matrix

gamma=0.1
y = y.flatten()

clf = svm.SVC(kernel="precomputed", verbose=2, C=2.0)
clf.fit(gaussianKernelGramMatrix(X,X, gauss_kernel, gamma=gamma), y)

As you see I have tried various combination of achieving xx as well as trying both predict method as well as decision_function methods in plot_boundary. I wonder why both time I get the same error and how could that be fixed? [sklearn noob here]

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-61aad96e14ed> in <module>()
      1 plot_data()
----> 2 plot_boundary(clf,-.5,.3,-.8,.6)

<ipython-input-144-1fdc3948d9d2> in plot_boundary(my_svm, xmin, xmax, ymin, ymax)
     15     #xx, yy = np.mgrid[xmin:xmax:200j, ymin:ymax:200j]
     16 
---> 17     Z = my_svm.predict(np.c_[xx.ravel(), yy.ravel()])
     18     #Z = my_svm.predict(np.array(Xval[xx]), np.array(yvals[yy]))
     19     #Z = my_svm.predict(Xval, yval)

~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in predict(self, X)
    546             Class labels for samples in X.
    547         """
--> 548         y = super(BaseSVC, self).predict(X)
    549         return self.classes_.take(np.asarray(y, dtype=np.intp))
    550 

~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in predict(self, X)
    306         y_pred : array, shape (n_samples,)
    307         """
--> 308         X = self._validate_for_predict(X)
    309         predict = self._sparse_predict if self._sparse else self._dense_predict
    310         return predict(X)

~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in _validate_for_predict(self, X)
    453                 raise ValueError("X.shape[1] = %d should be equal to %d, "
    454                                  "the number of samples at training time" %
--> 455                                  (X.shape[1], self.shape_fit_[0]))
    456         elif n_features != self.shape_fit_[1]:
    457             raise ValueError("X.shape[1] = %d should be equal to %d, "

ValueError: X.shape[1] = 2 should be equal to 211, the number of samples at training time

0条回答
登录 后发表回答