mojolearn.Nystroem#

class mojolearn.Nystroem(kernel='rbf', gamma=None, degree=3, coef0=1.0, n_components=100, random_state=0)[source]#

Bases: _KernelMethodBase

Approximate a kernel map with the Nystroem method.

Follows sklearn.kernel_approximation.Nystroem. fit picks n_components training rows at random as the basis, forms their kernel matrix and computes its inverse square root with a Jacobi eigensolver on the device; transform maps rows to K(X, components_) @ normalization_.T, a feature space whose inner products approximate the kernel. Inputs and outputs are float32. Supports the 'identical' (default) and 'fast' numeric modes; 'fast' is available on Apple GPUs only.

Parameters:
  • kernel (str, default ‘rbf’) – One of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘laplacian’, ‘cosine’, ‘chi2’ or ‘additive_chi2’. ‘precomputed’ is refused with ValueError. Both chi2 kernels refuse negative values in X.

  • gamma (float or None, default None) – Kernel coefficient. None means 1 / n_features (1.0 for ‘chi2’).

  • degree (int, default 3) – Degree of the ‘poly’ kernel.

  • coef0 (float, default 1.0) – Independent term of the ‘poly’ and ‘sigmoid’ kernels.

  • n_components (int, default 100) – Number of basis rows. Must be positive and at most n_samples.

  • random_state (int, default 0) – Seed of the basis selection.

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

Variables:
  • components_ (Array of shape (n_components, n_features), float32) – The basis rows.

  • component_indices_ (Array of shape (n_components,), int32) – Indices of the basis rows in the training data.

  • normalization_ (Array of shape (n_components, n_components), float32) – Inverse square root of the basis kernel matrix.

  • eigenvalues_ (Array of shape (n_components,), float32) – Absolute eigenvalues of the basis kernel matrix (its singular values), descending. A negative eigenvalue keeps its sign in the right factor of normalization_, as scikit-learn’s SVD does.

  • eigenvectors_ (Array of shape (n_components, n_components), float32) – Eigenvectors of the basis kernel matrix.

  • sweeps_ (int) – Number of Jacobi sweeps the eigensolver ran.

  • 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.Nystroem(n_components=64).fit_transform(X)
__init__(kernel='rbf', gamma=None, degree=3, coef0=1.0, n_components=100, random_state=0)[source]#

Methods

fit(X[, y])

Choose the basis rows and compute the normalization.

fit_transform(X[, y])

Fit to X, then transform X.

get_params([deep])

load(path)

Load a transformer written by save.

numeric_mode_used()

The numeric mode this estimator runs in.

save(path)

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

set_params(**params)

Set constructor parameters.

transform(X)

Map X to the approximate kernel feature space.

vendor_used()

The GPU backend this estimator runs on.

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

Choose the basis rows and compute the normalization.

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 (Nystroem) – The fitted transformer.

transform(X)[source]#

Map X to the approximate kernel feature space.

Parameters:

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

Returns:

Array of shape (n_rows, n_components), float32 – K(X, components_) @ normalization_.T.

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 transformer to path as an .npz file.

Load it with Nystroem.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 transformer has not been fitted.

classmethod load(path)[source]#

Load a transformer written by save.

Parameters:

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

Returns:

Nystroem – A fitted transformer, ready to transform, with the saved numeric mode.