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)#

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). 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)) – 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)

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)#

Fit the model with EM.

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

  • y (ignored)

Returns:

self

transform(X)#

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()#

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

Returns:

ndarray of shape (n_features, n_features), float32

get_precision()#

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)#

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)#

Return the average log-likelihood of the samples.

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

  • y (ignored)

Returns:

float

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.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

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

Returns:

self