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.
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- The requested result, with the values, labels and row order described by this task.
counts- Requested result described in the task and deliverables.
length | width | label |
|---|---|---|
| 236.033 | -0.805568 | A |
| 581.297 | 0.728558 | A |
| -1511.27 | -1.00866 | A |
| 99.0248 | -0.24496 | A |
| -13.0141 | -0.660765 | A |
| 681.179 | 0.602475 | A |
| 51.1472 | 0.873157 | A |
| 362.131 | -0.665605 | A |
- Store each class observation count in
counts. - Calculate the sample covariance of
lengthandwidthwithin class A and store that feature-labelled table inanswer.
answercountsHints
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.