Select Best C With LogisticRegressionCV

Quick Overview

Implement a function that uses scikit-learn's LogisticRegressionCV to pick the inverse regularization strength C from a fixed grid with unshuffled stratified 3-fold cross-validation. It tests knowledge of the estimator's arguments, the refit step, and how to read the selected value from the fitted model.

Select Best C With LogisticRegressionCV

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def logregcv_best_c(X: np.ndarray, y: np.ndarray) -> float`. `X` has shape `(n_samples, n_features)` (float64, already standardized) and `y` has shape `(n_samples,)` with values in `{0, 1}`. Use `sklearn.linear_model.LogisticRegressionCV` to select `C` and return it. The estimator must use exactly these settings: - `Cs=[0.001, 0.01, 0.1, 1.0, 10.0, 100.0]`, passed as an explicit list - `cv=StratifiedKFold(n_splits=3, shuffle=False)`, with `StratifiedKFold` imported from `sklearn.model_selection` - `scoring='accuracy'`, `penalty='l2'`, `solver='lbfgs'` - `refit=True`, `fit_intercept=True` - `max_iter=20000`, `tol=1e-10` Return `float(model.C_[0])`. `C_` has shape `(1,)`, and the function must return a plain Python `float`. ```hint How the grid is read sklearn treats an integer `Cs` and a list `Cs` differently. Make sure the six required values are the only candidates it tries. ``` ### Constraints & Assumptions - `X` is float64 with shape `(n_samples, n_features)` and is already standardized, so the function does no scaling of its own. - `y` holds only the labels 0 and 1, and both classes appear often enough to fill three stratified folds. ### What a Strong Answer Covers - Builds `LogisticRegressionCV` with every required argument, including the explicit `Cs` list and the `StratifiedKFold` object. - Explains how the cross-validation sweep scores each candidate C and what `refit=True` does once a winner is chosen. - Reads the selected value from `C_` and returns it as a plain Python float, with the reason `C_` is an array. - Says what makes the selection reproducible and which C wins when two candidates tie on mean accuracy. ### Follow-up Questions - Why does `C_` have shape `(1,)` here instead of one value per class? - Why set `shuffle=False` and such a strict `tol`? - If two values of C reach the same mean accuracy, which one is returned? - What would `C_` contain with `refit=False`?

Overview: Implement a function that uses scikit-learn's LogisticRegressionCV to pick the inverse regularization strength C from a fixed grid with unshuffled stratified 3-fold cross-validation. It tests knowledge of the estimator's arguments, the refit step, and how to read the selected value from the fitted model.

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

Implement def logregcv_best_c(X: np.ndarray, y: np.ndarray) -> float. X has shape (n_samples, n_features) (float64, already standardized) and y has shape (n_samples,) with values in {0, 1}. Use sklearn.linear_model.LogisticRegressionCV to select C and return it.

The estimator must use exactly these settings:

  • Cs=[0.001, 0.01, 0.1, 1.0, 10.0, 100.0] , passed as an explicit list
  • cv=StratifiedKFold(n_splits=3, shuffle=False) , with StratifiedKFold imported from sklearn.model_selection
  • scoring='accuracy' , penalty='l2' , solver='lbfgs'
  • refit=True , fit_intercept=True
  • max_iter=20000 , tol=1e-10

Return float(model.C_[0]). C_ has shape (1,), and the function must return a plain Python float.

Constraints & Assumptions

  • X is float64 with shape (n_samples, n_features) and is already standardized, so the function does no scaling of its own.
  • y holds only the labels 0 and 1, and both classes appear often enough to fill three stratified folds.

What a Strong Answer Covers Guidance

  • Builds LogisticRegressionCV with every required argument, including the explicit Cs list and the StratifiedKFold object.
  • Explains how the cross-validation sweep scores each candidate C and what refit=True does once a winner is chosen.
  • Reads the selected value from C_ and returns it as a plain Python float, with the reason C_ is an array.
  • Says what makes the selection reproducible and which C wins when two candidates tie on mean accuracy.

Follow-up Questions Guidance

  • Why does C_ have shape (1,) here instead of one value per class?
  • Why set shuffle=False and such a strict tol ?
  • If two values of C reach the same mean accuracy, which one is returned?
  • What would C_ contain with refit=False ?