mojolearn.LinearDiscriminantAnalysis#
- class mojolearn.LinearDiscriminantAnalysis(solver='svd', shrinkage=None, priors=None, n_components=None, store_covariance=False, tol=0.0001, covariance_estimator=None)#
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, andtransformprojects onto the most discriminative directions. Computation is float32.With
solver='svd'the projection directions (scalings_andtransform) 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 notransform.'eigen'solves the generalized eigenproblem between the between-class and within-class scatter; it raisesValueErrorwhen 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. Withsolver='svd'it raisesNotImplementedError.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 mostmin(n_classes - 1, n_features)(ValueErrorotherwise). 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
fitand acovariance_attribute, used instead of the empirical covariance with'lsqr'or'eigen'(ValueErrorwith'svd'or withshrinkageset). 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
ValueErrorwith 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)
Methods
decision_function(X)fit(X, y)fit_transform(X, y)Fit to
X, then transformX.get_params([deep])Return the constructor parameters as a dict.
predict(X)Predict the class of each row.
Return the unnormalized log score of each row and class.
Return the log of the class probabilities for each row.
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)#
Fit to
X, then transformX.- Returns:
array – The transformed data.
- get_params(deep=True)#
Return the constructor parameters as a dict.
- Parameters:
deep (bool, default True) – Accepted for scikit-learn compatibility.
- Returns:
dict – Parameter names mapped to their values.
- predict(X)#
Predict the class of each row.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to classify.
- Returns:
array of shape (n_samples,) – Predicted class labels.
- predict_joint_log_proba(X)#
Return the unnormalized log score of each row and class.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to score.
- Returns:
array of shape (n_samples, n_classes) – Log scores that
predict_log_probanormalizes, in the order ofclasses_.
- predict_log_proba(X)#
Return the log of the class probabilities for each row.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to score.
- Returns:
array of shape (n_samples, n_classes) – Log probability of each class, in the order of
classes_.
- predict_proba(X)#
Return class probabilities for each row.
Probabilities are float32 in
identicalmode. Infastmode (Apple), they are float64, so that a very confident prediction does not round to exactly 1.0.- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to score.
- Returns:
array of shape (n_samples, n_classes) – Probability of each class, in the order of
classes_.
- score(X, y)#
Return the mean accuracy on the given rows and labels.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Rows to classify.
y (array-like of shape (n_samples,)) – True labels.
- Returns:
float – Fraction of rows classified correctly.
- set_params(**params)#
Set constructor parameters.
An unknown parameter name raises
ValueError.- Returns:
self