Fit a logistic regression with an explicit class_weight dict and return positive-class probabilities

Quick Overview

Fit a scikit-learn logistic regression with an explicit class_weight dictionary and return positive-class probabilities for new rows. It tests how class weights enter the loss, why the weights must be passed unchanged, and what weighting does to probability calibration.

Fit a logistic regression with an explicit class_weight dict and return positive-class probabilities

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_with_class_weight_dict(X, y, weights, X_new)`: fit a binary logistic regression with an explicit `class_weight` dict `weights` (label to float, keys are class labels, e.g. `{0: 1.0, 1: 5.0}`) and return positive-class probabilities for `X_new`. The function must follow these rules: - Fit exactly `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, penalty='l2', random_state=0, class_weight=weights)`. - Pass `weights` straight through: do not rebuild or normalize it. - Return `predict_proba(X_new)[:, 1]` (class 1 probability) as a 1-D `float64` ndarray of shape `(X_new.shape[0],)`. ```hint Where the weights enter Write out the objective scikit-learn minimizes for an L2 logistic regression and see where each sample's class weight appears relative to the penalty term. Then ask what rescaling the whole dict would do. ``` ### Constraints & Assumptions - Labels in `y` are binary, and the keys of `weights` are those labels. - No preprocessing or scaling is required. - The result is graded with `assert_allclose(rtol=1e-5)` against an identically pinned reference fit. ### What a Strong Answer Covers - The pinned estimator with the dict forwarded unchanged, and why rescaling the dict would change the fitted model. - Picking the probability column that belongs to the positive class and returning the required dtype and shape. - How class weights enter the loss and its gradient. - What weighting does to the decision boundary and to the calibration of the returned probabilities. ### Follow-up Questions - How does an explicit `class_weight` dict differ from `'balanced'`? - Mechanically, how do these weights change the fit? - How would you get calibrated probabilities back from a class-weighted model?

Overview: Fit a scikit-learn logistic regression with an explicit class_weight dictionary and return positive-class probabilities for new rows. It tests how class weights enter the loss, why the weights must be passed unchanged, and what weighting does to probability calibration.

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

Implement fit_with_class_weight_dict(X, y, weights, X_new): fit a binary logistic regression with an explicit class_weight dict weights (label to float, keys are class labels, e.g. {0: 1.0, 1: 5.0}) and return positive-class probabilities for X_new.

The function must follow these rules:

  • Fit exactly LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, penalty='l2', random_state=0, class_weight=weights) .
  • Pass weights straight through: do not rebuild or normalize it.
  • Return predict_proba(X_new)[:, 1] (class 1 probability) as a 1-D float64 ndarray of shape (X_new.shape[0],) .

Constraints & Assumptions

  • Labels in y are binary, and the keys of weights are those labels.
  • No preprocessing or scaling is required.
  • The result is graded with assert_allclose(rtol=1e-5) against an identically pinned reference fit.

What a Strong Answer Covers Guidance

  • The pinned estimator with the dict forwarded unchanged, and why rescaling the dict would change the fitted model.
  • Picking the probability column that belongs to the positive class and returning the required dtype and shape.
  • How class weights enter the loss and its gradient.
  • What weighting does to the decision boundary and to the calibration of the returned probabilities.

Follow-up Questions Guidance

  • How does an explicit class_weight dict differ from 'balanced' ?
  • Mechanically, how do these weights change the fit?
  • How would you get calibrated probabilities back from a class-weighted model?