Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessons
New axes · ML-P02 · 20–30 MIN

Fit a reusable PCA representation

Transform rows with the fitted axes and scale.

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferAdapt the Python
  4. ApplyIntegrate the methods taught so far

Understand the idea

Standardisation changes which variation PCA emphasises. Fit the representation on the intended population; transform new rows using those same fitted statistics and axes.

Transform rows with the fitted axes and scale.Fitting rowsFit scaleFit PCA axesIncoming rowsReuse fitted scaleReuse fitted axesLearn scale and axes once; transform preserves their fitting boundary.
Transform rows with the fitted axes and scale.Scroll the diagram horizontally if needed.

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. Store the supplied PCA component scores for every PCA48 row in answer.
answer
Hints

Think: Scores are each observation’s coordinates along the fitted component axes.

Tools: The supplied PCA scores array.

Approach: Return the prepared component-score matrix and inspect its row/column shape.

Explained solution
answer=scores

Rows remain observations while columns become component coordinates rather than original measurements.

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.