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.
Python skill: Transposes component-by-feature weights into feature-by-component columns.
Meet the syntax
weights = pd.DataFrame(pca.components_.T, index=X.columns)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.
Follow the code
Use the numbered comments to connect each Python block to the workflow above.
answer=pd.DataFrame(pca.components_[:2].T,index=X.columns,columns=['PC1','PC2'])
This practice: Read and run the Python. Next: Change · Component weights and scores.
Given data · PCA48
48 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.
Reference labels are omitted from this preview and must remain outside fitting.
| 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 |
Column meanings and units
Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.
Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.
| Column | Stored type |
|---|---|
| a | float64 |
| b | float64 |
| c | float64 |
| d | float64 |
| e | float64 |
Supplied setup · available if you need to inspect it
This code runs before your editor on every Run. These are the objects your exercise uses.
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
X=df.copy()
scaler=StandardScaler()
scaled=scaler.fit_transform(X)
pca=PCA().fit(scaled)
scores=pca.transform(scaled)
Your task · Follow
Build a labelled first-two-component weight table.
Hint 1 — Think
components_ stores axes as rows, while the requested table uses features as rows.
Hint 2 — Tools
Transpose, original feature names and component labels.
Hint 3 — 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.
Helpful prior knowledge: Fit a reusable PCA representation These links are guidance, not locks.
Sources and API context
Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.