Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessons
New axes · ML-P01 · 12–18 MIN

New coordinates, not selected original columns

Distinguish PCA from feature selection and clustering.

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

Understand the idea

PCA rotates numeric measurements into component axes and can retain fewer axes. Components combine original features; they are not selected original columns or cluster assignments.

Illustrative data

Illustrative data
ColumnValues
x1, 2, 3, 4, 5, 6
y1, 3, 2, 5, 4, 6

Click the highlighted code to change the result.

scaled = StandardScaler().fit_transform(example[["x", "y"]])
example_pca = PCA(n_components=)
scores = example_pca.fit_transform(scaled)
2 components retain 100% of variance. Output: 6 rows × 2 columns.Original features003366PC2PC1-22-4042 componentsxyPC1PC2

2 components retain 100% of variance. Output: 6 rows × 2 columns.

What the syntax does

PCA(n_components=2)
  • Creates a PCA model that finds directions of greatest input variation.
  • n_components: 2, None, 0.9
  • An integer n_components requests that many axes; no argument retains all available axes.
  • Fit scale and axes on the declared discovery population.
  • Explained variance describes retained variation, not prediction accuracy.
pca.fit_transform(scaled)
  • Learns these axes and returns two coordinates per input row; it does not select two original columns.

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. Standardise the supplied measurements
  2. Create two PCA coordinates for every row, and store the coordinates in answer.
answer
Hints

Think: Call fit_transform() to learn a representation and return coordinates in the new feature space.

Tools: Use StandardScaler().fit_transform(X), PCA(n_components=2), pca.fit_transform(scaled). Read the visible syntax meanings before editing.

Approach: Standardise the supplied measurements, create two PCA coordinates for every row, and store the coordinates in answer. Keep the supplied row order and inspect the named output after running.

Explained solution
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X = df.select_dtypes(include='number')
scaled = StandardScaler().fit_transform(X)
pca = PCA(n_components=2)
answer = pca.fit_transform(scaled)

The output has one row per observation and two new coordinate columns. Each coordinate combines original measurements. This first two-axis picture introduces the API; later lessons use explained variance to decide how many components to retain.

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.