Squash, Then Classify

Machine Learning in CodeMedium+10 XP+10

Squash, Then Classify

PCA is a transformer like a scaler, so it can sit inside a pipeline in front of a model. How few directions can a classifier get away with?

Task: write accuracy_with_directions(n_components).

  • Load load_breast_cancer(return_X_y=True).
  • Split with train_test_split(X, y, test_size=0.25, random_state=0, stratify=y).
  • Build make_pipeline(StandardScaler(), PCA(n_components=n_components), LogisticRegression(max_iter=1000)) and fit it on the training rows.
  • Return its accuracy on the test rows, as a plain float rounded to 3 decimal places.

Examples

n_components = 1
returns 0.902
n_components = 5
returns 0.965

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