Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn LinearDiscriminantAnalysis

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Discriminant Analysis
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for classification and dimensionality reduction using Linear Discriminant Analysis with class-conditional Gaussian densities provided by scikit-learn.

Description

LinearDiscriminantAnalysis (LDA) is a classifier with a linear decision boundary, generated by fitting class-conditional Gaussian densities to the data and using Bayes' rule. The model assumes that all classes share the same covariance matrix. LDA can also be used for dimensionality reduction by projecting data onto the most discriminative directions via the transform method. It supports three solvers: SVD (no covariance matrix computation, recommended for high-dimensional data), LSQR (least squares, supports shrinkage), and Eigen (eigenvalue decomposition, supports shrinkage and custom covariance estimators).

Usage

Use LinearDiscriminantAnalysis when you need a fast, interpretable classifier that works well with small sample sizes relative to the number of features. It is also effective as a supervised dimensionality reduction method before applying other classifiers. LDA is particularly well-suited when the class-conditional distributions are approximately Gaussian with equal covariance matrices.

Code Reference

Source Location

Signature

class LinearDiscriminantAnalysis(
    ClassNamePrefixFeaturesOutMixin,
    LinearClassifierMixin,
    TransformerMixin,
    BaseEstimator,
):
    def __init__(
        self,
        solver="svd",
        shrinkage=None,
        priors=None,
        n_components=None,
        store_covariance=False,
        tol=1e-4,
        covariance_estimator=None,
    ):

Import

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

I/O Contract

Inputs

Name Type Required Description
solver str No Solver: 'svd' (default), 'lsqr', or 'eigen'
shrinkage str, float, or None No Shrinkage parameter: None, 'auto' (Ledoit-Wolf), or float in [0,1] (default=None)
priors array-like of shape (n_classes,) No Class prior probabilities; inferred from data if None (default=None)
n_components int No Number of components for dimensionality reduction; at most min(n_classes-1, n_features) (default=None)
store_covariance bool No Whether to compute and store the class covariance matrices (default=False)
tol float No Absolute threshold for considering a singular value significant (default=1e-4)
covariance_estimator estimator No Custom covariance estimator with fit method and covariance_ attribute (default=None)

Outputs

Name Type Description
coef_ ndarray of shape (n_features,) or (n_classes, n_features) Weight vector(s) for the decision function
intercept_ ndarray of shape (n_classes,) Intercept term in the decision function
covariance_ ndarray of shape (n_features, n_features) Weighted within-class covariance matrix (if store_covariance=True)
means_ ndarray of shape (n_classes, n_features) Class-wise means
priors_ ndarray of shape (n_classes,) Class prior probabilities
scalings_ ndarray of shape (rank, n_classes-1) Scaling of features in the space spanned by the class centroids
classes_ ndarray of shape (n_classes,) Unique class labels
explained_variance_ratio_ ndarray of shape (n_components,) Percentage of variance explained by each selected component

Usage Examples

Basic Usage

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
model = LinearDiscriminantAnalysis(n_components=2)
model.fit(X, y)
X_transformed = model.transform(X)
print("Score:", model.score(X, y))
print("Transformed shape:", X_transformed.shape)
print("Explained variance ratio:", model.explained_variance_ratio_)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment