2016-08-02 122 views
0

我知道這個錯誤信息(ValueError: too many values to unpack (expected 4))出現多個變量設置爲值比函數返回時。`ValueError:太多的值來解壓縮(預期4)`與`scipy.stats.linregress``

scipy.stats.linregress根據scipy文檔返回5個值(http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.linregress.html)。

這裏是一個工作電話的短,重複的例子,然後呼叫失敗,到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,:])) 
+0

你能發佈完整的錯誤信息和堆棧跟蹤嗎? – njzk2

+2

你的X和Y的形狀是:(12,1),但你需要的是(12,)。 – Dataman

+0

也是''i'的值導致這個問題? – njzk2

回答

2

您的問題,從切片XY陣列起源。您也不需要for循環。使用下面的代替,它應該工作。

slope, intercept, r_value, p_value, std_err = stats.linregress(X[:,0], Y[:,0]) 
+1

他們可能*想* for'循環仍然(很難說)。但解決方案*是*將'[...,:]'更改爲'[...,0]'。 – jedwards

+0

我假設他/他已經使用'for'循環來處理數組的形狀。如果使用正確的切片,則不需要「for」循環。 – Dataman

+0

我以爲他們試圖得到* n *不同迴歸的結果,每個迴歸都考慮X/Y中的一個額外元素。 – jedwards

0

問題的事實,您輸入np.asarray是單元素列表的列表莖。

因此,XY兩者都具有(12,1)的形狀:

print(X.shape) # (12, 1) [or (12L, 1L), depending on version] 
print(Y.shape) # (12, 1) 

注意,這些均爲二維陣列。儘管其中一個維度是1,但它們仍然被認爲是二維的。

現在考慮創建一個數組的是這樣的:

x = np.asarray([1,2,3,4,5]) 
print(x.shape) # (5,) 

注意,在這種情況下,我們通過一個整數列表,以asarray,我們得到了一個一維數組。

當您的函數被兩個變量調用時,您的函數都需要是一維數組。所以,你可以開始創建數組作爲一維:

例如,通過手:

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_data = [[ 2.13431051], 
      [ 1.17325668], 
      [ 34.35910918], 
      [ 36.83795516], 
      [ 2.80896507], 
      [ 2.12107248], 
      [ 14.71026831], 
      [ 2.61418439], 
      [ 3.74017167], 
      [ 3.73169131], 
      [ 7.62765885], 
      [ 22.7524283 ]] 
Y = np.asarray([e[0] for e in y_data]) 

或者通過切片:

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 ]]) 
Y = Y[:,0] 

這三種方法都會導致你有X和形狀(12,) 210(一維):

print(X.shape) # (12,) 
print(Y.shape) # (12,) 

然後,你可以使用你的循環爲:

for i in range(3,len(X)): 
    slope, intercept, r_value, p_value, std_err = stats.linregress(X[0:i], y = Y[0:i]) 
    print(slope) 

注意,我開始循環3,它的第一個值是「有道理」。

或者,你可以讓你的陣列未修改的二維,只是解決您的循環中的切片語法:

for i in range(3,len(X)): 
    slope, intercept, r_value, p_value, std_err = stats.linregress(X[0:i,0], y = Y[0:i,0]) 
    print(slope) 

這是在我評論的回答提出的方法。

相關問題