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.
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 withX_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 onX_train,y_train; reuse without refitting.
Outputs you create
answer- The requested result, with the values, labels and row order described by this task.
x1 | x2 | label |
|---|---|---|
| -0.743901 | -0.783885 | A |
| -2.12019 | -0.179627 | A |
| 1.34074 | 1.03856 | A |
| -0.952592 | -0.268834 | A |
| -0.558928 | -0.411972 | A |
| -2.15048 | -0.372685 | A |
| -1.6208 | 0.506436 | A |
| -0.975229 | -0.834521 | A |
- Build
answeras a table of fitted LDA means, with model.classes_as rows andX_traincolumn names as columns.
answerHints
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.