Successive Halving Search Over a Scaler, PCA and Classifier Pipeline

Quick Overview

Build a scikit-learn HalvingRandomSearchCV over a scaler, PCA and logistic regression pipeline with a fixed search space, then tune it on a stratified train split and report test accuracy. It tests successive halving schedules, pipeline parameter naming and leak-free model evaluation.

Successive Halving Search Over a Scaler, PCA and Classifier Pipeline

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `build_search(n_features, seed=0)`, which returns an unfitted `HalvingRandomSearchCV` over a pipeline of a scaler, PCA and a classifier. Also implement `tune_and_evaluate(X, y, seed=0)`, which splits the data, fits the search on the train split and returns `(fitted_search, test_accuracy)`. Results must match a reference search exactly. - Pipeline steps, in order: `"scaler"`, `"pca"`, `"clf"`, configured exactly as shown below. - Search: `n_candidates=24`, `factor=2`, `resource="n_samples"`, `min_resources=40`, `max_resources="auto"`, `aggressive_elimination=False`, `scoring="accuracy"`, `refit=True`, `random_state=seed`. - CV: `StratifiedKFold(n_splits=3, shuffle=True, random_state=seed)`. - Split: `train_test_split(X, y, test_size=0.25, stratify=y, random_state=seed)`. Tuning sees only the train split. - `test_accuracy` is a Python `float`: the accuracy of the refitted best estimator on the test split. The pipeline and the search space: ```python pipe = Pipeline([ ("scaler", StandardScaler()), ("pca", PCA(random_state=seed)), ("clf", LogisticRegression(solver="liblinear", max_iter=1000, random_state=seed)), ]) param_distributions = { "scaler": [StandardScaler(), MinMaxScaler(), RobustScaler()], "pca__n_components": list(range(2, n_features + 1)), "clf__penalty": ["l1", "l2"], "clf__C": loguniform(1e-3, 1e2), } # 400 samples x 6 features -> n_resources_ = [40, 80, 160], n_candidates_ = [24, 12, 6] ``` ```hint Experimental import `HalvingRandomSearchCV` is still marked experimental in scikit-learn. Check what has to be imported before the class can be imported at all. ``` ```hint Where the budget ceiling comes from With `max_resources="auto"`, the ceiling is the number of rows passed to `fit`. Work out that number from the split, then count how many doublings from 40 stay under it. ``` ### Constraints & Assumptions - `X` is a numeric feature matrix of shape (n_samples, n_features) and `y` holds class labels. - The reference check uses 400 samples and 6 features, which leaves 300 training rows after the split and produces the schedule in the comment above. - scikit-learn provides the search, pipeline and metrics; SciPy provides `loguniform`. ### What a Strong Answer Covers - A pipeline with the exact step names, the whole-step `scaler` entry in the search space, and a solver that accepts both penalties. - Every listed search setting, including the seeded `StratifiedKFold` and `random_state` that make the sampled candidates and the row subsamples reproducible. - A stratified 75/25 split made before tuning, with only the training rows passed to `fit`. - Why the fitted search reports `n_resources_ = [40, 80, 160]` and `n_candidates_ = [24, 12, 6]` for 400 rows, and what `refit=True` trains after the last round. - Test accuracy computed from the refitted best estimator and returned as a Python `float`. ### Follow-up Questions - How do `n_resources_` and `n_candidates_` change if you set `aggressive_elimination=True`? - The last round compares six candidates on 160 of the 300 training rows. When can that pick a weaker configuration, and which settings would let the final round use more data? - With only 100 test rows, how would you report the uncertainty of `test_accuracy`, and how would you compare this search fairly against a plain `RandomizedSearchCV`?

Overview: Build a scikit-learn HalvingRandomSearchCV over a scaler, PCA and logistic regression pipeline with a fixed search space, then tune it on a stratified train split and report test accuracy. It tests successive halving schedules, pipeline parameter naming and leak-free model evaluation.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement build_search(n_features, seed=0), which returns an unfitted HalvingRandomSearchCV over a pipeline of a scaler, PCA and a classifier. Also implement tune_and_evaluate(X, y, seed=0), which splits the data, fits the search on the train split and returns (fitted_search, test_accuracy). Results must match a reference search exactly.

  • Pipeline steps, in order: "scaler" , "pca" , "clf" , configured exactly as shown below.
  • Search: n_candidates=24 , factor=2 , resource="n_samples" , min_resources=40 , max_resources="auto" , aggressive_elimination=False , scoring="accuracy" , refit=True , random_state=seed .
  • CV: StratifiedKFold(n_splits=3, shuffle=True, random_state=seed) .
  • Split: train_test_split(X, y, test_size=0.25, stratify=y, random_state=seed) . Tuning sees only the train split.
  • test_accuracy is a Python float : the accuracy of the refitted best estimator on the test split.

The pipeline and the search space:

pipe = Pipeline([
    ("scaler", StandardScaler()),
    ("pca", PCA(random_state=seed)),
    ("clf", LogisticRegression(solver="liblinear", max_iter=1000, random_state=seed)),
])
param_distributions = {
    "scaler": [StandardScaler(), MinMaxScaler(), RobustScaler()],
    "pca__n_components": list(range(2, n_features + 1)),
    "clf__penalty": ["l1", "l2"],
    "clf__C": loguniform(1e-3, 1e2),
}
# 400 samples x 6 features -> n_resources_ = [40, 80, 160], n_candidates_ = [24, 12, 6]

Constraints & Assumptions

  • X is a numeric feature matrix of shape (n_samples, n_features) and y holds class labels.
  • The reference check uses 400 samples and 6 features, which leaves 300 training rows after the split and produces the schedule in the comment above.
  • scikit-learn provides the search, pipeline and metrics; SciPy provides loguniform .

What a Strong Answer Covers Guidance

  • A pipeline with the exact step names, the whole-step scaler entry in the search space, and a solver that accepts both penalties.
  • Every listed search setting, including the seeded StratifiedKFold and random_state that make the sampled candidates and the row subsamples reproducible.
  • A stratified 75/25 split made before tuning, with only the training rows passed to fit .
  • Why the fitted search reports n_resources_ = [40, 80, 160] and n_candidates_ = [24, 12, 6] for 400 rows, and what refit=True trains after the last round.
  • Test accuracy computed from the refitted best estimator and returned as a Python float .

Follow-up Questions Guidance

  • How do n_resources_ and n_candidates_ change if you set aggressive_elimination=True ?
  • The last round compares six candidates on 160 of the 300 training rows. When can that pick a weaker configuration, and which settings would let the final round use more data?
  • With only 100 test rows, how would you report the uncertainty of test_accuracy , and how would you compare this search fairly against a plain RandomizedSearchCV ?