scipy linregress的多变量线性回归

jbr*_*own 6 python scipy linear-regression

我正在尝试训练一个非常简单的线性回归模型。

我的代码是:

from scipy import stats

xs = [[   0,    1,  153]
 [   1,    2,    0]
 [   2,    3,  125]
 [   3,    1,   93]
 [   2,   24, 5851]
 [   3,    1,  524]
 [   4,    1,    0]
 [   2,    3,    0]
 [   2,    1,    0]
 [   5,    1,    0]]

ys = [1, 1, 1, 1, 1, 0, 1, 1, 0, 1]

slope, intercept, r_value, p_value, std_err = stats.linregress(xs, ys)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/stats/stats.py", line 3100, in linregress
ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 1747, in cov
X = concatenate((X, y), axis)
ValueError: all the input array dimensions except for the concatenation 
axis must match exactly
Run Code Online (Sandbox Code Playgroud)

我的输入有什么问题?我试图ys以几种方式更改结构,但没有任何效果。

Abd*_*tir 5

您正在寻找多变量回归。AFAIK stats.linregress没有该功能。

您可能想尝试一下sklearn.linear_model.LinearRegression。检查答案。

  • 或者 statsmodels 的 OLS (2认同)