如果我想在整个矩阵上应用一个函数,为什么 fromiter 会失败?
>>> aaa = np.matrix([[2],[23]])
>>> np.fromiter( [x/2 for x in aaa], np.float)
array([ 1., 11.])
这工作正常,但如果矩阵是 2D,我得到以下错误:
>>> aaa = np.matrix([[2,2],[1,23]])
>>> aaa
matrix([[ 2, 2],
[ 1, 23]])
>>> np.fromiter( [x/2 for x in aaa], np.float)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
我可以使用什么替代品?我知道我可以为行和列编写 2 个循环,但这似乎很慢而且不是 Python 的。提前致谢。