Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessons
How much to retain · ML-P03 · 12–18 MIN

Explained variance is not predictive accuracy

Interpret per-component variation.

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

Understand the idea

Explained variance ratios measure variation along fitted axes relative to total variation in the prepared inputs. They say nothing directly about predicting a target.

Interpret per-component variation.PC160%PC225%PC310%PC45%Example variance shares sum to 100%; these are not accuracy.
Interpret per-component variation.Scroll the diagram horizontally if needed.

What the syntax does

pca.explained_variance_ratio_
  • Reads each component’s share of input variance in fitted order; these values are not prediction scores.

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.copy()
scaler
Already fitted coordinate system learned from X; transform reuses its statistics without fitting again. Fitted on X; reuse without refitting.
scaled
Standardised features in the same row/column order as X.
scaled = scaler.fit_transform(X)
pca
Already fitted on scaled. PCA fitted on scaled measurements. Fitted on scaled; reuse without refitting.
scores
Supplied scores; its definition below specifies the values and role.
scores = pca.transform(scaled)

Outputs you create

answer
  1. The requested result, with the values, labels and row order described by this task.
PCA48 · first 8 rows
abcde
103.04750.862222.715744.61840.906993
89.600244.301520.270340.1253-0.680116
107.50553.952121.156541.70090.818161
109.40654.250122.525244.90951.39291
80.489640.055714.171429.4087-3.27953
86.978244.138718.721337.5997-1.70077
101.27850.461118.118536.0784-0.343557
96.837648.787517.444533.8533-0.987809
Your task
  1. Inspect the explained variance ratios.
answer
Hints

Think: Variance ratios refer to the representation on which PCA was fitted.

Tools: explained_variance_ratio_.

Approach: Read the fitted per-component ratio array in component order.

Explained solution
answer=pca.explained_variance_ratio_

The ratios describe how input variation is distributed across axes; they are not prediction scores.

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.