mojolearn.LinearDiscriminantAnalysis#

class mojolearn.LinearDiscriminantAnalysis(solver='svd', shrinkage=None, priors=None, n_components=None, store_covariance=False, tol=0.0001, covariance_estimator=None)[source]#

Bases: _Classifier

Linear discriminant analysis classifier and supervised dimensionality reduction.

Follows scikit-learn’s LinearDiscriminantAnalysis. It fits one Gaussian per class with a shared covariance, classifies by the resulting linear decision function, and transform projects onto the most discriminative directions. Computation is float32.

With solver='svd' the projection directions (scalings_ and transform) match scikit-learn’s up to the sign of each component, and the decision function matches outright.

Parameters:
  • solver ({‘svd’, ‘lsqr’, ‘eigen’}, default ‘svd’) – 'svd' needs no covariance matrix. 'lsqr' solves with the pooled class covariance (minimum-norm least squares) and has no transform. 'eigen' solves the generalized eigenproblem between the between-class and within-class scatter; it raises ValueError when the within-class covariance is not positive definite.

  • shrinkage ({None, ‘auto’} or float, default None) – Covariance shrinkage for 'lsqr' and 'eigen': 'auto' uses the Ledoit-Wolf estimate on standardized classes, a float in [0, 1] sets it directly. With solver='svd' it raises NotImplementedError.

  • priors (array-like of shape (n_classes,), default None) – Class priors (nonnegative); renormalized when they do not sum to one. None uses the class frequencies.

  • n_components (int, default None) – Number of components kept by transform, at most min(n_classes - 1, n_features) (ValueError otherwise). None keeps that maximum.

  • store_covariance (bool, default False) – Store the pooled within-class covariance in covariance_ (always stored by 'lsqr' and 'eigen').

  • tol (float, default 1e-4) – Threshold below which singular values are treated as zero by 'svd'.

  • covariance_estimator (object, default None) – An estimator with fit and a covariance_ attribute, used instead of the empirical covariance with 'lsqr' or 'eigen' (ValueError with 'svd' or with shrinkage set). It is fitted on the host on each class’s float32 rows (and, for 'eigen', on all rows).

Variables:
  • classes_ (array of shape (n_classes,)) – Class labels.

  • coef_ (array of shape (n_classes, n_features), or (1, n_features) for two classes) – Weights of the decision function.

  • intercept_ (array of shape (n_classes,), or (1,) for two classes) – Intercepts of the decision function.

  • means_ (array of shape (n_classes, n_features)) – Class means.

  • priors_ (array of shape (n_classes,)) – Class priors used.

  • xbar_ (array of shape (n_features,)) – Overall mean.

  • scalings_ (array of shape (n_features, rank)) – Projection directions.

  • explained_variance_ratio_ (array of shape (n_components,)) – Share of between-class variance explained by each component.

  • covariance_ (array of shape (n_features, n_features)) – Pooled within-class covariance, when stored.

  • n_features_in_ (int) – Number of columns seen in fit.

  • numeric_mode_ (str) – Numeric mode the estimator was fitted in; prediction runs in the same mode.

Notes

Fit raises ValueError with fewer than two classes or with no more rows than classes.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = rng.integers(0, 3, 1000)
>>> lda = mojolearn.LinearDiscriminantAnalysis(n_components=2).fit(X, y)
>>> Z = lda.transform(X)
__init__(solver='svd', shrinkage=None, priors=None, n_components=None, store_covariance=False, tol=0.0001, covariance_estimator=None)[source]#

Methods

decision_function(X)

fit(X, y)

fit_transform(X, y)

Fit to X, then transform X.

get_params([deep])

Return the constructor parameters as a dict.

predict(X)

Predict the class of each row.

predict_joint_log_proba(X)

Return the unnormalized log score of each row and class.

predict_log_proba(X)

Return the log of the class probabilities for each row.

predict_proba(X)

Return class probabilities for each row.

score(X, y)

Return the mean accuracy on the given rows and labels.

set_params(**params)

Set constructor parameters.

transform(X)

fit_transform(X, y)[source]#

Fit to X, then transform X.

Returns:

array – The transformed data.