fa.bianp.net

Support Vector machines with custom kernels using scikits.learn

Category: General, scikit-learn, Tecnologí­a

It is now possible (using the development version as of may 2010) to use Support Vector Machines with custom kernels in scikits.learn. How to use it couldn't be more simple: you just pass a callable (the kernel) to the class constructor). For example, a linear kernel would be implemented as follows: [cc lang="python"] import numpy as np def my_kernel(x, y): return np.dot(x, y.T) [/cc] The only requisites for defining a kernel is that it should take as argument two numpy arrays and return also a numpy array. Then you would pass the kernel to the classifier's constructor: [cc lang="python"] from scikits.learn import svm clf = svm.SVC(kernel=my_kernel) [/cc] and that's all. The construct recognizes this as a custom kernel and you can then use the classifier as any other classifier. [cc lang="python"] clf.fit([[0, 0], [1, 1]], [0, 1]) print clf.predict([[0, 0]]) --> [0.] [/cc] For a complete reference, see the the reference manual and an example.