Binary vs multiclass logistic regression in scikit-learn: coef_ and decision_function shapes
Quick Overview
Fit a binary and a three-class scikit-learn logistic regression on the same features and report the shapes of coef_, decision_function and classes_. Tests how scikit-learn lays out a single decision boundary compared with a softmax model over several classes, and how to return exact shape tuples for grading.
Binary vs multiclass logistic regression in scikit-learn: coef_ and decision_function shapes
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `coef_shapes(X, y_binary, y_multi) -> dict`. Fit two scikit-learn models on `X` (shape `(n, 2)`): one on `y_binary` (2 classes) and one on `y_multi` (3 classes). Construct each one as exactly `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)`, without passing `multi_class`. Return a dict that describes the shapes of the two fitted models.
The dict must have exactly five keys:
- `'binary_coef_shape'` and `'multi_coef_shape'`: each model's `coef_.shape`.
- `'binary_decision_shape'` and `'multi_decision_shape'`: each model's `decision_function(X).shape`.
- `'multi_classes'`: the `classes_` ndarray of the 3-class model.
Shapes must be tuples of Python ints. For reference, binary `coef_` is 2-D with shape `(1, n_features)` and 3-class `coef_` is `(n_classes, n_features)`, so here `(1, 2)` and `(3, 2)`. Binary `decision_function` is 1-D with shape `(n,)`, and the multiclass one is `(n, n_classes)`.
```hint Count the scores
Think about how many separate linear scores a two-class model needs to make a decision, and how many a three-class softmax model produces for each sample.
```
```hint Types matter for grading
The shapes are compared with exact `==` against tuples. Check what type `.shape` returns and what `classes_` holds before you return them.
```
### Constraints & Assumptions
- `X` has shape `(n, 2)`, so `n_features = 2`.
- `y_binary` has exactly 2 distinct labels and `y_multi` has exactly 3.
- `'multi_classes'` is graded with `assert_array_equal`; the four shape tuples are graded with exact `==`.
### What a Strong Answer Covers
- Fits both models with exactly the pinned hyperparameters and no `multi_class` argument.
- Returns exactly the five keys, with shapes as tuples of Python ints and `classes_` as an ndarray.
- Explains why binary `coef_` keeps a leading dimension of 1 while binary `decision_function` is 1-D.
- Identifies the default 3-class fit with `lbfgs` as a multinomial (softmax) model with one weight row per class.
### Follow-up Questions
- Why is binary `coef_` shaped `(1, n_features)` instead of a flat vector?
- How would the results change if you passed `multi_class='ovr'` for the 3-class model?
- What shape does `predict_proba` return in each case, and how does it relate to `decision_function`?
- A 3-class softmax only needs two free weight vectors. Why does the model store three, and what pins down the extra one?
Overview: Fit a binary and a three-class scikit-learn logistic regression on the same features and report the shapes of coef_, decision_function and classes_. Tests how scikit-learn lays out a single decision boundary compared with a softmax model over several classes, and how to return exact shape tuples for grading.
Binary vs multiclass logistic regression in scikit-learn: coef_ and decision_function shapes
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0
Implement coef_shapes(X, y_binary, y_multi) -> dict. Fit two scikit-learn models on X (shape (n, 2)): one on y_binary (2 classes) and one on y_multi (3 classes). Construct each one as exactly LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0), without passing multi_class. Return a dict that describes the shapes of the two fitted models.
The dict must have exactly five keys:
'binary_coef_shape'
and
'multi_coef_shape'
: each model's
coef_.shape
.
'binary_decision_shape'
and
'multi_decision_shape'
: each model's
decision_function(X).shape
.
'multi_classes'
: the
classes_
ndarray of the 3-class model.
Shapes must be tuples of Python ints. For reference, binary coef_ is 2-D with shape (1, n_features) and 3-class coef_ is (n_classes, n_features), so here (1, 2) and (3, 2). Binary decision_function is 1-D with shape (n,), and the multiclass one is (n, n_classes).
Constraints & Assumptions
X
has shape
(n, 2)
, so
n_features = 2
.
y_binary
has exactly 2 distinct labels and
y_multi
has exactly 3.
'multi_classes'
is graded with
assert_array_equal
; the four shape tuples are graded with exact
==
.
What a Strong Answer Covers Guidance
Fits both models with exactly the pinned hyperparameters and no
multi_class
argument.
Returns exactly the five keys, with shapes as tuples of Python ints and
classes_
as an ndarray.
Explains why binary
coef_
keeps a leading dimension of 1 while binary
decision_function
is 1-D.
Identifies the default 3-class fit with
lbfgs
as a multinomial (softmax) model with one weight row per class.
Follow-up Questions Guidance
Why is binary
coef_
shaped
(1, n_features)
instead of a flat vector?
How would the results change if you passed
multi_class='ovr'
for the 3-class model?
What shape does
predict_proba
return in each case, and how does it relate to
decision_function
?
A 3-class softmax only needs two free weight vectors. Why does the model store three, and what pins down the extra one?