4
votes

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)
1
You're missing p_value in your return tuple: docs.scipy.org/doc/scipy-0.16.0/reference/generated/… - tzaman
@tzaman nailed it. There are five values on the right side, but only four on the left. The error message informs you of this discrepancy -- but it is, indeed, confusing the first time you encounter it. - Prune

1 Answers

4
votes

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