mojolearn.RBFSampler#
- class mojolearn.RBFSampler(gamma=1.0, n_components=100, random_state=0)#
Approximate the RBF kernel with random Fourier features.
Follows
sklearn.kernel_approximation.RBFSampler.fitdraws random weights and offsets fromrandom_state;transformmaps rows toscale_ * cos(X @ random_weights_ + random_offset_), a feature space whose inner products approximateexp(-gamma * ||x - y||^2).fitreads only the number of features ofX(and its variance whengamma='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.
'fast'is available on Apple GPUs only.
- 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)
See also
Methods
fit(X[, y])Draw the random weights and offsets.
fit_transform(X[, y])Fit to
X, then transformX.get_params([deep])load(path)Load a sampler written by
save.The numeric mode this estimator runs in.
save(path)Save the fitted sampler to
pathas an.npzfile.set_params(**params)Set constructor parameters.
transform(X)Map
Xto the random Fourier feature space.The GPU backend this estimator runs on.
- fit(X, y=None)#
Draw the random weights and offsets.
Reads only
X.shape[1](and, forgamma='scale', the variance ofX), 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)#
Map
Xto 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)#
Fit to
X, then transformX.- 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)#
Save the fitted sampler to
pathas an.npzfile.Load it with
RBFSampler.load, or transform from it on a CPU without a GPU withmojolearn.host_model(path).- Parameters:
path (str or path-like) – Destination file.
- Raises:
RuntimeError – If the sampler has not been fitted.
- classmethod load(path)#
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.
- numeric_mode_used()#
The numeric mode this estimator runs in.
- Returns:
str –
'identical'or'fast', read from the compiled binary the estimator uses, not from the argument it was given.
- set_params(**params)#
Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.
- vendor_used()#
The GPU backend this estimator runs on.
- Returns:
str –
'metal','cuda'or'hip', read from the compiled binary the estimator uses.