【问题标题】:Writing SKLearn Regresion Coefficients To Pandas Series将 SKLearn 回归系数写入 Pandas 系列
【发布时间】:2020-07-28 08:08:32
【问题描述】:

我有一个适合 SKlearn 的 LinearRegression 模块的回归模型:

为了提取系数,我使用了代码;

coefficients = model.coef_

它产生了以下形状为 (1, 10) 的数组:

[-4.72307152e-05  1.29731143e-04  8.75483702e-05 -6.28749019e-04
   1.75096740e-04 -3.30209379e-06  1.35937650e-03  3.89048429e-11
   8.48406857e-03 -1.36499030e-05]

现在,我想将数组保存到 pd.Series。我正在采取以下方法:

features = ["f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10"]
model_coefs = pd.Series(coefficients, index=features)

而且,系统给了我以下错误:

ValueError: Length of passed values is 1, index implies 10.

我尝试过的:

  • 转置基础数组系数,使其长度为 10。
  • 重新调整数组的形状,使其形状为 (10,1)。

但似乎没有任何效果。我不确定我哪里出错了。

【问题讨论】:

  • 数组中的值之间没有逗号,因此它的形状为 1

标签: pandas numpy series


【解决方案1】:

对于您的情况,您希望展平数组,因此 .ravel 应该可以解决问题,例如:

pd.Series(np.zeros((1, 10)).ravel(), index=features)

奇怪的是,coeffs 输出的形状为 (1, 10),当我运行基本 sklearn 示例 here(具有多个功能)时,我的 coeffs 为 1-d

In [27]: regr.coef_
Out[27]:
array([ 3.03499549e-01, -2.37639315e+02,  5.10530605e+02,  3.27736980e+02,
       -8.14131709e+02,  4.92814588e+02,  1.02848452e+02,  1.84606489e+02,
        7.43519617e+02,  7.60951722e+01])

In [28]: regr.coef_.shape
Out[28]: (10,)

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 2021-02-18
    • 2016-10-25
    • 2021-09-20
    • 2016-12-14
    • 2016-10-19
    • 2018-12-03
    • 2016-10-05
    • 2016-11-21
    相关资源
    最近更新 更多