Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessonsQUESTIONS · MODELS · EVIDENCE
Class distributions · ML-C18 · 18–25 MIN

QDA allows class-specific shapes

Balance covariance flexibility against data support and stability.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeReason about the Python
  3. PractiseAdapt the Python
  4. TransferReason about the Python

Understand the idea

QDA fits a covariance shape for each class, allowing curved boundaries. More parameters require more within-class information. Production regularises covariance and limits input feature count to ten.

Balance covariance flexibility against data support and stability.Class AClass BCompare means, covariance shapes and assumptions.
Schematic · Balance covariance flexibility against data support and stability.Scroll the diagram horizontally if needed.

Python skill: Fits separate class covariance structures.

Meet the syntax

QuadraticDiscriminantAnalysis(reg_param=0.1)
QuadraticDiscriminantAnalysis
Fits separate class covariance structures.
reg_param=0.1
Regularises those covariance estimates toward a diagonal identity contribution to improve stability.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

model=QuadraticDiscriminantAnalysis(reg_param=.1).fit(X_train,y_train)
answer=model.predict(X_test)

This practice: Read and run the Python. Next: Change · QDA allows class-specific shapes.

Quadratic discriminant analysis · from idea to workflow

Question: Do classes need different covariance shapes?

Mechanism: Estimate a covariance per class to form curved boundaries.

Watch for: Many covariance parameters need enough examples per class; singular fits are unstable.

Interpret or debug · self-review: Explain why flexibility may hurt a small rare class and compare the simpler shared-covariance alternative.

Independent full workflow: ML-X14. First practise complete regression and classification in F11–F12; check readiness in W-K2–W-K3.

Given data · COV90

90 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

COV90 · first 8 prepared rows
x1x2label
-0.878151-0.683719A
-2.05616-0.327603A
1.30311.29443A
-0.995383-0.240343A
-0.654128-0.314893A
-2.112-0.509527A
-1.497460.375701A
-1.09822-0.763825A

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
x1float64
x2float64
labelstr
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.model_selection import train_test_split
X=df[['x1','x2']]
y=df.label
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2,random_state=42,stratify=y)
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis

Your task · Follow

Fit regularised QDA and predict held-away labels.

Hint 1 — Think

Regularisation stabilises the class-specific covariance estimates.

Hint 2 — Tools

QuadraticDiscriminantAnalysis with reg_param, fit and predict.

Hint 3 — Approach

Fit the declared regularised recipe on training rows and predict held-away labels.

Explained solution
model=QuadraticDiscriminantAnalysis(reg_param=.1).fit(X_train,y_train)
answer=model.predict(X_test)

The regularisation choice is explicit and the evaluation rows do not estimate class shapes.

Helpful prior knowledge: LDA shares a covariance shape These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Fit regularised QDA and predict held-away labels.

answer

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.