1 回答

TA貢獻1802條經驗 獲得超5個贊
由于您沒有提供Xor Y,我不會看:
(Y.T @ -np.log(hyp) + (1-Y).T @ -np.log(1-hyp))
但與:
(theta[1:].T @ theta[1:]).item()
如果theta是(n,1):
In [15]: theta = np.arange(5)[:,None]
In [16]: theta.shape
Out[16]: (5, 1)
In [17]: (theta[1:].T @ theta[1:])
Out[17]: array([[30]])
In [18]: (theta[1:].T @ theta[1:])[0,0]
Out[18]: 30
In [19]: (theta[1:].T @ theta[1:]).item()
Out[19]: 30
但是如果你把它theta給minimize,它會把它分解成 (n,) 形狀:
In [20]: theta=theta.ravel()
In [21]: (theta[1:].T @ theta[1:])
Out[21]: 30
In [22]: (theta[1:].T @ theta[1:]).shape
Out[22]: ()
In [23]: (theta[1:].T @ theta[1:]).item()
Out[23]: 30
In [24]: (theta[1:].T @ theta[1:])[0,0]
...
IndexError: invalid index to scalar variable.
I 正如最初所寫的那樣,item可以與單個項目數組一起使用,而不管尺寸如何。 [0,0]僅適用于二維(或更高)數組。
添加回答
舉報