【问题标题】:How to calculate the intercept using numpy.linalg.lstsq如何使用 numpy.linalg.lstsq 计算截距
【发布时间】:2015-11-13 20:35:04
【问题描述】:

使用numpy.linalg.lstsq 运行多元线性回归后,我得到了文档中描述的 4 个数组,但是我不清楚如何获得截距值。有人知道吗?我是统计分析的新手。

这是我的模型:

X1 = np.array(a)
X2 = np.array(b)
X3 = np.array(c)
X4 = np.array(d)
X5 = np.array(e)
X6 = np.array(f)
X1l = np.log(X1)
X2l = np.log(X2)
X3l = np.log(X3)
X6l = np.log(X6)
Y = np.array(g)

A = np.column_stack([X1l, X2l, X3l, X4, X5, X6l, np.ones(len(a), float)])
result = np.linalg.lstsq(A, Y)

这是我的模型生成的示例:

(array([  654.12744154,  -623.28893569,   276.50269246,    11.52493817,
  49.92528734,  -375.43282832,  3852.95023087]), array([  4.80339071e+11]),
  7, array([ 1060.38693842,   494.69470547,   243.14700033,   164.97697748,
  58.58072929,    19.30593045,    13.35948642]))

我相信拦截是第二个数组,但我仍然不确定,因为它的值太高了。

【问题讨论】:

    标签: python arrays numpy regression linear-regression


    【解决方案1】:

    intersect是ones的列对应的系数,在本例中为:

    result[0][6]
    

    为了更清楚地看到,考虑你的回归,它是这样的:

    y = c1*x1 + c2*x2 + c3*x3 + c4*x4 + m
    

    写成矩阵形式:

    [[y1],      [[x1_1,  x2_1,  x3_1, x4_1, 1],      [[c1],
     [y2],       [x1_2,  x2_2,  x3_2, x4_2, 1],       [c2],
     [y3],  =    [x1_3,  x2_3,  x3_3, x4_3, 1],  *    [c3],
     ...                      ...                     [c4],
     [yn]]       [x1_n,  x2_n,  x3_n, x4_n, 1]]       [m]]
    

    或:

     Y = A * C
    

    其中A 是所谓的“系数”矩阵,C 是包含回归解决方案的向量。注意m 对应于ones 的列。

    【讨论】:

      猜你喜欢
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2017-12-17
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多