1

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
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • 1
    What is `my_svm.SVC`. Directly use `my_svm.decision_function()`. – Vivek Kumar Nov 30 '17 at 08:02
  • it didn't work. Additionally, as I said, I do actually need to use the my_svm.SVC.predict thought that gives the same exact error – Mona Jalal Nov 30 '17 at 08:03
  • 1
    You are sending `clf` to `plot_boundary`. clf is already `svm.SVC`. So why calling `clf.SVC...`? `decision_function` and `predict` are methods of the object 'clf` – Vivek Kumar Nov 30 '17 at 08:06
  • If I don't use the SVC I get this error https://pastebin.com/2HH41t50 I do need to use it – Mona Jalal Nov 30 '17 at 08:08
  • 1
    What exactly do you send to `plot_boundary(my_svm, xmin, xmax, ymin, ymax)`? What is your `my_svm` you send? As far as I understand it should be your fitted `clf`, shouldn't it? ==EDIT== Especially when you also have (commented) `.predict` called on it. – jo9k Nov 30 '17 at 08:14
  • @jo9k that was a typo that brought me back to one of my past errors! please see the update – Mona Jalal Nov 30 '17 at 08:23
  • The new error says that the features have changed. While fitting your `gaussianKernelGramMatrix` returned a matrix with 211 columns. (A square matrix of shape (211, 211). But now you are passing X of 2 columns. – Vivek Kumar Nov 30 '17 at 08:34
  • 1
    You should have the same number of gridlines, but not the same step. you have h=.2 but both xx and yy would have different values for h. you can get the same number of gridlines by taking `(xmax - xmin) / len(Xval)` and the corresponding result for yy – Matthew Ciaramitaro Nov 30 '17 at 08:50
  • 1
    Additionally Z needs to be a matrix with len(xx) columns and len(yy) rows. Right now its the same shape as xx. Essentially for every x in xx, and every y in yy Z[y][x] = predict([x,y]) – Matthew Ciaramitaro Nov 30 '17 at 08:53

0 Answers0