0
我有以下代碼:NumPy的矩陣必須是2維誤差
import numpy as np
def J(x, y):
return np.matrix([[8-(4 * y), -4 * y], [y, -5 + x]])
x_0 = np.matrix([[1], [1]])
test = J(x_0[0], x_0[1])
當我去運行它,我收到以下錯誤:
Traceback (most recent call last):
File "broyden.py", line 15, in <module>
test = J(x_0[0][0], x_0[1][0])
File "broyden.py", line 12, in J
return np.matrix([[8-(4 * y), -4 * y], [y, -5 + x]])
File "/home/collin/anaconda/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 261, in __new__
raise ValueError("matrix must be 2-dimensional")
ValueError: matrix must be 2-dimensional
我不明白爲什麼我會得到這個錯誤。一切似乎都是二維的。
好的,這是有道理的。如果我想從2x2 numpy矩陣的第二行第二列得到標量值,該怎麼辦? – user2079802
'x_0.item(i,j)'也適用。但爲什麼不使用'np.array'而不是'np.matrix'? – emeth