In my code in which X and y are the training data:
from sklearn.svm import SVC
clf = SVC(kernel=lambda x,y:gauss_kernel(x, y, 100) )
print(X.shape[0])
print(X.shape[1])
print(X.shape)
clf.fit(X, y)
I get the following error:
211
2
(211, 2)
/Users/mona/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py:547: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-23-1f163ab380a5> in <module>()
8 print(X.shape)
9
---> 10 clf.fit(X, y)
11 plot_data()
12 plot_boundary(svm,-.5,.3,-.8,.6)
~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
185
186 seed = rnd.randint(np.iinfo('i').max)
--> 187 fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
188 # see comment on the other call to np.iinfo in this file
189
~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed)
226 X = self._compute_kernel(X)
227
--> 228 if X.shape[0] != X.shape[1]:
229 raise ValueError("X.shape[0] should be equal to X.shape[1]")
230
IndexError: tuple index out of range
Here's the customized Gaussian Kernel I wrote:
import math
def gauss_kernel(x1, x2, gamma):
sigma = math.sqrt(gamma)
return np.exp(-np.sum((x1-x2)**2)/(2*sigma**2))
How should I fix this? When I look at SVM examples in sklearn, they basically do the same thing. I believe I am neglecting something small but can't pin down the problem when matching with sklearn examples.