mojolearn.MiniBatchSparsePCA#

class mojolearn.MiniBatchSparsePCA(n_components=None, *, alpha=1, ridge_alpha=0.01, max_iter=1000, callback=None, batch_size=3, verbose=False, shuffle=True, n_jobs=None, method='lars', random_state=None, tol=0.001, max_no_improvement=10, numeric_mode=None)#

Sparse principal component analysis fitted on mini-batches.

Follows scikit-learn’s MiniBatchSparsePCA: runs MiniBatchDictionaryLearning on the centered, transposed data. Each component is scaled to unit norm.

Parameters:
  • n_components (int or None, default None) – Number of components; None uses n_features.

  • alpha (float, default 1) – Sparsity penalty.

  • ridge_alpha (float, default 0.01) – Ridge penalty used by transform.

  • max_iter (int, default 1000) – Maximum passes over the data.

  • callback (ignored)

  • batch_size (int, default 3) – Features per mini-batch (the data is transposed).

  • verbose (ignored)

  • shuffle (bool, default True) – Shuffle once before fitting.

  • n_jobs (ignored)

  • method ({“lars”, “cd”}, default “lars”) – Lasso solver name.

  • random_state (int or None, default None) – Seed for the shuffle; None means 0.

  • tol (float, default 1e-3) – Convergence tolerance on the dictionary change.

  • max_no_improvement (int or None, default 10) – Stop after this many mini-batches without improvement; None disables this test.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Variables:
  • components_ (ndarray of shape (n_components, n_features)) – Sparse components, each scaled to unit norm.

  • n_components_ (int)

  • mean_ (ndarray of shape (n_features,))

  • n_iter_ (int)

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((500, 8), dtype=np.float32)
>>> spca = mojolearn.MiniBatchSparsePCA(n_components=3, max_iter=5, random_state=0).fit(X)

Methods

fit(X[, y])

Fit the sparse components.

fit_transform(X[, y])

Fit to X, then return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

inverse_transform(X)

Map projected data back: X @ components_ + mean_.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Project X onto the sparse components by ridge regression.

fit(X, y=None)#

Fit the sparse components.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (ignored)

Returns:

self

fit_transform(X, y=None)#

Fit to X, then return transform(X).

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

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

inverse_transform(X)#

Map projected data back: X @ components_ + mean_.

Parameters:

X (array-like of shape (n_samples, n_components))

Returns:

ndarray of shape (n_samples, n_features), float32

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self

transform(X)#

Project X onto the sparse components by ridge regression.

Solves a least-squares problem with penalty ridge_alpha for each centered row, as scikit-learn does.

Parameters:

X (array-like of shape (n_samples, n_features))

Returns:

ndarray of shape (n_samples, n_components), float32