Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

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

LDA shares a covariance shape

Understand shared class geometry and linear boundaries.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferDeeper investigation · affine decision scores
  4. ApplyBuild a guided full workflow

Understand the idea

LDA fits different class means with a shared within-class covariance shape, producing linear decision boundaries. Start by reading those means and comparing a shrinkage choice on training folds. The midpoint score identity is an optional deeper investigation, introduced beside its own code.

Understand shared class geometry and linear boundaries.Class AClass BLDA: different class means, one shared covariance shape.
Understand shared class geometry and linear boundaries.Scroll the diagram horizontally if needed.

What the syntax does

LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto')
  • Creates a linear discriminant analysis classifier.
  • shrinkage: None, "auto", 0.1
  • solver=svd is the sklearn default.
  • The authored lsqr path supports shrinkage: None uses the covariance estimate directly, auto estimates shrinkage, and a number from 0 to 1 sets its strength.
  • Use matching training folds to investigate choices.
model.means_
  • Reads the fitted class-by-feature mean matrix; label its axes with classes_ and feature names.

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)
model
Already fitted on X_train, y_train. Fitted estimator or pipeline for the requested model family. Fitted on X_train, y_train; reuse without refitting.

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
COV90_SHARED · first 8 rows
x1x2label
-0.743901-0.783885A
-2.12019-0.179627A
1.340741.03856A
-0.952592-0.268834A
-0.558928-0.411972A
-2.15048-0.372685A
-1.62080.506436A
-0.975229-0.834521A
Your task
  1. Build answer as a table of fitted LDA means, with model.classes_ as rows and X_train column names as columns.
answer
Hints

Think: Class means need the same feature names and class order used by the fitted model.

Tools: means_, classes_ and a labelled dataframe.

Approach: Read the fitted means and attach class rows plus training-feature columns.

Explained solution
answer=pd.DataFrame(model.means_,index=model.classes_,columns=X_train.columns)

The table exposes each class centre while leaving the shared covariance assumption conceptually separate.

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.