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:
n_features_in_

Number of features input.

Type:

int

n_features_out_

Number of features output. This attribute is not available in estimators from sklearn.kernel_approximation.

Type:

int

ift_

Probability distribution corresponding to inverse Fourier transform of chosen kernel.

Type:

scipy.stats.rv_continuous

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 transform scipy.stats.norm (default),

    • 'laplacian' – Laplacian kernel, with inverse Fourier transform scipy.stats.cauchy, or

    • 'cauchy' – Cauchy kernel, with inverse Fourier transform scipy.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. If method='weight_only', then 2 * 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 from sklearn.kernel_approximation.RBFSampler. This can lead to a mysterious factor of sqrt(2) in other kernels. Default is 1.

  • method (str) – Feature generation method to use. If 'weight_offset' (default), each weight corresponds to a feature cos(weight.T @ x + offset). If 'weight_only', then each weight corresponds to two features, cos(weight.T @ x) and sin(weight.T @ x), meaning the number of features generated is 2 * 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()

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:

RandomFourierKernelApprox

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.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

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

  • None: Transform configuration is unchanged

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