qstack.regression.local_kernels¶
Local (atomic) kernel implementations.
- Provides:
local_kernels_dict: Dictionary mapping kernel names to their implementations. RAM_BATCHING_SIZE: Max. RAM (in bytes) that can be used for batched compuation
of Manhattan distance matrix for L_custom_py kernel. Can be modified before call using local_kernels.RAM_BATCHING_SIZE = ….
- qstack.regression.local_kernels.compute_distance_matrix(R1, R2)[source]¶
Compute the Manhattan-distance matrix.
This computes (||r_1 - r_2||_1) between the samples of R1 and R2, using a batched python/numpy implementation, designed to be more memory-efficient than a single numpy call and faster than a simple python for loop.
This function is a batched-over-R1 implementation of the following code: return np.sum( (R1[:,None, …]-R2[None,:, …])**2, axis=tuple(range(2, R1.ndim)))
- Parameters:
R1 (numpy ndarray) – First set of samples (can be multi-dimensional).
R2 (numpy ndarray) – Second set of samples.
- Returns:
squared-distance matrix of shape (len(R1), len(R2)).
- Return type:
numpy ndarray
- Raises:
RuntimeError – If X and Y have incompatible shapes.
- qstack.regression.local_kernels.cosine_similarity_wrapper(x, y, *_kargs, **_kwargs)[source]¶
Compute cosine similarity kernel.
- Parameters:
x (numpy ndarray) – First set of samples.
y (numpy ndarray) – Second set of samples.
*_kargs – Unused positional arguments (for compatibility).
**_kwargs – Unused keyword arguments (for compatibility).
- Returns:
Cosine similarity matrix.
- Return type:
numpy ndarray
- qstack.regression.local_kernels.custom_C_kernels(kernel_function, return_distance_function=False)[source]¶
Create kernel function wrappers using C implementation for speed.
- Parameters:
kernel_function (str) – Kernel type (‘L’ for Laplacian, ‘G’ for Gaussian).
return_distance_function (bool) – If True, returns distance function instead of kernel. Defaults to False.
- Returns:
Kernel or distance function, or None if C library cannot be loaded.
- Return type:
callable or None
- qstack.regression.local_kernels.custom_laplacian_kernel(X, Y, gamma)[source]¶
Compute Laplacian kernel between X and Y using Python implementation.
K(x, y) = exp(-gamma * ||x - y||_1)
- Parameters:
X (numpy ndarray) – First set of samples (can be multi-dimensional).
Y (numpy ndarray) – Second set of samples.
gamma (float) – Kernel width parameter.
- Returns:
Laplacian kernel matrix of shape (len(X), len(Y)).
- Return type:
numpy ndarray
- Raises:
RuntimeError – If X and Y have incompatible shapes.
- qstack.regression.local_kernels.dot_kernel_wrapper(x, y, *_kargs, **_kwargs)[source]¶
Compute linear (dot product) kernel.
- Parameters:
x (numpy ndarray) – First set of samples.
y (numpy ndarray) – Second set of samples.
*_kargs – Unused positional arguments (for compatibility).
**_kwargs – Unused keyword arguments (for compatibility).
- Returns:
Linear kernel matrix.
- Return type:
numpy ndarray
- qstack.regression.local_kernels.local_laplacian_kernel_wrapper(X, Y, gamma)[source]¶
Decide which kernel implementation to call.
Wrapper that acts as a generic Laplacian kernel function.
- Parameters:
X (numpy ndarray) – First set of samples (can be multi-dimensional).
Y (numpy ndarray) – Second set of samples.
gamma (float) – Kernel width parameter.
- Returns:
Laplacian kernel matrix of shape (len(X), len(Y)).
- Return type:
numpy ndarray
- Raises:
RuntimeError – If X and Y have incompatible shapes.