One-Standard-Error Lambda Selection

Quick Overview

Implement the one-standard-error rule for choosing a regularization strength from a grid of cross-validation fold errors in NumPy. It tests standard error computation with the sample standard deviation, tie handling, an inclusive threshold, order-invariant selection and input validation.

One-Standard-Error Lambda Selection

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Given `lambdas` (1-D, any order) and `fold_errors` of shape `(n_lambdas, n_folds)`, implement two functions: - `cv_mean_and_se(fold_errors)` returns the per-lambda mean error and standard error. - `one_standard_error_lambda(lambdas, fold_errors)` returns, as a `float`, the largest lambda whose mean error is at most the minimum mean plus the SE at the minimizer. Rules: - SE = sample std across folds (`ddof=1`) / `sqrt(n_folds)`. - If several lambdas tie for the minimum mean, the largest of them supplies the reference SE. - The threshold comparison is inclusive (`<=`). - Raise `ValueError` if `fold_errors` is not 2-D, has fewer than 2 folds, `lambdas` is not 1-D, or the row count differs from `len(lambdas)`. Example: ```python lambdas = [1.0, 0.5, 0.25, 0.125] fold_errors = [[5, 6, 7], [3, 3.5, 4], [2, 3, 4], [2.5, 3.5, 4.5]] # means: [6, 3.5, 3, 3.5], min at 0.25 with se = 1/sqrt(3) ≈ 0.577 # threshold ≈ 3.577 -> eligible {0.5, 0.25, 0.125} # Output: 0.5 ``` ```hint Row order Select by lambda value, since the rows can arrive in any order. Test your code with the rows shuffled and with two lambdas tied at the minimum mean. ``` ### Constraints & Assumptions - `n_folds >= 2` and all errors are finite. - The result must not depend on the order of `lambdas` (rows of `fold_errors` are permuted together with it). ### What a Strong Answer Covers - Per-lambda mean and standard error, using the sample standard deviation with `ddof=1` divided by `sqrt(n_folds)`. - The tie rule for the reference point and the inclusive threshold. - Selection by lambda value, so the answer is the same for any ordering of the path. - All four `ValueError` checks and a plain `float` return value. ### Follow-up Questions - Why prefer the 1-SE lambda over the CV minimizer for lasso? - Is the fold-based SE a trustworthy estimate of uncertainty? - How would you generalize this to a k-SE rule or to models without a single strength parameter?

Overview: Implement the one-standard-error rule for choosing a regularization strength from a grid of cross-validation fold errors in NumPy. It tests standard error computation with the sample standard deviation, tie handling, an inclusive threshold, order-invariant selection and input validation.

PracHub logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Given lambdas (1-D, any order) and fold_errors of shape (n_lambdas, n_folds), implement two functions:

  • cv_mean_and_se(fold_errors) returns the per-lambda mean error and standard error.
  • one_standard_error_lambda(lambdas, fold_errors) returns, as a float , the largest lambda whose mean error is at most the minimum mean plus the SE at the minimizer.

Rules:

  • SE = sample std across folds ( ddof=1 ) / sqrt(n_folds) .
  • If several lambdas tie for the minimum mean, the largest of them supplies the reference SE.
  • The threshold comparison is inclusive ( <= ).
  • Raise ValueError if fold_errors is not 2-D, has fewer than 2 folds, lambdas is not 1-D, or the row count differs from len(lambdas) .

Example:

lambdas = [1.0, 0.5, 0.25, 0.125]
fold_errors = [[5, 6, 7], [3, 3.5, 4], [2, 3, 4], [2.5, 3.5, 4.5]]
# means: [6, 3.5, 3, 3.5], min at 0.25 with se = 1/sqrt(3) ≈ 0.577
# threshold ≈ 3.577 -> eligible {0.5, 0.25, 0.125}
# Output: 0.5

Constraints & Assumptions

  • n_folds >= 2 and all errors are finite.
  • The result must not depend on the order of lambdas (rows of fold_errors are permuted together with it).

What a Strong Answer Covers Guidance

  • Per-lambda mean and standard error, using the sample standard deviation with ddof=1 divided by sqrt(n_folds) .
  • The tie rule for the reference point and the inclusive threshold.
  • Selection by lambda value, so the answer is the same for any ordering of the path.
  • All four ValueError checks and a plain float return value.

Follow-up Questions Guidance

  • Why prefer the 1-SE lambda over the CV minimizer for lasso?
  • Is the fold-based SE a trustworthy estimate of uncertainty?
  • How would you generalize this to a k-SE rule or to models without a single strength parameter?