How Many Directions?

Machine Learning in CodeMedium+10 XP+10

How Many Directions?

The breast cancer dataset has 30 columns, and many of them repeat each other. How many PCA directions does it take to hold a given share of the variation?

Task: write directions_needed(threshold).

  • Load the features with load_breast_cancer(return_X_y=True).
  • Scale them with StandardScaler().fit_transform(X).
  • Fit PCA(n_components=threshold), where threshold is a fraction between 0 and 1.
  • Return [number_of_directions_kept, total_share_kept]. The first is a plain int. The second is the sum of explained_variance_ratio_, as a plain float rounded to 3 decimal places.

Examples

threshold = 0.8
returns [5, 0.847]
threshold = 0.9
returns [7, 0.91]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run