Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Classification lessons
Class distributions · ML-C19 · 12–18 MIN

Covariance support and regularisation limits

Qualify numerical stability and unit effects.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeCompute and inspect the evidence
  3. TransferCompute and inspect the evidence

Understand the idea

Regularisation can stabilise a covariance estimate but cannot manufacture missing class information. Adding a regularising identity term also means unit choices can matter.

Qualify numerical stability and unit effects.Class AClass BCompare means, covariance shapes and assumptions.
Qualify numerical stability and unit effects.Scroll the diagram horizontally if needed.

What the syntax does

df['label'].eq('A')
  • Creates a Boolean mask selecting examples from one class.
class_a.cov()
  • Returns feature-by-feature sample covariance.
  • Its values depend on the measurement units.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
counts
  1. Requested result described in the task and deliverables.
CLASS180 · first 8 rows
lengthwidthlabel
236.033-0.805568A
581.2970.728558A
-1511.27-1.00866A
99.0248-0.24496A
-13.0141-0.660765A
681.1790.602475A
51.14720.873157A
362.131-0.665605A
Your task
  1. Store each class observation count in counts.
  2. Calculate the sample covariance of length and width within class A and store that feature-labelled table in answer.
answercounts
Hints

Think: Filter by class and use cov() to inspect the covariance information available to QDA.

Tools: Use df['label'].eq('A'), class_a.cov(). Read the visible syntax meanings before editing.

Approach: Store each class observation count in counts. Calculate the sample covariance of length and width within class A and store that feature-labelled table in answer. Keep the supplied row order and inspect the named output after running.

Explained solution
counts = df['label'].value_counts()
class_a = df.loc[df['label'].eq('A'), ['length', 'width']]
answer = class_a.cov()

QDA estimates a separate covariance structure for each class. A small class or nearly redundant measurements can make that estimate unstable. This table shows what is being estimated, not proof of stability. Use regularisation and validation; adding a parameter does not create new observations.

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.