# Kernels¶

A collection of common kernel / similarity functions. All kernels are continuous, bounded, and symmetric real functions which integrate to 1.

## LinearKernel¶

class numpy_ml.utils.kernels.LinearKernel(c0=0)[source]

The linear (i.e., dot-product) kernel.

Notes

For input vectors $$\mathbf{x}$$ and $$\mathbf{y}$$, the linear kernel is:

$k(\mathbf{x}, \mathbf{y}) = \mathbf{x}^\top \mathbf{y} + c_0$
Parameters: c0 (float) – An “inhomogeneity” parameter. When c0 = 0, the kernel is said to be homogenous. Default is 1.
set_params(summary_dict)[source]

Set the model parameters and hyperparameters using the settings in summary_dict.

Parameters: summary_dict (dict) – A dictionary with keys ‘parameters’ and ‘hyperparameters’, structured as would be returned by the summary() method. If a particular (hyper)parameter is not included in this dict, the current value will be used. new_kernel (Kernel instance) – A kernel with parameters and hyperparameters adjusted to those specified in summary_dict.
summary()[source]

Return the dictionary of model parameters, hyperparameters, and ID

## PolynomialKernel¶

class numpy_ml.utils.kernels.PolynomialKernel(d=3, gamma=None, c0=1)[source]

The degree-d polynomial kernel.

Notes

For input vectors $$\mathbf{x}$$ and $$\mathbf{y}$$, the polynomial kernel is:

$k(\mathbf{x}, \mathbf{y}) = (\gamma \mathbf{x}^\top \mathbf{y} + c_0)^d$

In contrast to the linear kernel, the polynomial kernel also computes similarities across dimensions of the x and y vectors, allowing it to account for interactions between features. As an instance of the dot product family of kernels, the polynomial kernel is invariant to a rotation of the coordinates about the origin, but not to translations.

Parameters: d (int) – Degree of the polynomial kernel. Default is 3. gamma (float or None) – A scaling parameter for the dot product between x and y, determining the amount of smoothing/resonlution of the kernel. Larger values result in greater smoothing. If None, defaults to 1 / C. Sometimes referred to as the kernel bandwidth. Default is None. c0 (float) – Parameter trading off the influence of higher-order versus lower-order terms in the polynomial. If c0 = 0, the kernel is said to be homogenous. Default is 1.
set_params(summary_dict)[source]

Set the model parameters and hyperparameters using the settings in summary_dict.

Parameters: summary_dict (dict) – A dictionary with keys ‘parameters’ and ‘hyperparameters’, structured as would be returned by the summary() method. If a particular (hyper)parameter is not included in this dict, the current value will be used. new_kernel (Kernel instance) – A kernel with parameters and hyperparameters adjusted to those specified in summary_dict.
summary()[source]

Return the dictionary of model parameters, hyperparameters, and ID

## RBFKernel¶

class numpy_ml.utils.kernels.RBFKernel(sigma=None)[source]

Radial basis function (RBF) / squared exponential kernel.

Notes

For input vectors $$\mathbf{x}$$ and $$\mathbf{y}$$, the radial basis function kernel is:

$k(\mathbf{x}, \mathbf{y}) = \exp \left\{ -0.5 \left\lVert \frac{\mathbf{x} - \mathbf{y}}{\sigma} \right\rVert_2^2 \right\}$

The RBF kernel decreases with distance and ranges between zero (in the limit) to one (when x = y). Notably, the implied feature space of the kernel has an infinite number of dimensions.

Parameters: sigma (float or array of shape (C,) or None) – A scaling parameter for the vectors x and y, producing an isotropic kernel if a float, or an anistropic kernel if an array of length C. Larger values result in higher resolution / greater smoothing. If None, defaults to $$\sqrt(C / 2)$$. Sometimes referred to as the kernel ‘bandwidth’. Default is None.
set_params(summary_dict)[source]

Set the model parameters and hyperparameters using the settings in summary_dict.

Parameters: summary_dict (dict) – A dictionary with keys ‘parameters’ and ‘hyperparameters’, structured as would be returned by the summary() method. If a particular (hyper)parameter is not included in this dict, the current value will be used. new_kernel (Kernel instance) – A kernel with parameters and hyperparameters adjusted to those specified in summary_dict.
summary()[source]

Return the dictionary of model parameters, hyperparameters, and ID