mojolearn.FactorAnalysis#

class mojolearn.FactorAnalysis(n_components=None, *, tol=0.01, copy=True, max_iter=1000, noise_variance_init=None, svd_method='randomized', iterated_power=3, rotation=None, random_state=0, numeric_mode=None)[source]#

Bases: _Base

Factor analysis: a linear Gaussian latent variable model.

Follows scikit-learn’s FactorAnalysis, fitted by EM. Both svd_method values use an exact SVD at every iteration (scikit-learn’s "randomized" is an approximation of it), so iterated_power and random_state have no effect.

Parameters:
  • n_components (int or None, default None) – Dimension of the latent space; None uses n_features.

  • tol (float, default 1e-2) – Stop when the log-likelihood gain falls below this.

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

  • max_iter (int, default 1000) – Maximum number of EM iterations.

  • noise_variance_init (array-like of shape (n_features,) or None, default None) – Starting noise variances; None uses ones.

  • svd_method ({“lapack”, “randomized”}, default “randomized”) – Accepted for scikit-learn compatibility; both run the exact SVD.

  • iterated_power (int, default 3) – Accepted for scikit-learn compatibility; unused.

  • rotation ({“varimax”, “quartimax”} or None, default None) – Rotation applied to the fitted components.

  • random_state (int or None, default 0) – Accepted for scikit-learn compatibility; unused.

  • 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)) – Factor loadings.

  • noise_variance_ (ndarray of shape (n_features,))

  • mean_ (ndarray of shape (n_features,))

  • loglike_ (list of float) – Log-likelihood at each iteration.

  • n_iter_ (int)

  • n_features_in_ (int)

Raises:

ValueError – For an unsupported svd_method or rotation, or a noise_variance_init of the wrong length.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> fa = mojolearn.FactorAnalysis(n_components=2).fit(X)
>>> Z = fa.transform(X)
__init__(n_components=None, *, tol=0.01, copy=True, max_iter=1000, noise_variance_init=None, svd_method='randomized', iterated_power=3, rotation=None, random_state=0, numeric_mode=None)[source]#

Methods

fit(X[, y])

Fit the model with EM.

fit_transform(X[, y])

Fit to X, then return transform(X).

get_covariance()

Return the model covariance components_.T @ components_ + diag(noise_variance_).

get_params([deep])

Return this estimator's constructor parameters.

get_precision()

Return the inverse of the model covariance.

score(X[, y])

Return the average log-likelihood of the samples.

score_samples(X)

Return the log-likelihood of each sample under the model.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Return the expected latent variables for each sample.

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

Fit the model with EM.

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

  • y (ignored)

Returns:

self

transform(X)[source]#

Return the expected latent variables for each sample.

Parameters:

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

Returns:

ndarray of shape (n_samples, n_components), float32

get_covariance()[source]#

Return the model covariance components_.T @ components_ + diag(noise_variance_).

Returns:

ndarray of shape (n_features, n_features), float32

get_precision()[source]#

Return the inverse of the model covariance.

Returns:

ndarray of shape (n_features, n_features), float32

Raises:

ValueError – If the covariance is singular.

score_samples(X)[source]#

Return the log-likelihood of each sample under the model.

Parameters:

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

Returns:

ndarray of shape (n_samples,), float32

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

Return the average log-likelihood of the samples.

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

  • y (ignored)

Returns:

float