Detect the ConvergenceWarning from an Under-Trained scikit-learn LogisticRegression

Quick Overview

Fit a scikit-learn LogisticRegression capped at one lbfgs iteration, record the ConvergenceWarning it raises, and return the iteration count and a warning flag. It tests Python's warnings filters, how the solver decides to stop, and returning native Python types instead of NumPy scalars.

Detect the ConvergenceWarning from an Under-Trained scikit-learn LogisticRegression

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Write `def underfit_iterations(X: np.ndarray, y: np.ndarray) -> tuple[int, bool]` that fits a deliberately under-trained `LogisticRegression` and detects the resulting `sklearn.exceptions.ConvergenceWarning`. Return `(n_iterations, warned)`. The function must: - Pin the hyperparameters: `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `tol=1e-12`, `fit_intercept=True`, `max_iter=1`. - Wrap `fit` in `warnings.catch_warnings(record=True)` with `warnings.simplefilter('always')`. - Set `n_iterations` to `n_iter_[0]` as a plain Python `int` (it must equal `1`). - Set `warned` to a Python `bool` that is `True` if and only if some recorded warning satisfies `issubclass(w.category, ConvergenceWarning)`. - Return native Python types, not NumPy scalars. ```hint Check the types Look at what type `model.n_iter_[0]` actually has, and what `isinstance(value, int)` returns for it. ``` ### Constraints & Assumptions - `y` contains only the integer labels `0` and `1`, so this is a binary problem. - `X` is a numeric `(n, p)` array. The grader's data is such that lbfgs cannot meet `tol=1e-12` in one step, so the expected `n_iterations` is 1. - scikit-learn, NumPy and the standard library `warnings` module are available. ### What a Strong Answer Covers - Why `max_iter=1` together with `tol=1e-12` makes lbfgs stop at the iteration cap and triggers a `ConvergenceWarning`. - Reliable capture: `catch_warnings(record=True)` with `simplefilter('always')` inside the block, and a category check with `issubclass` that ignores unrelated warnings. - The shape of `n_iter_` for a binary problem and the conversion to native Python `int` and `bool`. - What changes when the optimizer gets enough iterations to converge. ### Follow-up Questions - Why does `simplefilter('always')` matter here? - How would you make lbfgs actually converge on this data? - In an automated training pipeline, why might you turn `ConvergenceWarning` into an error, and how would you do it?

Overview: Fit a scikit-learn LogisticRegression capped at one lbfgs iteration, record the ConvergenceWarning it raises, and return the iteration count and a warning flag. It tests Python's warnings filters, how the solver decides to stop, and returning native Python types instead of NumPy scalars.

General logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Write def underfit_iterations(X: np.ndarray, y: np.ndarray) -> tuple[int, bool] that fits a deliberately under-trained LogisticRegression and detects the resulting sklearn.exceptions.ConvergenceWarning. Return (n_iterations, warned).

The function must:

  • Pin the hyperparameters: penalty='l2' , C=1.0 , solver='lbfgs' , tol=1e-12 , fit_intercept=True , max_iter=1 .
  • Wrap fit in warnings.catch_warnings(record=True) with warnings.simplefilter('always') .
  • Set n_iterations to n_iter_[0] as a plain Python int (it must equal 1 ).
  • Set warned to a Python bool that is True if and only if some recorded warning satisfies issubclass(w.category, ConvergenceWarning) .
  • Return native Python types, not NumPy scalars.

Constraints & Assumptions

  • y contains only the integer labels 0 and 1 , so this is a binary problem.
  • X is a numeric (n, p) array. The grader's data is such that lbfgs cannot meet tol=1e-12 in one step, so the expected n_iterations is 1.
  • scikit-learn, NumPy and the standard library warnings module are available.

What a Strong Answer Covers Guidance

  • Why max_iter=1 together with tol=1e-12 makes lbfgs stop at the iteration cap and triggers a ConvergenceWarning .
  • Reliable capture: catch_warnings(record=True) with simplefilter('always') inside the block, and a category check with issubclass that ignores unrelated warnings.
  • The shape of n_iter_ for a binary problem and the conversion to native Python int and bool .
  • What changes when the optimizer gets enough iterations to converge.

Follow-up Questions Guidance

  • Why does simplefilter('always') matter here?
  • How would you make lbfgs actually converge on this data?
  • In an automated training pipeline, why might you turn ConvergenceWarning into an error, and how would you do it?