Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessonsQUESTIONS · MODELS · EVIDENCE
How much to retain · ML-P04 · 12–18 MIN

Select a retained representation

Choose the smallest component prefix meeting a variance criterion.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python

Understand the idea

Production uses the smallest prefix reaching at least 90% cumulative variance. The threshold is a stated compression criterion, not a universal optimum.

Choose the smallest component prefix meeting a variance criterion.PC1PC2Variance by componentNew axes combine measurements.
Schematic · Choose the smallest component prefix meeting a variance criterion.Scroll the diagram horizontally if needed.

Python skill: Finds the first zero-based position reaching the variance threshold.

Meet the syntax

retained = np.searchsorted(cumulative, 0.9) + 1
np.cumsum(ratios)
np.searchsorted(cumulative, 0.9)
Finds the first zero-based position reaching the variance threshold.
+ 1
Converts that position into a component count for the retained prefix.
np.cumsum(ratios)
Accumulates explained variance in component order before applying the retention threshold.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

cumulative=np.cumsum(pca.explained_variance_ratio_)
retained=int(np.searchsorted(cumulative,.9)+1)
answer=scores[:,:retained]

This practice: Read and run the Python. Next: Change · Select a retained representation.

Given data · PCA48

48 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

Reference labels are omitted from this preview and must remain outside fitting.

PCA48 · first 8 prepared 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

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
afloat64
bfloat64
cfloat64
dfloat64
efloat64
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X=df.copy()
scaler=StandardScaler()
scaled=scaler.fit_transform(X)
pca=PCA().fit(scaled)
scores=pca.transform(scaled)

Your task · Follow

Keep the fewest leading PCA components that explain at least 90% variance. Store that count in retained and the matching row scores in answer.

Hint 1 — Think

The rule asks for the first cumulative total that reaches the threshold.

Hint 2 — Tools

np.cumsum, np.searchsorted and score-column slicing.

Hint 3 — Approach

Accumulate variance ratios, locate the first qualifying component count and retain that prefix of scores.

Explained solution
cumulative=np.cumsum(pca.explained_variance_ratio_)
retained=int(np.searchsorted(cumulative,.9)+1)
answer=scores[:,:retained]

Adding one converts a zero-based position into a dimension count; the prefix retains the required variation with the fewest leading axes.

Helpful prior knowledge: Explained variance is not predictive accuracy These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Keep the fewest leading PCA components that explain at least 90% variance. Store that count in retained and the matching row scores in answer.

answer

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.