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

如何从经StandardScaler标准化后的逻辑回归系数还原原始系数

还原逻辑回归原始系数的正确方法

首先,你用inverse_transform的思路错了——这个方法是用来还原标准化后的特征数据的,不是处理模型系数的,直接套在系数上肯定得不到正确结果。

要把标准化后的逻辑回归系数转换回原始特征空间的系数,得手动根据StandardScaler的缩放规则计算:

假设原始特征为$X$,标准化后为$X_{scaled} = \frac{X - \text{mean}}{\text{std}}$,逻辑回归在$X_{scaled}$上的系数为$\beta_{scaled}$,截距为$\beta_{0,scaled}$,那么原始特征空间的系数$\beta_{raw}$和截距$\beta_{0,raw}$的计算公式是:

  • $\beta_{raw} = \frac{\beta_{scaled}}{\text{std}}$
  • $\beta_{0,raw} = \beta_{0,scaled} - \sum\left(\beta_{scaled} \times \frac{\text{mean}}{\text{std}}\right)$

具体实现代码

基于你的现有代码,修改成如下:

import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

# 你的原有训练代码
log_model = make_pipeline(StandardScaler(), LogisticRegression())
log_model.fit(X, Y)

# 从Pipeline中取出标准化器和逻辑回归模型实例
scaler = log_model.steps[0][1]
log_reg = log_model.steps[1][1]

# 获取标准化时用到的特征均值和标准差
feature_means = scaler.mean_
feature_stds = scaler.scale_

# 计算原始特征对应的系数
raw_coefficients = log_reg.coef_[0] / feature_stds

# 计算原始特征空间下的截距项
raw_intercept = log_reg.intercept_[0] - np.sum(log_reg.coef_[0] * feature_means / feature_stds)

# 整理成DataFrame方便查看
coeff_df = pd.DataFrame({
    '特征名称': X.columns,
    '原始系数': raw_coefficients
})
# 手动添加截距项
coeff_df.loc[len(coeff_df)] = ['截距', raw_intercept]

print(coeff_df)

验证方式

你可以直接用原始特征训练一个未做标准化的逻辑回归模型,把得到的系数和上面代码的输出对比,二者在浮点误差范围内应该完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 18:57:17