Fit a Binary Logistic Regression and Return Test Accuracy

Quick Overview

Train a binary scikit-learn logistic regression with pinned hyperparameters and return its accuracy on a held-out test set as a plain Python float. It tests the fit and score workflow, what score measures for a classifier, and careful handling of the return type.

Fit a Binary Logistic Regression and Return Test Accuracy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_and_score(X_train, y_train, X_test, y_test)` that trains a binary `LogisticRegression` (from `sklearn.linear_model`) and returns `model.score(X_test, y_test)`: the fraction of test rows predicted correctly, in `[0.0, 1.0]`. Labels are integers 0 and 1 only. 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)` and evaluate on `(X_test, y_test)`. - Return a scalar Python `float` (wrap with `float(...)`). A numpy array, a percentage or the estimator does not meet the contract. Example: ```python acc = fit_and_score(X_train, y_train, X_test, y_test) # acc == 0.95 # 95% of test rows predicted correctly ``` ```hint Watch the return type Check what type the estimator's scoring method hands back before you return it. ``` ### What a Strong Answer Covers - Constructs the estimator with exactly the pinned hyperparameters, fits it on the training split only, and evaluates it on the test split. - Explains what `score` computes for a classifier and how it relates to `predict` and the true labels. - Returns a plain Python `float` between 0.0 and 1.0, with no numpy wrapper, percentage scaling or estimator. ### Follow-up Questions - Why can accuracy mislead on an imbalanced test set, and what would you report instead? - How would you compute the same number without calling `score`? - How would you get a more reliable accuracy estimate than one train/test split gives?

Overview: Train a binary scikit-learn logistic regression with pinned hyperparameters and return its accuracy on a held-out test set as a plain Python float. It tests the fit and score workflow, what score measures for a classifier, and careful handling of the return type.

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

Implement fit_and_score(X_train, y_train, X_test, y_test) that trains a binary LogisticRegression (from sklearn.linear_model) and returns model.score(X_test, y_test): the fraction of test rows predicted correctly, in [0.0, 1.0]. Labels are integers 0 and 1 only.

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) and evaluate on (X_test, y_test) .
  • Return a scalar Python float (wrap with float(...) ). A numpy array, a percentage or the estimator does not meet the contract.

Example:

acc = fit_and_score(X_train, y_train, X_test, y_test)
# acc == 0.95   # 95% of test rows predicted correctly

What a Strong Answer Covers Guidance

  • Constructs the estimator with exactly the pinned hyperparameters, fits it on the training split only, and evaluates it on the test split.
  • Explains what score computes for a classifier and how it relates to predict and the true labels.
  • Returns a plain Python float between 0.0 and 1.0, with no numpy wrapper, percentage scaling or estimator.

Follow-up Questions Guidance

  • Why can accuracy mislead on an imbalanced test set, and what would you report instead?
  • How would you compute the same number without calling score ?
  • How would you get a more reliable accuracy estimate than one train/test split gives?