簡體   English   中英

ValueError:無法將輸入數組從形狀(1,3)廣播到形狀(3,1)

[英]ValueError: could not broadcast input array from shape (1,3) into shape (3,1)

我正在研究線性回歸編碼問題,但在嘗試對特征矩陣部分進行編碼時出現此錯誤。 你能幫我糾正一下嗎?

回溯(最后一次調用):文件“C:\Users\visha\AppData\Local\Continuum\anaconda3\lib\site-packages\nose\case.py”,第 197 行,在 runTest self.test(*self. arg)文件“C:\Users\visha\machinelearning\test.py”,第 22 行,在 test_compute_Phi Phi = compute_Phi(x,2) 文件“C:\Users\visha\machinelearning\linear_regression.py”,第 30 行,在 compute_Phi Phi[:,i] = np.power(x,i).reshape(x.shape[0],) ValueError: could not broadcast input array from shape (1,3) into shape (3,1)

[代碼]

def compute_Phi(x,p):
    x = np.asmatrix(x)
    Phi = np.zeros(shape = (x.shape[0],p))
    for i in range(0,p):
        Phi[:,i] = np.power(x,i).reshape(x.shape[0],)
        Phi = np.asmatrix(Phi)
return Phi 

你的x ,沒有np.mat

In [225]: x = np.array([1,2,3])[:,None]                                                
In [226]: x                                                                            
Out[226]: 
array([[1],
       [2],
       [3]])
In [227]: p = 2                                                                        
In [228]: Phi = np.zeros((3,2))                                                        
In [229]: Phi[:,0] = np.power(x,0)                                                     
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-229-f8ff29de133c> in <module>
----> 1 Phi[:,0] = np.power(x,0)

ValueError: could not broadcast input array from shape (3,1) into shape (3)

為什么? x具有形狀 (3,1) (定義)。 但是Phi[:,0]的形狀為 (3,)。 通過廣播規則, (3,1) 不能放在 (3,) 空間中。 A (1,3) 可以。 A (3,) 可以。

讓我們使x (3,):

In [230]: x = np.array([1,2,3])                                                        
In [231]: Phi[:,0] = np.power(x,0)                                                     
In [232]: Phi[:,1] = np.power(x,1)                                                     
In [233]: Phi                                                                          
Out[233]: 
array([[1., 1.],
       [1., 2.],
       [1., 3.]])

現在我們可以分配列。

現在 (3,1) 形狀x可以同時使用多個冪:

In [234]: np.power(x[:,None],[0,1,2,3])                                                
Out[234]: 
array([[ 1,  1,  1,  1],
       [ 1,  2,  4,  8],
       [ 1,  3,  9, 27]])

這里 (3,1) x使用 (4,) p廣播以產生 (3,4) 結果。

廣播步驟為:(3,1), (4,) => (3,1), (1,4) => (3,4), (3,4)

關鍵是 - 尺寸 1 尺寸可以在 position 中自動添加。 並且尺寸 1 尺寸被縮放以匹配其他尺寸。

實際上錯誤是因為在 for 循環的第一次迭代中你 Phi 是 np.array 並且在第二次迭代中它被更改為矩陣。 如果將行Phi = np.asmatrix(Phi)移到循環之外,它就可以工作

def compute_Phi(x,p):
    x = np.asmatrix(x)
    Phi = np.zeros(shape = (x.shape[0],p))
    Phi = np.asmatrix(Phi)
    for i in range(0,p):
        print(Phi[:,i])
        print(np.power(x,i))
        Phi[:,i] = np.power(x,i)

    return Phi 

compute_Phi(np.mat('1.;2.;3'), 2)

Output

matrix([[1., 1.],
        [1., 2.],
        [1., 3.]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM