Kernel functions

Kernel functions are the key to producing kernel matrices and hence are the backbone of kernel methods and machines. These are represented by a fundamental [abstract base] class called ``BaseKernelFunction``, which defines several desirable properties, such as

  • ensures it induces a positive semi-definite kernel matrix

  • making it callable, accepting at least two inputs (data points)

  • a readable representation of the underlying function with a name.

This modularization of all the kernel functions separate from the KernelMatrix class enables us to support diverse and mixed data types. This also enables to support various formats and data structures beyond numpy arrays such as pyradigm.

We also provide a KernelFromCallable class which makes it even easier to define a kernel function just by specifying the underlying computation, without having to define a fully separate class.

In addition, the following classes are provided to enable compositional representation of multiple kernel functions for advanced applications: CompositeKernel, ProductKernel, SumKernel, AverageKernel, and WeightedAverageKernel.

kernelmethods aims to offer kernel functions that can operate on the following data types:

Below, we document the API for the important classes related to kernel functions, such as :

  • BaseKernelFunction

  • KernelFromCallable

  • and composites as noted above.

BaseKernelFunction

class kernelmethods.BaseKernelFunction(name)[source]

Bases: abc.ABC

Abstract base class for kernel functions.

Enforces each derived kernel: 1. to be callable, with two inputs 2. to have a name and a str representation 3. provides a method to check whether the derived kernel func is a valid kernel

i.e. kernel matrix derived on a random sample is positive semi-definite (PSD)

  1. and that it is symmetric (via tests) as required.

is_psd()[source]

Tests whether kernel matrix produced via this function is PSD

KernelFromCallable

class kernelmethods.base.KernelFromCallable(input_func, name=None, **func_params)[source]

Bases: kernelmethods.base.BaseKernelFunction

Class to create a custom kernel from a given callable.

Parameters
  • input_func (callable) – A callable that can accept atleast 2 args Must not be builtin or C function. If func is a C or builtin func, wrap it in a python def

  • name (str) – A name to identify this kernel in a human readable way

  • func_params (dict) – Parameters to func

is_psd()

Tests whether kernel matrix produced via this function is PSD

Composite kernel functions

class kernelmethods.base.CompositeKernel(km_set, name='Composite')[source]

Bases: abc.ABC

Class to combine a set of kernels into a composite kernel.

Parameters
  • km_set (KernelSet) – KernelSet on which the composite kernel will be applied to

  • name (str) – Identifier for the composite kernel

property composite_KM

Returns the result of composite operation

abstract fit()[source]

Abstract methods that needs to be defined later.

property full

Returns the result of compsoite operation.

Alias for composite_KM to match the KernelMatrix interface.

class kernelmethods.base.ProductKernel(km_set, name='ProductKernel')[source]

Bases: kernelmethods.base.CompositeKernel

Class to define and compute a Product kernel from a KernelSet

Parameters
  • km_set (KernelSet) – KernelSet from which the product kernel will be computed from

  • name (str) – Identifier for the composite kernel

property composite_KM

Returns the result of composite operation

fit()[source]

Computes the product kernel.

property full

Returns the result of compsoite operation.

Alias for composite_KM to match the KernelMatrix interface.

class kernelmethods.base.SumKernel(km_set, name='SumKernel')[source]

Bases: kernelmethods.base.CompositeKernel

Class to define and compute a weighted sum kernel from a KernelSet

Parameters
  • km_set (KernelSet) – KernelSet from which the summ kernel will be computed from

  • name (str) – Identifier for the composite kernel

property composite_KM

Returns the result of composite operation

fit(kernel_weights=None)[source]

Computes the sum kernel

property full

Returns the result of compsoite operation.

Alias for composite_KM to match the KernelMatrix interface.

class kernelmethods.base.AverageKernel(km_set, name='AverageKernel')[source]

Bases: kernelmethods.base.CompositeKernel

Class to define and compute an Average kernel from a KernelSet

Parameters
  • km_set (KernelSet) – KernelSet from which the average kernel will be computed

  • name (str) – Identifier for the composite kernel

property composite_KM

Returns the result of composite operation

fit()[source]

Computes the average kernel

property full

Returns the result of compsoite operation.

Alias for composite_KM to match the KernelMatrix interface.

class kernelmethods.base.WeightedAverageKernel(km_set, weights, name='WeightedAverageKernel')[source]

Bases: kernelmethods.base.CompositeKernel

Class to define and compute a weighted verage kernel from a KernelSet

Parameters
  • km_set (KernelSet) – KernelSet from which the average kernel will be computed

  • name (str) – Identifier for the composite kernel

property composite_KM

Returns the result of composite operation

fit()[source]

Computes the weighted average kernel

property full

Returns the result of compsoite operation.

Alias for composite_KM to match the KernelMatrix interface.