简体   繁体   中英

ValueError: too many values to unpack Scipy

I'm a beginner and I'm trying to use regression from scipy. I'm getting the error:

ValueError: too many values to unpack

when I run the following code:

testArray1 = [1,2,3] 
testArray2 = [2,3,4] 
slope, intercept, r_value, std_err = scipy.stats.linregress(testArray1,testArray2)

Looking for an answer to the same error got me here, just that in my case I was using a bit more complex input (masked arrays, detailed ahead). I'm posting my solution here in case someone might need it.

In case of Masked Arrays ( SciPy: Masked arrays ), SciPy have a set of statistical function designated for them: scipy.stats.mstats .

For example, a couple of lists of arrays would yield the same error when called by scipy.stats.linregress:

from scipy import stats    

x = [array([4.04]), array([4.38])]
y = [array([3.60]), array([4.03])]

slope, intercept, r_value, std_err = scipy.stats.linregress(x,y)

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    slope, intercept, r, prob, sterrest = stats.linregress(x,y)
  File ".../anaconda/lib/python3.4/site-packages/scipy/stats/_stats_mstats_common.py", line 79, in linregress
    ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat
ValueError: too many values to unpack (expected 4)

But using stats.mstats.linregress() would fix it:

from scipy import stats    

x = [array([4.04]), array([4.38])]
y = [array([3.60]), array([4.03])]

slope, intercept, r_value, std_err = scipy.stats.mstats.linregress(x,y)        
print(r**2)

>>> 1.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM