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:
_BaseFactor analysis: a linear Gaussian latent variable model.
Follows scikit-learn’s
FactorAnalysis, fitted by EM. Bothsvd_methodvalues use an exact SVD at every iteration (scikit-learn’s"randomized"is an approximation of it), soiterated_powerandrandom_statehave 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:
- Raises:
ValueError – For an unsupported
svd_methodorrotation, or anoise_variance_initof 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).Return the model covariance
components_.T @ components_ + diag(noise_variance_).get_params([deep])Return this estimator's constructor parameters.
Return the inverse of the model covariance.
score(X[, y])Return the average log-likelihood of the samples.
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.