From b384f900c03003a8cdd1b3b86a806b11a394bc12 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Sun, 15 Dec 2024 21:16:49 +0530 Subject: [PATCH 1/2] [Concept Entry] Sklearn: Quadratic Discriminant Analysis --- .../quadratic-discriminant-analysis.md | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md diff --git a/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md b/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md new file mode 100644 index 00000000000..6c0de6d4dd5 --- /dev/null +++ b/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md @@ -0,0 +1,100 @@ +--- +Title: 'Quadratic Discriminant Analysis' +Description: 'Quadratic Discriminant Analysis assumes that the data points within each class are normally distributed.' +Subjects: + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Machine Learning' + - 'Scikit-learn' + - 'Supervised Learning' + - 'Unsupervised Learning' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +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 + +```pseudo +from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis + +# Create a QDA model +model = QuadraticDiscriminantAnalysis() + +# 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) +``` + +## Example + +The following example demonstrates the implementation of QDA: + +```py +from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score + +# Load the Iris dataset +iris = load_iris() +X = iris.data +y = iris.target + +# Create training and testing sets by splitting the dataset +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) + +# Create a QDA model +model = QuadraticDiscriminantAnalysis() + +# 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) + +# Evaluate the model +print("Accuracy:", accuracy_score(y_test, y_pred)) +``` + +The above code produces the following output: + +```shell +Accuracy: 1.0 +``` + +## Codebyte Example + +The following codebyte example demonstrates the implementation of QDA: + +```codebyte/python +from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.metrics import accuracy_score + +# Load the Iris dataset +iris = load_iris() +X = iris.data +y = iris.target + +# Create training and testing sets by splitting the dataset +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=44) + +# Create a QDA model +model = QuadraticDiscriminantAnalysis() + +# 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) + +# Evaluate the model +print("Accuracy:", accuracy_score(y_test, y_pred)) +``` From 635ea1001710a5110b142409abaec964f22e70b3 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 21 Dec 2024 13:05:43 +0530 Subject: [PATCH 2/2] Update quadratic-discriminant-analysis.md minor fixes --- .../quadratic-discriminant-analysis.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md b/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md index 6c0de6d4dd5..de6d3e83781 100644 --- a/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md +++ b/content/sklearn/concepts/quadratic-discriminant-analysis/quadratic-discriminant-analysis.md @@ -1,6 +1,6 @@ --- Title: 'Quadratic Discriminant Analysis' -Description: 'Quadratic Discriminant Analysis assumes that the data points within each class are normally distributed.' +Description: 'Quadratic Discriminant Analysis is a technique that models each class with a quadratic decision boundary, assuming different covariance matrices for each class.' Subjects: - 'Data Science' - 'Machine Learning' @@ -22,7 +22,7 @@ In Sklearn, **Quadratic Discriminant Analysis (QDA)** is a classification techni from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis # Create a QDA model -model = QuadraticDiscriminantAnalysis() +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) @@ -31,6 +31,11 @@ model.fit(X_train, y_train) y_pred = model.predict(X_test) ``` +- `priors`: The prior probabilities of the classes. If `None`, 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. If `True`, the covariance matrix is explicitly computed and stored when `solver='svd'`. If `False`, it will not store the covariance matrices but will use them for prediction during training. +- `tol`: The tolerance value for the eigenvalue decomposition when using `solver='eigen'`. This helps control the precision of the eigenvalue computation. + ## Example The following example demonstrates the implementation of QDA: