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.
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 onX; reuse without refitting. scaled- Standardised features in the same row/column order as
X.scaled = scaler.fit_transform(X) pca- Already fitted on scaled.
PCAfitted 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- The requested result, with the values, labels and row order described by this task.
a | b | c | d | e |
|---|---|---|---|---|
| 103.047 | 50.8622 | 22.7157 | 44.6184 | 0.906993 |
| 89.6002 | 44.3015 | 20.2703 | 40.1253 | -0.680116 |
| 107.505 | 53.9521 | 21.1565 | 41.7009 | 0.818161 |
| 109.406 | 54.2501 | 22.5252 | 44.9095 | 1.39291 |
| 80.4896 | 40.0557 | 14.1714 | 29.4087 | -3.27953 |
| 86.9782 | 44.1387 | 18.7213 | 37.5997 | -1.70077 |
| 101.278 | 50.4611 | 18.1185 | 36.0784 | -0.343557 |
| 96.8376 | 48.7875 | 17.4445 | 33.8533 | -0.987809 |
- Build
answeras the first-two-component weight table, indexed byXcolumns and with columnsPC1andPC2. - Axis sign choices may vary.
answerHints
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.