Skip to content

Commit edf077c

Browse files
committed
Merge pull request scikit-learn#5050 from AnishShah/issue5043
DOC Fix documentation of callable kernel
2 parents 4ac6a90 + a7081d7 commit edf077c

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

doc/modules/svm.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,17 @@ Using Python functions as kernels
482482
You can also use your own defined kernels by passing a function to the
483483
keyword ``kernel`` in the constructor.
484484

485-
Your kernel must take as arguments two matrices and return a third matrix.
485+
Your kernel must take as arguments two matrices of shape
486+
``(n_samples_1, n_features)``, ``(n_samples_2, n_features)``
487+
and return a kernel matrix of shape ``(n_samples_1, n_samples_2)``.
486488

487489
The following code defines a linear kernel and creates a classifier
488490
instance that will use that kernel::
489491

490492
>>> import numpy as np
491493
>>> from sklearn import svm
492-
>>> def my_kernel(x, y):
493-
... return np.dot(x, y.T)
494+
>>> def my_kernel(X, Y):
495+
... return np.dot(X, Y.T)
494496
...
495497
>>> clf = svm.SVC(kernel=my_kernel)
496498

examples/svm/plot_custom_kernel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
Y = iris.target
2121

2222

23-
def my_kernel(x, y):
23+
def my_kernel(X, Y):
2424
"""
2525
We create a custom kernel:
2626
2727
(2 0)
28-
k(x, y) = x ( ) y.T
28+
k(X, Y) = X ( ) Y.T
2929
(0 1)
3030
"""
3131
M = np.array([[2, 0], [0, 1.0]])
32-
return np.dot(np.dot(x, M), y.T)
32+
return np.dot(np.dot(X, M), Y.T)
3333

3434

3535
h = .02 # step size in the mesh

sklearn/svm/classes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ class SVC(BaseSVC):
406406
It must be one of 'linear', 'poly', 'rbf', 'sigmoid', 'precomputed' or
407407
a callable.
408408
If none is given, 'rbf' will be used. If a callable is given it is
409-
used to precompute the kernel matrix.
409+
used to pre-compute the kernel matrix from data matrices; that matrix
410+
should be an array of shape ``(n_samples, n_samples)``.
410411
411412
degree : int, optional (default=3)
412413
Degree of the polynomial kernel function ('poly').

0 commit comments

Comments
 (0)