Linear Discriminant Analysis
Anonymous contributor
Published Dec 21, 2024
Contribute to Docs
In Sklearn, Linear Discriminant Analysis (LDA) is a supervised algorithm that aims to project data onto a lower-dimensional space while preserving the information that discriminates between different classes. LDA finds a set of directions in the original feature space that maximize the separation between the classes. These directions are called discriminant directions. By projecting the data onto these directions, LDA reduces the dimensionality of the data while retaining the information that is most relevant for classification.
Syntax
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
# Create an LDA model
model = LinearDiscriminantAnalysis(
solver='svd',
shrinkage=None,
priors=None,
n_components=None,
store_covariance=False,
tol=0.0001,
covariance_estimator=None
)
# Fit the model to the training data
model.fit(X_train, y_train)
# Make predictions on the new data
y_pred = model.predict(X_test)
solver
: The solver to be used. Common options include:svd
: Singular Value Decomposition (default).lsqr
: Least Squares Solution.eigen
: Eigenvalue Decomposition.
shrinkage
: Controls the amount of shrinkage applied to the covariance matrix. Common options include:None
: No shrinkage (default).auto
: Automatic shrinkage utilizing the Ledoit-Wolf lemma.
priors
: Prior probabilities of the classes. The default value isNone
.n_components
: The number of components. The default value isNone
.store_covariance
: If set toTrue
, it explicitly calculates the covariance matrix whensolver
is set tosvd
. The default value isFalse
.tol
: The tolerance for the eigenvalue calculation. The default value is0.0001
.covariance_estimator
: Estimates the covariance matrices. The default value isNone
.
Example
The following example demonstrates the implementation of LDA:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysisfrom sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score# Load the Iris datasetiris = load_iris()X = iris.datay = iris.target# Create training and testing sets by splitting the datasetX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)# Create an LDA modelmodel = LinearDiscriminantAnalysis()# Fit the model to the training datamodel.fit(X_train, y_train)# Make predictions on the new datay_pred = model.predict(X_test)# Evaluate the modelprint("Accuracy:", accuracy_score(y_test, y_pred))
The above code produces the following output:
Accuracy: 1.0
Codebyte Example
The following codebyte example demonstrates the implementation of LDA:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Sklearn on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours