Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← PCA lessons
Reading a representation · ML-P05 · 12–18 MIN

Component weights and scores

Distinguish feature-axis weights from row coordinates.

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

Understand the idea

Production calls components_.T “loadings”: these are component-axis weights. Other statistical conventions use scaled loadings. Scores locate observations on the axes. An axis sign can flip without changing its information.

Distinguish feature-axis weights from row coordinates.Weights: features × axesScores: rows × axesHow is each axis defined?Where is each observation?Label feature names, row identities and component order.
Distinguish feature-axis weights from row coordinates.Scroll the diagram horizontally if needed.

What the syntax does

pca.components_.T
  • Transposes component-by-feature weights into feature-by-component columns.
index=X.columns
  • Labels each weight row with its original measurement name.

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. Build answer as the first-two-component weight table, indexed by X columns and with columns PC1 and PC2.
  2. Axis sign choices may vary.
answer
Hints

Think: components_ stores axes as rows, while the requested table uses features as rows.

Tools: Transpose, original feature names and component labels.

Approach: Take the first two axes, transpose them and attach feature/PC names.

Explained solution
answer=pd.DataFrame(pca.components_[:2].T,index=X.columns,columns=['PC1','PC2'])

The labelled weights distinguish how each original measurement contributes to each new axis.

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.