pykoop.RandomFourierKernelApprox
- class RandomFourierKernelApprox(kernel_or_ift='gaussian', n_components=100, shape=1, method='weight_offset', random_state=None)
Bases:
KernelApproximation
Kernel approximation with random Fourier features.
Similar to
sklearn.kernel_approximation.RBFSampler
, but supports kernels other than just the Gaussian.For details, see [RR07].
- Parameters:
kernel_or_ift (str | rv_continuous)
n_components (int)
shape (float)
method (str)
random_state (int | RandomState | None)
- n_features_out_
Number of features output. This attribute is not available in estimators from
sklearn.kernel_approximation
.- Type:
- ift_
Probability distribution corresponding to inverse Fourier transform of chosen kernel.
- random_weights_
Random weights to inner-product with features.
- Type:
np.ndarray, shape (n_features, n_components)
- random_offsets_
Random offsets if
method
is'weight_offset'
.- Type:
np.ndarray, shape (n_features, )
Examples
Generate random Fourier features from a Gaussian kernel
>>> ka = pykoop.RandomFourierKernelApprox( ... kernel_or_ift='gaussian', ... n_components=10, ... shape=1, ... random_state=1234, ... ) >>> ka.fit(X_msd[:, 1:]) # Remove episode feature RandomFourierKernelApprox(n_components=10, random_state=1234) >>> ka.transform(X_msd[:, 1:]) array([...])
- __init__(kernel_or_ift='gaussian', n_components=100, shape=1, method='weight_offset', random_state=None)
Instantiate
RandomFourierKernelApprox
.- Parameters:
kernel_or_ift (Union[str, scipy.stats.rv_continuous]) –
Kernel to approximate. Possible options are
'gaussian'
– Gaussian kernel, with inverse Fourier transformscipy.stats.norm
(default),'laplacian'
– Laplacian kernel, with inverse Fourier transformscipy.stats.cauchy
, or'cauchy'
– Cauchy kernel, with inverse Fourier transformscipy.stats.laplace
.
Alternatively, a positive, shift-invariant kernel can be implicitly specified by providing its inverse Fourier transform as a univariate probability distribution subclassing
scipy.stats.rv_continuous
.n_components (int) – Number of random samples used to generate features. If
method='weight_offset'
, this corresponds directly to the number of features generated. Ifmethod='weight_only'
, then2 * n_components
features are generated.shape (float) – Shape parameter. Must be greater than zero. Larger numbers correspond to “sharper” kernels. Scaled to be consistent with
gamma
fromsklearn.kernel_approximation.RBFSampler
. This can lead to a mysterious factor ofsqrt(2)
in other kernels. Default is1
.method (str) – Feature generation method to use. If
'weight_offset'
(default), each weight corresponds to a featurecos(weight.T @ x + offset)
. If'weight_only'
, then each weight corresponds to two features,cos(weight.T @ x)
andsin(weight.T @ x)
, meaning the number of features generated is2 * n_components
.random_state (Union[int, np.random.RandomState, None]) – Random seed.
- Return type:
None
Methods
__init__
([kernel_or_ift, n_components, ...])Instantiate
RandomFourierKernelApprox
.fit
(X[, y])Fit kernel approximation.
fit_transform
(X[, y])Fit to data, then transform it.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X)Transform data.
- fit(X, y=None)
Fit kernel approximation.
- Parameters:
X (np.ndarray) – Data matrix.
y (Optional[np.ndarray]) – Ignored.
- Returns:
Instance of itself.
- Return type:
- Raises:
ValueError – If any of the constructor parameters are incorrect.
- fit_transform(X, y=None, **fit_params)
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).
**fit_params (dict) – Additional fit parameters.
- Returns:
X_new – Transformed array.
- Return type:
ndarray array of shape (n_samples, n_features_new)
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequest
encapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)
Get parameters for this estimator.
- set_output(*, transform=None)
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
transform ({"default", "pandas"}, default=None) –
Configure output of transform and fit_transform.
”default”: Default output format of a transformer
”pandas”: DataFrame output
”polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_params(**params)
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- transform(X)
Transform data.
- Parameters:
X (np.ndarray) – Data matrix.
- Returns:
Transformed data matrix.
- Return type:
np.ndarray