Fit Logistic Regression Without Intercept

Quick Overview

Fit a binary scikit-learn logistic regression with the intercept turned off and return the coefficient vector and intercept with exact shapes and types. It tests LogisticRegression settings, the shapes of coef_ and intercept_, and when dropping the bias term is a sound modeling choice.

Fit Logistic Regression Without Intercept

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `no_intercept_fit(X, y)` that fits a binary scikit-learn `LogisticRegression` with the bias term disabled, so the model scores are `z = Xw`, and returns `(coefficients, intercept)`. `y` contains only the integer labels `0` and `1`. Configure the estimator with exactly these settings: `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=False`. The return values must have these shapes and types: - `coefficients` is a 1-D `np.ndarray` of shape `(n_features,)`. The fitted `coef_` has shape `(1, n_features)`, so flatten it. - `intercept` is a plain Python `float` read from `model.intercept_[0]`. Its value will be `0.0`, but it must come from the fitted model, not a hard-coded literal. ```python w, b = no_intercept_fit(X, y) # w.shape == (n_features,) # b == 0.0 (a Python float) ``` ```hint Shapes and types Look up the shapes scikit-learn gives `coef_` and `intercept_` for a binary problem, and check what type you get when you index into a NumPy array. ``` ### Constraints & Assumptions - `X` is a 2-D numeric array of shape `(n_samples, n_features)`. - `y` is a 1-D array of length `n_samples` that holds only `0` and `1`. ### What a Strong Answer Covers - Builds the estimator with all six required settings, including `fit_intercept=False`. - Returns `coef_` flattened to shape `(n_features,)`. - Reads `intercept_[0]` from the fitted model and converts it to a Python `float`. - Explains what turning off the intercept does to the decision boundary and the predicted probabilities. ### Follow-up Questions - Why might you want `fit_intercept=False` in practice? - How does regularization interact with the intercept term? - If you append a column of ones to `X` and keep `fit_intercept=False`, do you get the same model as fitting with an intercept?

Overview: Fit a binary scikit-learn logistic regression with the intercept turned off and return the coefficient vector and intercept with exact shapes and types. It tests LogisticRegression settings, the shapes of coef_ and intercept_, and when dropping the bias term is a sound modeling choice.

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

Implement no_intercept_fit(X, y) that fits a binary scikit-learn LogisticRegression with the bias term disabled, so the model scores are z = Xw, and returns (coefficients, intercept). y contains only the integer labels 0 and 1.

Configure the estimator with exactly these settings: penalty='l2', C=1.0, solver='lbfgs', max_iter=1000, tol=1e-6, fit_intercept=False.

The return values must have these shapes and types:

  • coefficients is a 1-D np.ndarray of shape (n_features,) . The fitted coef_ has shape (1, n_features) , so flatten it.
  • intercept is a plain Python float read from model.intercept_[0] . Its value will be 0.0 , but it must come from the fitted model, not a hard-coded literal.
w, b = no_intercept_fit(X, y)
# w.shape == (n_features,)
# b == 0.0  (a Python float)

Constraints & Assumptions

  • X is a 2-D numeric array of shape (n_samples, n_features) .
  • y is a 1-D array of length n_samples that holds only 0 and 1 .

What a Strong Answer Covers Guidance

  • Builds the estimator with all six required settings, including fit_intercept=False .
  • Returns coef_ flattened to shape (n_features,) .
  • Reads intercept_[0] from the fitted model and converts it to a Python float .
  • Explains what turning off the intercept does to the decision boundary and the predicted probabilities.

Follow-up Questions Guidance

  • Why might you want fit_intercept=False in practice?
  • How does regularization interact with the intercept term?
  • If you append a column of ones to X and keep fit_intercept=False , do you get the same model as fitting with an intercept?