Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error - Using custom kernels for SVM in scikit-learn

I created an SVM instance with a kernel function of my own definition. When I try to run cross validation on the created model, I get the following error:

ValueError: X should be a square kernel matrix
Traceback:

scores = cross_val_score(model, X, y, cv=10)

File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\cross_validation.py", line 1152, in cross_val_score
for train, test in cv)

File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\externals\joblib\parallel.py", line 517, in call

self.dispatch(function, args, kwargs)

File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\externals\joblib\parallel.py", line 312, in dispatch

job = ImmediateApply(func, args, kwargs)

File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7- win32.egg\sklearn\externals\joblib\parallel.py", line 136, in init

self.results = func(*args, **kwargs)

File "C:\Python27\lib\site-packages\scikit_learn-0.14.1-py2.7-win32.egg\sklearn\cross_validation.py", line 1047, in _cross_val_score

raise ValueError("X should be a square kernel matrix")

Here is my code:

def hist_intersection(x, y):
    return np.sum(np.array([min(xi,yi) for xi,yi in zip(x,y)]))

model = svm.SVC(kernel = hist_intersection)
scores = cross_val_score(model, X, y, cv=10)
like image 557
Guru Prasad Avatar asked Oct 21 '22 12:10

Guru Prasad


1 Answers

I had a quick look and the SVC class (and the cross validation tool) both seem to expect kernel callables to compute the whole kernel matrix at once from the full-data matrix (which makes this feature very limited I agree). Please have a look at the tests for more details:

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/svm/tests/test_svm.py#L124

like image 106
ogrisel Avatar answered Oct 23 '22 02:10

ogrisel