Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessons
Reading a representation · ML-P-R1 · 15–20 MIN

PCA retrieval

  1. Retrieval 1Retrieve the methodCurrent exercise
  2. Retrieval 2Retrieve the method
  3. Retrieval 3Compute and inspect the evidence

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.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

cumulative
  1. Cumulative sum of ratios.
retained_80
  1. Requested result described in the task and deliverables.
retained_95
  1. Requested result described in the task and deliverables.
PCA60_MIXED · first 8 rows
abcde
84.607341.797123.268544.0585-1.48542
118.32758.820124.000745.4287-0.0617574
95.996543.354823.841638.02320.0332781
96.427649.114223.051146.07730.139073
113.80962.443522.657353.2181-0.764884
111.1253.135714.811828.17741.18599
107.79656.096118.592541.5523-0.874061
105.98852.410216.652434.00580.290617
Your task
  1. Repair this variance report: its starter cumulatively sums explained_variance_ in raw variance units and uses zero-based search positions as counts.
  2. Using the supplied fitted PCA60_MIXED
  3. Return the cumulative explained-variance ratios in cumulative and the smallest sufficient counts for 80% and 95% in retained_80 and retained_95.
cumulativeretained_80retained_95
Hints

Think: A fraction-of-total requirement needs ratios, and a retained prefix needs a count.

Tools: Recall explained_variance_ratio_, cumulative sums and zero-based threshold positions.

Approach: Repair the units before repairing the index-to-count conversion; test both thresholds on one fitted spectrum.

Explained solution
cumulative = np.cumsum(pca.explained_variance_ratio_)
retained_80 = int(np.searchsorted(cumulative,.8)+1)
retained_95 = int(np.searchsorted(cumulative,.95)+1)

Explained variances and explained-variance ratios are different quantities. A threshold lookup is a position until converted into the number of retained leading components.

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.