`ValueError:太多值无法通过`scipy.stats.linregress`解包(预期为4)

Mun*_*uno 4 python scipy linear-regression

我知道ValueError: too many values to unpack (expected 4)当将更多变量设置为值而不是函数返回时,会出现此错误消息()。

scipy.stats.linregress根据scipy文档(http://docs.scipy.org/doc/scipy/reference/generation/scipy.stats.linregress.html)返回5个值。

这是一个简短的,可复制的示例,它是对的工作呼叫,然后是失败的呼叫linregress

有什么可以解释差异的原因,为什么第二个没有得到很好的称呼呢?

from scipy import stats
import numpy as np

if __name__ == '__main__':
    x = np.random.random(10)
    y = np.random.random(10)
    print(x,y)
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)


'''
Code above works
Code below fails
'''

    X = np.asarray([[-15.93675813],
 [-29.15297922],
 [ 36.18954863],
 [ 37.49218733],
 [-48.05882945],
 [ -8.94145794],
 [ 15.30779289],
 [-34.70626581],
 [  1.38915437],
 [-44.38375985],
 [  7.01350208],
 [ 22.76274892]])

    Y = np.asarray( [[  2.13431051],
 [  1.17325668],
 [ 34.35910918],
 [ 36.83795516],
 [  2.80896507],
 [  2.12107248],
 [ 14.71026831],
 [  2.61418439],
 [  3.74017167],
 [  3.73169131],
 [  7.62765885],
 [ 22.7524283 ]])

    print(X,Y) # The array initialization succeeds, if both arrays are print out


    for i in range(1,len(X)):
        slope, intercept, r_value, p_value, std_err = (stats.linregress(X[0:i,:], y = Y[0:i,:]))
Run Code Online (Sandbox Code Playgroud)

Dat*_*man 6

您的问题源自切片Xand Y数组。另外,您不需要for循环。请改用以下内容,它应该可以工作。

slope, intercept, r_value, p_value, std_err = stats.linregress(X[:,0], Y[:,0])
Run Code Online (Sandbox Code Playgroud)