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.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
cumulative- Cumulative sum of ratios.
retained_80- Requested result described in the task and deliverables.
retained_95- Requested result described in the task and deliverables.
a | b | c | d | e |
|---|---|---|---|---|
| 84.6073 | 41.7971 | 23.2685 | 44.0585 | -1.48542 |
| 118.327 | 58.8201 | 24.0007 | 45.4287 | -0.0617574 |
| 95.9965 | 43.3548 | 23.8416 | 38.0232 | 0.0332781 |
| 96.4276 | 49.1142 | 23.0511 | 46.0773 | 0.139073 |
| 113.809 | 62.4435 | 22.6573 | 53.2181 | -0.764884 |
| 111.12 | 53.1357 | 14.8118 | 28.1774 | 1.18599 |
| 107.796 | 56.0961 | 18.5925 | 41.5523 | -0.874061 |
| 105.988 | 52.4102 | 16.6524 | 34.0058 | 0.290617 |
- Repair this variance report: its starter cumulatively sums explained_variance_ in raw variance units and uses zero-based search positions as counts.
- Using the supplied fitted PCA60_MIXED
- Return the
cumulativeexplained-variance ratios incumulativeand the smallest sufficient counts for 80% and 95% inretained_80andretained_95.
cumulativeretained_80retained_95Hints
Think: A fraction-of-total requirement needs ratios, and a retained prefix needs a count.
Tools: Recall explained_variance_ratio_, cumulative sums and zero-based threshold positions.
Approach: Repair the units before repairing the index-to-count conversion; test both thresholds on one fitted spectrum.
Explained solution
cumulative = np.cumsum(pca.explained_variance_ratio_)
retained_80 = int(np.searchsorted(cumulative,.8)+1)
retained_95 = int(np.searchsorted(cumulative,.95)+1)
Explained variances and explained-variance ratios are different quantities. A threshold lookup is a position until converted into the number of retained leading components.