RBF (Radial Basis Function) Kernel implementation.
Classes
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:
- Compute the squared Euclidean distance \|x - y\|^2 between two vectors
- Scale by the negative kernel coefficient -\gamma
- Apply the exponential function to produce a similarity in (0, 1]
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)
- 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.
See Also
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
compute_matrix_cross
(self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray
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)`.