Class Ordering of LogisticRegression classes_ with String Labels

Quick Overview

Fit a scikit-learn logistic regression on string class labels and return the fitted classes_ array. It tests how the estimator orders string labels, which predict_proba column and decision_function sign belong to which class, and how label spelling can silently flip the positive class.

Class Ordering of LogisticRegression classes_ with String Labels

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fitted_classes(X: np.ndarray, y: np.ndarray) -> np.ndarray` that fits a binary `LogisticRegression` on string labels (for example `np.array(['no', 'yes', 'no', ...])`) and returns the estimator's `classes_`. scikit-learn uses the sorted unique values, so `'no'`/`'yes'` gives `array(['no', 'yes'])` with `'yes'` as `classes_[1]` (the column `predict_proba(...)[:, 1]` and the positive `decision_function` direction). Requirements: - Pin `LogisticRegression` to `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`. - Return the `np.ndarray` of shape `(2,)` holding the class labels. - Return `classes_` itself: do not re-sort or re-encode by hand. Example: ```python X = np.array([[0.0], [1.0], [2.0], [3.0]]) y = np.array(['yes', 'no', 'yes', 'no']) fitted_classes(X, y) # -> array(['no', 'yes'], dtype='<U3') ``` `'yes'` appears first in `y`, but `classes_` is sorted, so `'no'` is `classes_[0]` and `predict_proba(X)[:, 1]` is the probability of `'yes'`. ```hint Sort order Think about how strings compare character by character, and what that does to a label pair such as `'Yes'` and `'no'`. ``` ### Constraints & Assumptions - `X` is a 2-D numeric array of shape `(n_samples, n_features)`. - `y` is a 1-D array of strings with exactly two distinct values, and both appear. ### What a Strong Answer Covers - Fitting the pinned estimator directly on the string labels, with no manual encoding. - Returning `classes_` unchanged as a shape `(2,)` array. - How `classes_` is built from `y`, and why the order in which labels appear in `y` does not matter. - Which label `predict_proba(...)[:, 1]` and a positive `decision_function` value refer to. ### Follow-up Questions - With labels `'Yes'` and `'no'`, which class does `predict_proba(X)[:, 1]` describe, and how would you make downstream code safe for any pair of labels? - How do you pass string labels to metrics such as `f1_score` or `precision_recall_curve`? - What do `classes_`, `predict_proba` and `coef_` look like when `y` has three string classes?

Overview: Fit a scikit-learn logistic regression on string class labels and return the fitted classes_ array. It tests how the estimator orders string labels, which predict_proba column and decision_function sign belong to which class, and how label spelling can silently flip the positive class.

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

Implement def fitted_classes(X: np.ndarray, y: np.ndarray) -> np.ndarray that fits a binary LogisticRegression on string labels (for example np.array(['no', 'yes', 'no', ...])) and returns the estimator's classes_. scikit-learn uses the sorted unique values, so 'no'/'yes' gives array(['no', 'yes']) with 'yes' as classes_[1] (the column predict_proba(...)[:, 1] and the positive decision_function direction).

Requirements:

  • Pin LogisticRegression to penalty='l2' , C=1.0 , solver='lbfgs' , max_iter=1000 , tol=1e-6 .
  • Return the np.ndarray of shape (2,) holding the class labels.
  • Return classes_ itself: do not re-sort or re-encode by hand.

Example:

X = np.array([[0.0], [1.0], [2.0], [3.0]])
y = np.array(['yes', 'no', 'yes', 'no'])

fitted_classes(X, y)  # -> array(['no', 'yes'], dtype='<U3')

'yes' appears first in y, but classes_ is sorted, so 'no' is classes_[0] and predict_proba(X)[:, 1] is the probability of 'yes'.

Constraints & Assumptions

  • X is a 2-D numeric array of shape (n_samples, n_features) .
  • y is a 1-D array of strings with exactly two distinct values, and both appear.

What a Strong Answer Covers Guidance

  • Fitting the pinned estimator directly on the string labels, with no manual encoding.
  • Returning classes_ unchanged as a shape (2,) array.
  • How classes_ is built from y , and why the order in which labels appear in y does not matter.
  • Which label predict_proba(...)[:, 1] and a positive decision_function value refer to.

Follow-up Questions Guidance

  • With labels 'Yes' and 'no' , which class does predict_proba(X)[:, 1] describe, and how would you make downstream code safe for any pair of labels?
  • How do you pass string labels to metrics such as f1_score or precision_recall_curve ?
  • What do classes_ , predict_proba and coef_ look like when y has three string classes?