mojolearn.RBFSampler#
- class mojolearn.RBFSampler(gamma=1.0, n_components=100, random_state=0)[source]#
Bases:
_KernelMethodBaseApproximate 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.
- 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)
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.numeric_mode_used()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.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, 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)[source]#
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)[source]#
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)[source]#
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.