RBF (Radial Basis Function) Kernel implementation.

Classes

RBFKernel

class algorithms.svm.kernels.rbf.RBFKernel(CachedKernel)

RBF (Radial Basis Function) Kernel, also known as the Gaussian kernel.

The RBF kernel maps data into an infinite-dimensional feature space, enabling SVMs to learn arbitrarily complex non-linear decision boundaries. It is the most widely used kernel for general-purpose classification and regression tasks.
Constructor
__init__(
    self,
    gamma: float = 0.01,
    cache_size: int = 250007,
)

Overview

The kernel evaluation proceeds as follows:

  1. Compute the squared Euclidean distance \|x - y\|^2 between two vectors
  2. Scale by the negative kernel coefficient -\gamma
  3. Apply the exponential function to produce a similarity in (0, 1]
During build(), squared norms are precomputed for efficient matrix-level evaluation.

Theory

The RBF kernel function is defined as:

K(x, y) = \exp\bigl(-\gamma \|x - y\|^2\bigr)

Equivalently, with bandwidth parameter \sigma:

K(x, y) = \exp\!\left(-\frac{\|x - y\|^2}{2\sigma^2}\right)

where \gamma = 1 / (2\sigma^2).

Properties:

  • K(x, x) = 1 for all x (unit self-similarity)
  • K(x, y) \to 0 as \|x - y\| \to \infty
  • The kernel is positive semi-definite for all \gamma > 0

Parameters

gamma
Union[str, float] = 0.01

Kernel coefficient controlling the width of the Gaussian:

  • 'scale' -- Uses ``1 / (n_features * X.var())``
  • 'auto' -- Uses ``1 / n_features``
  • float -- User-defined positive coefficient
cache_size
int = 250007
Maximum number of kernel evaluations to cache for repeated lookups.

Attributes

gamma\_
float
Actual gamma value used (computed during build() when 'scale' or 'auto' is specified).

Notes

Complexity:

  • Single evaluation: O(p) where p = number of features
  • Matrix computation: O(n^2 p) for n samples (vectorized)
When to use RBFKernel:
  • Default choice when no domain knowledge suggests a specific kernel
  • Non-linearly separable data of moderate dimensionality
  • When a smooth, radially symmetric similarity measure is appropriate
  • Classification and regression problems with continuous features

References

Scholkopf2002
Schoelkopf, B. and Smola, A.J. (2002). Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond. MIT Press.
Chang2010
Chang, Y.W., Hsieh, C.J., Chang, K.W., Ringgaard, M. and Lin, C.J. (2010). Training and Testing Low-degree Polynomial Data Mappings via Linear SVM. Journal of Machine Learning Research, 11, pp. 1471-1490.

Basic usage with an explicit gamma value:

python
>>> from tuiml.algorithms.svm.kernels import RBFKernel
>>> import numpy as np
>>>
>>> X_train = np.array([[1, 2], [3, 4], [5, 6]])
>>> kernel = RBFKernel(gamma=0.1)
>>> kernel.build(X_train)
RBFKernel(...)
>>> K = kernel.compute_matrix()
>>> print(K.shape)
(3, 3)

Methods

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema.

build (self, X: np.ndarray) -> 'RBFKernel'

Build kernel and precompute squared norms.

Parameters
X
np.ndarray
Training data.
Returns
self
RBFKernel
Returns the built instance.
evaluate (self, x1: np.ndarray, x2: np.ndarray) -> float

Evaluate RBF kernel: K(x, y) = exp(-gamma * ||x - y||^2).

Parameters
x1
np.ndarray
First vector.
x2
np.ndarray
Second vector.
Returns
val
float
RBF kernel value in (0, 1].
compute (self, i: int, j: int) -> float

Compute RBF kernel efficiently using precomputed norms.

Parameters
i
int
Index of first instance.
j
int
Index of second instance.
Returns
val
float
Kernel value.
compute_matrix (self) -> np.ndarray

Compute the full RBF kernel matrix using vectorized operations.

Returns
K
np.ndarray of shape (n_samples, n_samples)
The kernel (Gram) matrix with :math:`K[i,j] = \exp(-\gamma \|x_i - x_j\|^2)`.
compute_matrix_cross (self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray

Compute the RBF kernel matrix between two sample sets.

Parameters
X1
np.ndarray of shape (n1, n_features)
First set of samples.
X2
np.ndarray of shape (n2, n_features)
Second set of samples.
Returns
K
np.ndarray of shape (n1, n2)
Kernel matrix with :math:`K[i,j] = \exp(-\gamma \|X_1[i] - X_2[j]\|^2)`.
__repr__ (self) -> str

String representation.