Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Class distributions · ML-C18 · 25–40 MIN

QDA allows class-specific shapes

Balance covariance flexibility against data support and stability.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. PractiseAdapt the Python
  4. TransferCompute and inspect the evidence
  5. ApplyBuild a guided full workflow

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.
Balance covariance flexibility against data support and stability.Scroll the diagram horizontally if needed.

What the syntax does

QuadraticDiscriminantAnalysis(reg_param=0.1)
  • Creates a quadratic discriminant analysis classifier.
  • reg_param: 0, 0.1, 0.5, 1
  • reg_param is between 0 and 1, default 0.
  • A positive value regularises the covariance toward a diagonal identity contribution.
  • This lesson requires positive regularisation; judge it using training validation, not final-test performance.

Supplied for this practice

These objects are prepared afresh before your editor runs.

X
Feature dataframe for the declared population, preserving row indices.
X = df[['x1', 'x2']]
y
Target series, aligned with X.
y = df.label
X_train / X_test / y_train / y_test
Training feature rows from the declared split. Final-test feature rows from the declared split. Training targets, aligned with X_train. Final-test targets, aligned with X_test.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
COV90 · first 8 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
Your task
  1. Fit regularised QDA and predict held-away labels.
answer
Hints

Think: Regularisation stabilises the class-specific covariance estimates.

Tools: QuadraticDiscriminantAnalysis with reg_param, fit and predict.

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.

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.