mojolearn.RBFSampler#

class mojolearn.RBFSampler(gamma=1.0, n_components=100, random_state=0)[source]#

Bases: _KernelMethodBase

Approximate the RBF kernel with random Fourier features.

Follows sklearn.kernel_approximation.RBFSampler. fit draws random weights and offsets from random_state; transform maps rows to scale_ * cos(X @ random_weights_ + random_offset_), a feature space whose inner products approximate exp(-gamma * ||x - y||^2). fit reads only the number of features of X (and its variance when gamma='scale'). Inputs and outputs are float32. Supports the 'identical' (default) and 'fast' numeric modes; 'fast' is available on Apple GPUs only.

Parameters:
  • gamma (float or ‘scale’, default 1.0) – RBF kernel coefficient; must be positive. ‘scale’ is scikit-learn’s 1 / (n_features * X.var()) (1.0 when the variance is zero), computed from the exact variance of the float32 data.

  • n_components (int, default 100) – Number of random features. Must be positive.

  • random_state (int, default 0) – Seed of the random draws.

  • numeric_mode ({‘identical’, ‘fast’} or None, default None) – Numeric mode for this estimator; None uses the process default.

Variables:
  • random_weights_ (Array of shape (n_features, n_components), float32) – The random projection weights.

  • random_offset_ (Array of shape (n_components,), float32) – The random phase offsets.

  • sigma_ (float) – sqrt(2 * gamma), the standard deviation of the weights.

  • scale_ (float) – sqrt(2 / n_components), the output scale.

  • n_features_in_ (int) – Number of features seen during fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> features = mojolearn.RBFSampler(gamma=0.5, n_components=256).fit_transform(X)
__init__(gamma=1.0, n_components=100, random_state=0)[source]#

Methods

fit(X[, y])

Draw the random weights and offsets.

fit_transform(X[, y])

Fit to X, then transform X.

get_params([deep])

load(path)

Load a sampler written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

save(path)

Save the fitted sampler to path as an .npz file.

set_params(**params)

Set constructor parameters.

transform(X)

Map X to the random Fourier feature space.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y=None)[source]#

Draw the random weights and offsets.

Reads only X.shape[1] (and, for gamma='scale', the variance of X), as scikit-learn does.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (ignored) – Present for scikit-learn API compatibility.

Returns:

self (RBFSampler) – The fitted transformer.

transform(X)[source]#

Map X to the random Fourier feature space.

Parameters:

X (array-like of shape (n_rows, n_features)) – Data to transform.

Returns:

Array of shape (n_rows, n_components), float32 – scale_ * cos(X @ random_weights_ + random_offset_).

fit_transform(X, y=None)[source]#

Fit to X, then transform X.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (ignored) – Present for scikit-learn API compatibility.

Returns:

Array of shape (n_samples, n_components), float32 – The transformed training data.

save(path)[source]#

Save the fitted sampler to path as an .npz file.

Load it with RBFSampler.load, or transform from it on a CPU without a GPU with mojolearn.host_model(path).

Parameters:

path (str or path-like) – Destination file.

Raises:

RuntimeError – If the sampler has not been fitted.

classmethod load(path)[source]#

Load a sampler written by save.

Parameters:

path (str or path-like) – File written by RBFSampler.save.

Returns:

RBFSampler – A fitted sampler with the saved weights (nothing is drawn again), ready to transform, with the saved numeric mode.