Kernel functions and utilities

Methods related to calculation of kernel function values and kernel matrices.

mklaren.kernel.kernel.center_kernel(K)

Center a kernel matrix.

\[\mathbf{K}_{c} = (\mathbf{I}-\dfrac{\mathbf{11}^T}{n})\mathbf{K}(\mathbf{I}-\dfrac{\mathbf{11}^1}{n})\]
Parameters:K – (numpy.ndarray) Kernel matrix of shape (n, n).
Returns:(numpy.ndarray) Centered kernel for a sample of points.
mklaren.kernel.kernel.center_kernel_low_rank(G)

Center a the feature matrix such that \(\mathbf{G}_c \mathbf{G}_c^T\) is centered.

\[\mathbf{G}_c = (\mathbf{I} - \dfrac{\mathbf{11}^T}{n})\mathbf{G}\]
Parameters:G – (numpy.ndarray) Low-rank approximation of the feature matrix of shape (n, k).
Returns:(numpy.ndarray) Centered low-rank approximation of the feature space.
mklaren.kernel.kernel.correct_xy(x, y)

Convert matrices to dense and correct shapes.

Parameters:
  • x – (numpy.ndarray) 2D or 1D array
  • y – (numpy.ndarray) 2D or 1D array
Returns:

(numpy.ndarray) Convert x, y to dense, 2D arrays.

mklaren.kernel.kernel.exponential_absolute(x, y, sigma=2.0, gamma=None)

The exponential quadratic / radial basis kernel (RBF) kernel.

\[k(\mathbf{x}, \mathbf{y}) = exp\{\dfrac{\|\mathbf{x} - \mathbf{y}\|^2}{\sigma^2} \}\]

or

\[k(\mathbf{x}, \mathbf{y}) = exp\{\gamma \|\mathbf{x} - \mathbf{y}\|^2 \}\]
param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param sigma:(float) Length scale.
param gamma:(float) Scale.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.exponential_cosine_kernel(x, y, gamma=1, omega=1)

A sum of exponential quadratic and a cosine kernel.

\[d = \|\mathbf{x} - \mathbf{y}\|\]
\[k(\mathbf{x}, \mathbf{y}) = \dfrac{1}{2} exp\{\dfrac{d^2}{\sigma^2}\} + \dfrac{1}{2} cos(\omega d^2)\]
param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param omega:(float) Frequency of the oscillation.
param gamma:(float) Scale.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.exponential_kernel(x, y, sigma=2.0, gamma=None)

The exponential quadratic / radial basis kernel (RBF) kernel.

\[k(\mathbf{x}, \mathbf{y}) = exp\{\dfrac{\|\mathbf{x} - \mathbf{y}\|^2}{\sigma^2} \}\]

or

\[k(\mathbf{x}, \mathbf{y}) = exp\{\gamma \|\mathbf{x} - \mathbf{y}\|^2 \}\]
param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param sigma:(float) Length scale.
param gamma:(float) Scale.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.kernel_row_normalize(K)

Divide inner products of examples by their norm in the feature space, effectively computing angles. Applycable only to symmetric kernels.

Parameters:K – (numpy.ndarray) Kernel matrix of shape (n, n).
Returns:(numpy.ndarray) Row-normalized kernel for a sample of points.
mklaren.kernel.kernel.kernel_sum(x, y, kernels, kernels_args, kernels_weights=None)

Sum of arbitrary kernel functions. :param x: (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ). :param y: (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ). :param kernels: (Iterable) Iterable of pointers to kernels. :param kernels_args: (Iterable) Iterable with dictionaries, of the same length as kernels.

Arguments are passed to kernels as kwargs.
Parameters:kernels_weights – (Iterable) Iterable with kernel weights, of the same length as kernels.
Returns:
mklaren.kernel.kernel.kernel_to_distance(K)

Divide inner products of examples by their norm in the feature space, effectively computing angles. Applycable only to symmetric kernels.

Parameters:K – (numpy.ndarray) Kernel matrix or Kinterface of shape (n, n).
Returns:(numpy.ndarray) Distance matrix in the feature space induced by K.
mklaren.kernel.kernel.linear_kernel(x, y, b=0)

The linear kernel (the usual dot product in n-dimensional space).

