Tune a random forest with RandomizedSearchCV and score it on a held-out set

Quick Overview

Tune a random forest classifier with RandomizedSearchCV and scipy.stats distributions, summarize the cross-validation results as a tidy DataFrame, and score the best model on held-out data. It tests choosing sampling distributions, reproducible seeding and the gap between CV and test scores.

Tune a random forest with RandomizedSearchCV and score it on a held-out set

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Tune a `RandomForestClassifier` with `RandomizedSearchCV` using `scipy.stats` distributions, summarize `cv_results_` as a DataFrame, and score the refitted best model on a held-out set. Implement these four functions: ```python def make_param_distributions(): ... def run_random_search(X_train, y_train, n_iter, random_state, cv=3): ... def summarize_cv_results(search): ... def held_out_score(search, X_test, y_test): ... ``` Requirements: - `make_param_distributions()` returns a dict with exactly the keys `max_depth` = `randint(2, 9)`, `n_estimators` = `randint(5, 31)` and `min_samples_leaf` = `loguniform(0.01, 0.2)`. - `run_random_search(X_train, y_train, n_iter, random_state, cv=3)` returns the fitted search, with `scoring="accuracy"`, `refit=True`, and `random_state` passed to both the search and the forest. - `summarize_cv_results(search)` returns a DataFrame with the columns `max_depth`, `n_estimators` (int64), `min_samples_leaf`, `mean_test_score`, `std_test_score` (float64) and `rank_test_score` (int64), in that order. - Sort the summary by `rank_test_score` ascending with a stable sort and reset the index. - `held_out_score(search, X_test, y_test)` returns the accuracy of `best_estimator_` on the test set as a Python `float`. ```hint Reading cv_results_ `cv_results_` stores the sampled values in more than one place. Pick the one that makes the required int64 and float64 dtypes easy to guarantee. ``` ### Constraints & Assumptions - The caller provides the train/test split. - The data is a binary classification problem. ### What a Strong Answer Covers - Builds the three distributions with the right bounds, knowing that the upper bound of `randint` is exclusive and that a float `min_samples_leaf` is a fraction of the training rows. - Seeds both the search and the forest and can say what each seed controls. - Produces the summary with the exact columns, dtypes and order, sorted stably by rank with a fresh index. - Scores only the refitted best estimator on the test set and returns a Python float. - Explains why the test score is an honest estimate and `best_score_` is not. ### Follow-up Questions - What does `loguniform` buy you over `uniform` for `min_samples_leaf`? - How would you choose `n_iter` for a fixed compute budget? - Why is the held-out score usually lower than `best_score_`?

Overview: Tune a random forest classifier with RandomizedSearchCV and scipy.stats distributions, summarize the cross-validation results as a tidy DataFrame, and score the best model on held-out data. It tests choosing sampling distributions, reproducible seeding and the gap between CV and test scores.

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

Tune a RandomForestClassifier with RandomizedSearchCV using scipy.stats distributions, summarize cv_results_ as a DataFrame, and score the refitted best model on a held-out set. Implement these four functions:

def make_param_distributions(): ...

def run_random_search(X_train, y_train, n_iter, random_state, cv=3): ...

def summarize_cv_results(search): ...

def held_out_score(search, X_test, y_test): ...

Requirements:

  • make_param_distributions() returns a dict with exactly the keys max_depth = randint(2, 9) , n_estimators = randint(5, 31) and min_samples_leaf = loguniform(0.01, 0.2) .
  • run_random_search(X_train, y_train, n_iter, random_state, cv=3) returns the fitted search, with scoring="accuracy" , refit=True , and random_state passed to both the search and the forest.
  • summarize_cv_results(search) returns a DataFrame with the columns max_depth , n_estimators (int64), min_samples_leaf , mean_test_score , std_test_score (float64) and rank_test_score (int64), in that order.
  • Sort the summary by rank_test_score ascending with a stable sort and reset the index.
  • held_out_score(search, X_test, y_test) returns the accuracy of best_estimator_ on the test set as a Python float .

Constraints & Assumptions

  • The caller provides the train/test split.
  • The data is a binary classification problem.

What a Strong Answer Covers Guidance

  • Builds the three distributions with the right bounds, knowing that the upper bound of randint is exclusive and that a float min_samples_leaf is a fraction of the training rows.
  • Seeds both the search and the forest and can say what each seed controls.
  • Produces the summary with the exact columns, dtypes and order, sorted stably by rank with a fresh index.
  • Scores only the refitted best estimator on the test set and returns a Python float.
  • Explains why the test score is an honest estimate and best_score_ is not.

Follow-up Questions Guidance

  • What does loguniform buy you over uniform for min_samples_leaf ?
  • How would you choose n_iter for a fixed compute budget?
  • Why is the held-out score usually lower than best_score_ ?