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)[source]#

Bases: _BaseSparsePCA

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). "identical" gives the same bits on NVIDIA and AMD GPUs; "fast" applies on Apple GPUs.

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)
__init__(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)[source]#

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)[source]#

Fit the sparse components.

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

  • y (ignored)

Returns:

self