Fit a Logistic Regression and Predict Binary Labels

Quick Overview

Train a binary scikit-learn logistic regression with fixed hyperparameters and return hard 0 or 1 labels for the test rows. It tests the basic fit and predict workflow, the decision rule behind predict, and the shape and dtype of the returned labels.

Fit a Logistic Regression and Predict Binary Labels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fit_and_predict(X_train, y_train, X_test)` that trains a binary scikit-learn `LogisticRegression` on `(X_train, y_train)` and returns the predicted class labels for `X_test`. `y_train` contains only the integer labels 0 and 1. Requirements: - Construct the estimator with exactly `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`. - Fit on `(X_train, y_train)`, then call `model.predict(X_test)`. - Return a 1-D `np.ndarray` of shape `(n_test,)` with an integer dtype, one label (0 or 1) per row. - Do not return probabilities or the fitted estimator. ```hint Where the labels come from Look at which fitted attribute `predict` takes its labels from, and what that implies for the dtype of the result. ``` ### What a Strong Answer Covers - Constructs the estimator with every pinned hyperparameter passed explicitly and fits it on the training data. - Returns hard labels from `predict`, with no probabilities and no estimator in the return value. - Explains the decision rule `predict` applies and how it relates to a 0.5 probability threshold. - Explains why the result is 1-D with an integer dtype. ### Follow-up Questions - How would you predict labels at a threshold other than 0.5? - What would `predict` return if `y_train` held string labels such as `"no"` and `"yes"`? - Why pass every hyperparameter explicitly when some of them match the library defaults?

Overview: Train a binary scikit-learn logistic regression with fixed hyperparameters and return hard 0 or 1 labels for the test rows. It tests the basic fit and predict workflow, the decision rule behind predict, and the shape and dtype of the returned labels.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fit_and_predict(X_train, y_train, X_test) that trains a binary scikit-learn LogisticRegression on (X_train, y_train) and returns the predicted class labels for X_test. y_train contains only the integer labels 0 and 1.

Requirements:

  • Construct the estimator with exactly penalty='l2' , C=1.0 , solver='lbfgs' , max_iter=1000 , tol=1e-6 , fit_intercept=True .
  • Fit on (X_train, y_train) , then call model.predict(X_test) .
  • Return a 1-D np.ndarray of shape (n_test,) with an integer dtype, one label (0 or 1) per row.
  • Do not return probabilities or the fitted estimator.

What a Strong Answer Covers Guidance

  • Constructs the estimator with every pinned hyperparameter passed explicitly and fits it on the training data.
  • Returns hard labels from predict , with no probabilities and no estimator in the return value.
  • Explains the decision rule predict applies and how it relates to a 0.5 probability threshold.
  • Explains why the result is 1-D with an integer dtype.

Follow-up Questions Guidance

  • How would you predict labels at a threshold other than 0.5?
  • What would predict return if y_train held string labels such as "no" and "yes" ?
  • Why pass every hyperparameter explicitly when some of them match the library defaults?