Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

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

Select a retained representation

Choose the smallest component prefix meeting a variance criterion.

  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.Leading axesCumulative variancePC160%PC1–PC285%PC1–PC395%Example: the smallest prefix reaching 90% has three axes.
Choose the smallest component prefix meeting a variance criterion.Scroll the diagram horizontally if needed.

What the syntax does

np.searchsorted(cumulative, 0.9)
  • Finds the first zero-based position reaching the variance threshold.
retained <= budget
  • Checks whether the smallest sufficient prefix fits a component storage limit; do not silently lower the stated variance requirement.

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. Keep the fewest leading PCA components that explain at least 90% variance.
  2. Store that count in retained and the matching row scores in answer.
answer
Hints

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

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

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.

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.