Quadratic Discriminant Analysis
Anonymous contributor
Published Dec 21, 2024
Contribute to Docs
In Sklearn, Quadratic Discriminant Analysis (QDA) is a classification technique that assumes that the data points within each class are normally distributed. Unlike Linear Discriminant Analysis (LDA), which assumes a shared covariance matrix for all classes, QDA enables each class to have its own covariance matrix. This flexibility enables QDA to model more complex decision boundaries, making it suitable for datasets with overlapping classes or non-linear relationships between features.
Syntax
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
# Create a QDA model
model = QuadraticDiscriminantAnalysis(priors=None, reg_param=0.0, store_covariance=False, tol=0.0001)
# 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)
priors
: The prior probabilities of the classes. IfNone
, the class distribution is estimated from the training data. If specified, it should sum to 1. This allows control over the importance of each class.reg_param
: The regularization parameter. A value greater than 0 applies regularization to the covariance estimates. Regularization can help in cases where the covariance matrices might be singular or near-singular.store_covariance
: Whether to store the covariance matrices for each class. IfTrue
, the covariance matrix is explicitly computed and stored whensolver='svd'
. IfFalse
, it will not store the covariance matrices but will use them for prediction during training.tol
: The tolerance value for the eigenvalue decomposition when usingsolver='eigen'
. This helps control the precision of the eigenvalue computation.
Example
The following example demonstrates the implementation of QDA:
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysisfrom 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 a QDA modelmodel = QuadraticDiscriminantAnalysis()# 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 QDA:
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