A kernel is a function that for all satisfies , where is a mapping from to an (inner product) feature space , .
The following functions take two array-like objects t (M, P) and x (N, P) and compute the (M, N) matrix with entries
Base class for kernels.
Linear kernel, t_i’ x_j.
Polynomial kernel, (gamma t_i’ x_j + b)^d.
Gaussian kernel, exp(-||t_i - x_j||^2 / 2 * sigma^2).
Exponential kernel, exp(-||t_i - x_j|| / 2 * sigma^2).
Sigmoid kernel, tanh(gamma t_i’ x_j + b).
Linear kernel, t_i’ x_j.
Polynomial kernel, (gamma t_i’ x_j + b)^d.
Gaussian kernel, exp(-||t_i - x_j||^2 / 2 * sigma^2).
Exponential kernel, exp(-||t_i - x_j|| / 2 * sigma^2).
Sigmoid kernel, tanh(gamma t_i’ x_j + b).
Example:
>>> import mlpy
>>> x = [[5, 1, 3, 1], [7, 1, 11, 4], [0, 4, 2, 9]] # three training points
>>> K = mlpy.kernel_gaussian(x, x, sigma=10) # compute the kernel matrix K_ij = k(x_i, x_j)
>>> K
array([[ 1. , 0.68045064, 0.60957091],
[ 0.68045064, 1. , 0.44043165],
[ 0.60957091, 0.44043165, 1. ]])
>>> t = [[8, 1, 5, 1], [7, 1, 11, 4]] # two test points
>>> Kt = mlpy.kernel_gaussian(t, x, sigma=10) # compute the test kernel matrix Kt_ij = <Phi(t_i), Phi(x_j)> = k(t_i, x_j)
>>> Kt
array([[ 0.93706746, 0.7945336 , 0.48190899],
[ 0.68045064, 1. , 0.44043165]])
The centered kernel matrix is computed by:
We can express in terms of and :
where is the matrix with all entries equal to and is .
Centers the testing kernel matrix Kt respect the training kernel matrix K. If Kt = K (kernel_center(K, K), where K = k(x_i, x_j)), the function centers the kernel matrix K.
Parameters : |
|
---|---|
Returns : |
|
Example:
>>> Kcentered = mlpy.kernel_center(K, K) # center K
>>> Kcentered
array([[ 0.19119746, -0.07197215, -0.11922531],
[-0.07197215, 0.30395696, -0.23198481],
[-0.11922531, -0.23198481, 0.35121011]])
>>> Ktcentered = mlpy.kernel_center(Kt, K) # center the test kernel matrix Kt respect to K
>>> Ktcentered
array([[ 0.15376875, 0.06761464, -0.22138339],
[-0.07197215, 0.30395696, -0.23198481]])
TODO
[Scolkopf98] | Bernhard Scholkopf, Alexander Smola, and Klaus-Robert Muller. Nonlinear component analysis as a kernel eigenvalue problem. Neural Computation, 10(5):1299–1319, July 1998. |