API Reference / base /

kernels.py

Base class for SVM Kernel functions.

Kernels compute similarity between instances in a (possibly infinite) feature space without explicitly computing the feature mapping.

Classes

Kernel

class base.kernels.Kernel(ABC)

Abstract base class for kernel functions.

A kernel function K(x, y) computes the dot product of two instances in a (possibly high-dimensional or infinite) feature space without explicitly computing the feature mapping \phi:

K(x, y) = \langle \phi(x), \phi(y) \rangle

Kernels must satisfy Mercer's condition (positive semi-definite) to ensure valid behavior with SVMs.

Constructor
__init__(
    self,
)

Attributes

X_
np.ndarray of shape (n_samples, n_features)
Training data (set by build()).
n_samples_
int
Number of training samples.
n_features_
int
Number of features.

Methods

__init__ (self)

Initialize the kernel.

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

Build the kernel with training data.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Training data.
Returns
self
Kernel
The built kernel, for method chaining.
evaluate (self, x1: np.ndarray, x2: np.ndarray) -> float

Evaluate the kernel function :math:`K(x_1, x_2)`.

Parameters
x1
np.ndarray of shape (n_features,)
First instance.
x2
np.ndarray of shape (n_features,)
Second instance.
Returns
value
float
Kernel value.
compute (self, i: int, j: int) -> float

Compute kernel value between training instances i and j.

Parameters
i
int
Index of first instance.
j
int
Index of second instance.
Returns
value
float
Kernel value K(X[i], X[j]).
compute_row (self, i: int) -> np.ndarray

Compute kernel values between instance i and all training instances.

Parameters
i
int
Index of query instance.
Returns
row
np.ndarray of shape (n_samples,)
Kernel values [K(X[i], X[0]), K(X[i], X[1]), ...].
compute_matrix (self) -> np.ndarray

Compute the full kernel (Gram) matrix.

Returns
K
np.ndarray of shape (n_samples, n_samples)
Kernel matrix where K[i, j] = K(X[i], X[j]).
compute_matrix_cross (self, X1: np.ndarray, X2: np.ndarray) -> np.ndarray

Compute the kernel matrix between two different sets of samples.

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 where K[i, j] = K(X1[i], X2[j]).
compute_with_point (self, x: np.ndarray) -> np.ndarray

Compute kernel values between a point and all training instances.

Parameters
x
np.ndarray of shape (n_features,)
Query point.
Returns
values
np.ndarray of shape (n_samples,)
Kernel values between x and each training instance.
get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return parameter schema for the kernel.

Returns
schema
dict of str to dict
Mapping of constructor parameter names to JSON-Schema-style descriptions. Empty for kernels without hyperparameters.
__repr__ (self) -> str

Return string representation of the kernel.

CachedKernel

class base.kernels.CachedKernel(Kernel)

Kernel with caching for repeated evaluations.

Stores computed kernel values to avoid redundant calculations.
Constructor
__init__(
    self,
    cache_size: int = _DEFAULT_CACHE_SIZE,
)

Parameters

cache_size
int = 250007
Size of the cache, in number of stored kernel entries. Use 0 for an unbounded (full) cache and -1 to disable caching entirely. The default is a large prime so the symmetric (i, j) cache keys hash evenly across buckets.

Methods

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

Build kernel with training data and initialize an empty cache.

Parameters
X
np.ndarray of shape (n_samples, n_features)
Training data.
Returns
self
CachedKernel
The built kernel, for method chaining.
compute (self, i: int, j: int) -> float

Compute kernel value between training instances i and j with caching.

Parameters
i
int
Index of first instance.
j
int
Index of second instance.
Returns
value
float
Kernel value K(X[i], X[j]), served from the cache when available.
clear_cache (self) -> None

Clear the kernel cache and reset hit/miss counters.

Returns
None
get_cache_stats (self) -> Dict[str, int]

Get cache statistics.

Returns
stats
dict of str to int
Dictionary with "hits", "misses", and "size" counts.

Functions

Func

kernel

Line 17
kernel(tags: List[str]=None, version: str='1.0.0')

Class decorator that registers a kernel with the component registry.

Parameters

tags
list of str = None
Tags for categorization in the registry.
version
str = "1.0.0"
Version string for the kernel.

Returns

decorator
callable
Decorator that attaches registry metadata to the kernel class.