\[k(\mathbf{x}, \mathbf{y}) = b + \mathbf{x}^T \mathbf{y}\]
Parameters:
  • x – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • y – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • b – (float) Bias term.
Returns:

(numpy.ndarray) Kernel value/matrix between data points.

mklaren.kernel.kernel.linear_kernel_noise(x, y, b=1, noise=1)

The linear kernel (the usual dot product in n-dimensional space). A noise term is added explicitly to avoid singular kernel matrices

\[k(\mathbf{x}, \mathbf{y}) = b + \mathbf{x}^T \mathbf{y} + noise \cdot (\mathbf{x} == \mathbf{y})\]
Parameters:
  • x – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • y – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • b – (float) Bias term.
  • noise – (float) Noise term.
Returns:

(numpy.ndarray) Kernel value/matrix between data points.

mklaren.kernel.kernel.matern32_gpy(x, y, lengthscale=1)

Temp: GPy wrapper for the matern kernel.

mklaren.kernel.kernel.matern52_gpy(x, y, lengthscale=1)

Temp: GPy wrapper for the matern kernel.

mklaren.kernel.kernel.matern_kernel(x, y, l=1.0, nu=1.5)

The Matern kernel wrapped from Scikit learn.

\[k(\mathbf{x}, \mathbf{y}) = \sigma^2 \dfrac{2^{1-\]

u}}{Gamma{ u}} (sqrt{2 u} dfrac{d}{l})^{ u} K_{ u} (sqrt{2 u} dfrac{d}{l})

where {Gamma } Gamma is the gamma function, {K_{

u }} K_{ u }

is the modified Bessel function of the second kind, and l and

u are non-negative parameters of the covariance.

param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param l:(float) Length scale.
param nu:(float) Differentiability of the kernel.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.periodic_gpy(x, y, lengthscale=1, period=6.28)

Temp: GPy wrapper for the matern kernel.

mklaren.kernel.kernel.periodic_kernel(x, y, sigma=1, per=1, l=1)

The periodic kernel. Defined as in http://www.cs.toronto.edu/~duvenaud/cookbook/index.html.

\[k(\mathbf{x}, \mathbf{y}) = \sigma^2 exp\{-2 \pi sin(\dfrac{\|\mathbf{x} - \mathbf{y}\|}{per})/l \}\]
param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param per:(float) Period.
param l:(float) Length scale.
param sigma:(float) Variance.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.poly_kernel(x, y, degree=2, b=0)

The polynomial kernel.

\[k(\mathbf{x}, \mathbf{y}) = (b + \mathbf{x}^T \mathbf{y})^p\]
Parameters:
  • x – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • y – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • degree – (float) Polynomial degree.
  • b – (float) Bias term.
Returns:

(numpy.ndarray) Kernel value/matrix between data points.

mklaren.kernel.kernel.random_kernel(n)

Generate a random kernel matrix of shape (n, n).

Parameters:n – (int) Number of examples.
Returns:(numpy.ndarray) Random positive semidefinite kernel matrix of shape (n, n).
mklaren.kernel.kernel.rbf_kernel(x, y, sigma=2.0, gamma=None)

The exponential quadratic / radial basis kernel (RBF) kernel.

\[k(\mathbf{x}, \mathbf{y}) = exp\{\dfrac{\|\mathbf{x} - \mathbf{y}\|^2}{\sigma^2} \}\]

or

\[k(\mathbf{x}, \mathbf{y}) = exp\{\gamma \|\mathbf{x} - \mathbf{y}\|^2 \}\]
param x:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param y:(numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
param sigma:(float) Length scale.
param gamma:(float) Scale.
return:(numpy.ndarray) Kernel value/matrix between data points.
mklaren.kernel.kernel.sigmoid_kernel(x, y, c=1, b=0)

The sigmoid kernel.

\[k(\mathbf{x}, \mathbf{y}) = tan(c \mathbf{x}^T \mathbf{y} + b)\]
Parameters:
  • x – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • y – (numpy.ndarray) Data point(s) of shape (n_samples, n_features) or (n_features, ).
  • c – (float) Scale.
  • b – (float) Bias.
Returns:

(numpy.ndarray) Kernel value/matrix between data points.