我有以下代码:
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
我不明白为什么我会收到此错误。一切似乎都是2-d。
答案 0 :(得分:1)
x_0[0]
的类型仍然是numpy.matrixlib.defmatrix.matrix
,而不是标量值。
您需要获得一个比例值作为矩阵元素。试试这段代码
test = J(x_0.item(0), x_0.item(1))