Precomputed Kernel Matrix implementation.
Classes
Precomputed Kernel Matrix for supplying a pre-calculated Gram matrix.
The Precomputed Kernel allows users to provide their own kernel (Gram) matrix rather than having it computed internally. This is useful when the kernel is expensive to compute, when using a custom kernel not available in the library, or when the kernel matrix comes from an external source.
Constructor
__init__( self, kernel_matrix: np.ndarray = None, )
Overview
The usage workflow is:
- Compute a kernel matrix K externally (e.g., a custom domain-specific kernel)
-
Pass the matrix to
PrecomputedKernel(either at construction or viaset_kernel_matrix) -
Call
build(X)to associate training indices with the matrix -
Use
compute(i, j)orcompute_matrix()to retrieve kernel values
Theory
A valid kernel matrix K \in \mathbb{R}^{n \times n} must be symmetric and positive semi-definite (Mercer's condition):
K_{ij} = K(x_i, x_j), \quad K = K^T, \quad \forall c: c^T K c \geq 0
Any function K: \mathcal{X} \times \mathcal{X} \to \mathbb{R} satisfying Mercer's theorem can be represented as a precomputed matrix.
Parameters
kernel_matrix
np.ndarray or None
= None
Precomputed kernel matrix of shape
(n_samples, n_samples). If None, the matrix passed to build() is treated as the kernel matrix itself.
Attributes
n_samples\_
int
Number of samples in the kernel matrix.
n_features\_
int
Number of features (or
n_samples when the matrix is used directly).
Notes
Complexity:
- Lookup: O(1) per pair (direct matrix indexing)
- Storage: O(n^2) for the full Gram matrix
- When using a domain-specific or custom kernel function
- When the kernel computation is expensive and should be done once
- Graph kernels, Wasserstein kernels, or other specialized similarity measures
- When leveraging external libraries to compute the kernel matrix
References
Scholkopf2002
Schoelkopf, B. and Smola, A.J. (2002).
Learning with Kernels: Support Vector Machines, Regularization, Optimization, and Beyond.
MIT Press.
See Also
Basic usage with an externally computed kernel matrix:
python
>>> from tuiml.algorithms.svm.kernels import PrecomputedKernel
>>> import numpy as np
>>>
>>> # Create a simple kernel matrix (linear kernel)
>>> X = np.array([[1, 2], [3, 4], [5, 6]])
>>> K = X @ X.T
>>> kernel = PrecomputedKernel(kernel_matrix=K)
>>> kernel.build(X)
PrecomputedKernel(...)
>>> value = kernel.compute(0, 1)
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
Retrieve sub-matrix from the precomputed kernel matrix by index.
Parameters
X1
np.ndarray of shape (n1,) or (n1, n_features)
If 1-D integer array, treated as row indices into the stored kernel matrix. Otherwise falls back to the base class.
X2
np.ndarray of shape (n2,) or (n2, n_features)
If 1-D integer array, treated as column indices into the stored kernel matrix. Otherwise falls back to the base class.
Returns
K
np.ndarray of shape (n1, n2)
Sub-matrix of the precomputed kernel.