mojolearn.IncrementalPCA#

class mojolearn.IncrementalPCA(n_components=None, *, whiten=False, copy=True, batch_size=None, numeric_mode=None)[source]#

Bases: _Base

Incremental principal component analysis, fitted batch by batch.

Follows scikit-learn’s IncrementalPCA: the mean, variance and components are updated one batch at a time, so a dataset larger than memory can be fitted through partial_fit. Component signs follow scikit-learn’s svd_flip on the rows of the components. A scipy.sparse input is densified one batch at a time in fit and whole in partial_fit and transform.

Parameters:
  • n_components (int or None, default None) – Number of components to keep. None keeps n_features (or, in a later partial_fit, the number already fitted). Must be between 1 and n_features and at most the number of rows in each batch.

  • whiten (bool, default False) – Divide the projected data by the square root of each component’s explained variance.

  • copy (bool, default True) – Accepted for scikit-learn compatibility; the input is never modified.

  • batch_size (int or None, default None) – Rows per batch in fit. None uses 5 * n_features.

  • 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)) – Principal axes, sorted by decreasing explained variance.

  • explained_variance_ (ndarray of shape (n_components,))

  • explained_variance_ratio_ (ndarray of shape (n_components,))

  • singular_values_ (ndarray of shape (n_components,))

  • mean_ (ndarray of shape (n_features,))

  • var_ (ndarray of shape (n_features,))

  • noise_variance_ (float)

  • n_components_ (int)

  • n_samples_seen_ (int)

  • n_features_in_ (int)

  • batch_size_ (int)

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> model = mojolearn.IncrementalPCA(n_components=3, batch_size=200).fit(X)
>>> model.transform(X).shape
(1000, 3)
__init__(n_components=None, *, whiten=False, copy=True, batch_size=None, numeric_mode=None)[source]#

Methods

fit(X[, y])

Fit the model on X in batches of batch_size rows.

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 to the original feature space.

partial_fit(X[, y])

Update the model with one more batch.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Project X onto the fitted components.

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

Fit the model on X in batches of batch_size rows.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data. Any earlier fit is discarded.

  • y (ignored)

Returns:

self

Raises:

ValueError – If X is empty or n_components is out of range.

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

Update the model with one more batch.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – The batch. n_features must match earlier batches and n_samples must be at least n_components.

  • y (ignored)

Returns:

self

Raises:

ValueError – If the number of features changed or n_components is out of range for this batch.

transform(X)[source]#

Project X onto the fitted components.

Parameters:

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

Returns:

ndarray of shape (n_samples, n_components), float32

Raises:
  • RuntimeError – If the model is not fitted.

  • ValueError – If X has a different number of features than the fitted data.

inverse_transform(X)[source]#

Map projected data back to the original feature space.

Parameters:

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

Returns:

ndarray of shape (n_samples, n_features), float32