Return Fitted Logistic Regression Coefficients and Intercept

Quick Overview

Fit a binary scikit-learn logistic regression with pinned hyperparameters and return its coefficients as a flat float64 array and its intercept as a plain Python float. It tests how scikit-learn stores fitted parameters for binary models and how the C setting shapes the learned weights.

Return Fitted Logistic Regression Coefficients and Intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fitted_weights(X, y)` that fits a binary scikit-learn `LogisticRegression` on `(X, y)` and returns `(coefficients, intercept)`, not the estimator. ```python def fitted_weights(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, float]: ... ``` Requirements: - Pin the hyperparameters: `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`. - `coefficients` is a 1-D `np.ndarray` of shape `(n_features,)` with dtype `float64` (`model.coef_` is `(1, n_features)`). - `intercept` is a plain Python `float` (`model.intercept_` has shape `(1,)`). ```hint Fitted attribute layout scikit-learn stores fitted parameters in a layout that also works for many classes. Check the shapes of both attributes on a binary model before you return them. ``` ### Constraints & Assumptions - `X` is a 2-D numeric array of shape `(n_samples, n_features)`. - `y` contains only the integer labels 0 and 1. - `numpy` and `scikit-learn` are available. No input validation is required. ### What a Strong Answer Covers - Fits the estimator with all six pinned hyperparameters. - Returns the learned parameters, not the fitted estimator. - Flattens `coef_` to a `float64` array of shape `(n_features,)`. - Converts `intercept_[0]` to a plain Python `float`. ### Follow-up Questions - Why is `coef_` shaped `(1, n_features)` instead of `(n_features,)`? - How does the `C` hyperparameter affect the learned weights? - How would you compute the predicted probability of class 1 from the two returned values alone?

Overview: Fit a binary scikit-learn logistic regression with pinned hyperparameters and return its coefficients as a flat float64 array and its intercept as a plain Python float. It tests how scikit-learn stores fitted parameters for binary models and how the C setting shapes the learned weights.

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

Implement fitted_weights(X, y) that fits a binary scikit-learn LogisticRegression on (X, y) and returns (coefficients, intercept), not the estimator.

def fitted_weights(X: np.ndarray, y: np.ndarray) -> tuple[np.ndarray, float]:
    ...

Requirements:

  • Pin the hyperparameters: penalty='l2' , C=1.0 , solver='lbfgs' , max_iter=1000 , tol=1e-6 , fit_intercept=True .
  • coefficients is a 1-D np.ndarray of shape (n_features,) with dtype float64 ( model.coef_ is (1, n_features) ).
  • intercept is a plain Python float ( model.intercept_ has shape (1,) ).

Constraints & Assumptions

  • X is a 2-D numeric array of shape (n_samples, n_features) .
  • y contains only the integer labels 0 and 1.
  • numpy and scikit-learn are available. No input validation is required.

What a Strong Answer Covers Guidance

  • Fits the estimator with all six pinned hyperparameters.
  • Returns the learned parameters, not the fitted estimator.
  • Flattens coef_ to a float64 array of shape (n_features,) .
  • Converts intercept_[0] to a plain Python float .

Follow-up Questions Guidance

  • Why is coef_ shaped (1, n_features) instead of (n_features,) ?
  • How does the C hyperparameter affect the learned weights?
  • How would you compute the predicted probability of class 1 from the two returned values alone?