Rank Search Trials and Flag Edge Hyperparameters

Quick Overview

Write two pandas helpers for hyperparameter search results: one ranks trials from a cv_results_-style table with stable tie-breaking, and one flags numeric hyperparameters whose best value sits at the edge of the searched range. It tests sort stability, careful type checks and reading search results.

Rank Search Trials and Flag Edge Hyperparameters

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given a `cv_results_`-style DataFrame `results`, implement two functions: - `top_configurations(results, k=3)` returns the `k` best trials. - `flag_edge_hyperparameters(results, search_space)` returns a dict that maps each numeric hyperparameter whose best-trial value is the minimum or maximum of its searched values to `"low"` or `"high"`. The functions must follow these rules: - Rank by `mean_test_score` descending. Ties keep the lower trial first. `trial` is the 0-based row position (ignore the input index). If `k` exceeds the trial count, return all trials. - The output columns, in order, are `rank` (starting at 1, `int64`), `trial` (`int64`), each `param_*` column with the prefix stripped (in the original order), `mean_test_score` and `std_test_score`. Drop all other columns and use a fresh index starting at 0. - Flagging uses the rank-1 trial. Compare values with `np.isclose`. Keys follow `search_space` order. - Skip a parameter if its space has any non-numeric value (bools count as non-numeric), fewer than 2 distinct values, no matching `param_<name>` column, or a `NaN`/`None` best value. - An empty `results` gives `{}` from `flag_edge_hyperparameters`. Example: ```python results = pd.DataFrame({ "param_alpha": [0.01, 0.1, 1.0, 1.0], "param_max_depth": [8, 4, 4, 2], "mean_test_score": [0.5, 0.6, 0.7, 0.8], "std_test_score": [0.0, 0.0, 0.0, 0.0], }) space = {"alpha": [0.01, 0.1, 1.0], "max_depth": [2, 4, 8]} flag_edge_hyperparameters(results, space) # {"alpha": "high", "max_depth": "low"} (best trial is 3) ``` ```hint Ties and order Check whether your sort keeps equal scores in their original row order, and what happens to that order if you reverse an ascending sort. ``` ### Constraints & Assumptions - `results` holds one row per trial with `param_*` columns, `mean_test_score` and `std_test_score`, and it may hold other columns that the output drops. - `search_space` maps each parameter name, without the `param_` prefix, to the list of values that were searched. ### What a Strong Answer Covers - How the ranking keeps tied trials in positional order and handles a `k` larger than the trial count. - How the output frame is built: column order, prefix stripping, dtypes and index. - How a search space is classified as numeric, including the bool case, and every condition that skips a parameter. - How the best value is compared with the ends of the range, and why an edge result matters for the next search. ### Follow-up Questions - How would you handle continuous distributions (for example `loguniform`) instead of discrete lists? - How would you use `std_test_score` to judge whether the top-ranked trial is really better than the next few? - The flags come from a single trial. How would you make the edge check less sensitive to noise in that one result?

Overview: Write two pandas helpers for hyperparameter search results: one ranks trials from a cv_results_-style table with stable tie-breaking, and one flags numeric hyperparameters whose best value sits at the edge of the searched range. It tests sort stability, careful type checks and reading search results.

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

Given a cv_results_-style DataFrame results, implement two functions:

  • top_configurations(results, k=3) returns the k best trials.
  • flag_edge_hyperparameters(results, search_space) returns a dict that maps each numeric hyperparameter whose best-trial value is the minimum or maximum of its searched values to "low" or "high" .

The functions must follow these rules:

  • Rank by mean_test_score descending. Ties keep the lower trial first. trial is the 0-based row position (ignore the input index). If k exceeds the trial count, return all trials.
  • The output columns, in order, are rank (starting at 1, int64 ), trial ( int64 ), each param_* column with the prefix stripped (in the original order), mean_test_score and std_test_score . Drop all other columns and use a fresh index starting at 0.
  • Flagging uses the rank-1 trial. Compare values with np.isclose . Keys follow search_space order.
  • Skip a parameter if its space has any non-numeric value (bools count as non-numeric), fewer than 2 distinct values, no matching param_<name> column, or a NaN / None best value.
  • An empty results gives {} from flag_edge_hyperparameters .

Example:

results = pd.DataFrame({
    "param_alpha":     [0.01, 0.1, 1.0, 1.0],
    "param_max_depth": [8, 4, 4, 2],
    "mean_test_score": [0.5, 0.6, 0.7, 0.8],
    "std_test_score":  [0.0, 0.0, 0.0, 0.0],
})
space = {"alpha": [0.01, 0.1, 1.0], "max_depth": [2, 4, 8]}

flag_edge_hyperparameters(results, space)
# {"alpha": "high", "max_depth": "low"}   (best trial is 3)

Constraints & Assumptions

  • results holds one row per trial with param_* columns, mean_test_score and std_test_score , and it may hold other columns that the output drops.
  • search_space maps each parameter name, without the param_ prefix, to the list of values that were searched.

What a Strong Answer Covers Guidance

  • How the ranking keeps tied trials in positional order and handles a k larger than the trial count.
  • How the output frame is built: column order, prefix stripping, dtypes and index.
  • How a search space is classified as numeric, including the bool case, and every condition that skips a parameter.
  • How the best value is compared with the ends of the range, and why an edge result matters for the next search.

Follow-up Questions Guidance

  • How would you handle continuous distributions (for example loguniform ) instead of discrete lists?
  • How would you use std_test_score to judge whether the top-ranked trial is really better than the next few?
  • The flags come from a single trial. How would you make the edge check less sensitive to noise in that one result?