问题描述
我知道,当将更多变量设置为值而不是函数返回时,会出现此错误消息( ValueError:太多的值无法解包(预期为4)
).
I know that this error message (ValueError: too many values to unpack (expected 4)
) appears when more variables are set to values than a function returns.
scipy.stats.linregress
根据scipy文档返回5个值( http://docs.scipy.org/doc/scipy/reference/generation/scipy.stats.linregress.html ).
scipy.stats.linregress
returns 5 values according to the scipy documentation (http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html).
以下是一个简短的、可重现的示例,显示了对 linregress
的正常调用和失败调用:
Here is a short, reproducible example of a working call, and then a failed call, to linregress
:
什么可以解释差异,为什么第二个却叫不好?
What could account for difference and why is the second one poorly called?
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,:]))
推荐答案
您的问题源自切片 X
和 Y
数组.另外,您不需要 for
循环.请改用以下内容,它应该可以正常工作.
Your problem originates from slicing the X
and Y
arrays. Also you do not need the for
loop. Use the following instead and it should work.
slope, intercept, r_value, p_value, std_err = stats.linregress(X[:,0], Y[:,0])
这篇关于`ValueError:太多值无法通过`scipy.stats.linregress`解包(预期为4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!