You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何LogisticRegression的coef_仅存储对应classes_[1]的一组系数?

Why does model.coef_ only store coefficients for model.classes_[1] in scikit-learn's LogisticRegression?

Great question! Let’s break down exactly why you’re seeing this behavior when working with a binary classification task (like predicting passenger survival):

1. Binary Classification Uses a Single Coefficient Vector by Design

When you’re dealing with two classes (e.g., 'Not Survived' and 'Survived'), scikit-learn’s Logistic Regression models the log-odds of the positive class (which maps to model.classes_[1] in your case) relative to the negative class (model.classes_[0]).

The core formula driving this is:

log(P(y = positive_class) / P(y = negative_class)) = model.intercept_ + X @ model.coef_

Since the probability of the negative class is simply 1 - P(positive_class), we don’t need a separate set of coefficients for it. The log-odds for the negative class can be fully derived by negating the positive class’s log-odds. Storing only one coefficient vector saves memory and eliminates redundant information—there’s no need to store what we can easily compute.

That’s exactly why your model.coef_ has a shape of (1, 4): 1 vector dedicated to the positive class, with 4 coefficients matching your 4 input features.

2. Contrast with Multi-Class Scenarios

If you were working with a multi-class problem (3+ classes), model.coef_ would have a shape of (n_classes, n_features). This is because scikit-learn uses one of two strategies:

  • One-vs-Rest (OvR): Trains a separate binary classifier for each class vs. all others, resulting in one coefficient vector per class.
  • Multinomial: Models class probabilities directly using a softmax function, which also requires one coefficient vector per class to calculate relative odds.

But for binary classification, this redundancy isn’t necessary, so scikit-learn simplifies things by only storing coefficients for the positive class.

3. Verify the Behavior Yourself

You can confirm this logic by checking predicted probabilities. Run model.predict_proba(X) for any sample—you’ll get two values: the first is the probability of model.classes_[0], the second is the probability of model.classes_[1]. These two values always sum to 1, and both are computed using the single coefficient vector stored in model.coef_.


内容的提问来源于stack exchange,提问作者user1216456

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:17:07