我想用 numpy 来计算倒数。但我收到一个错误:
'numpy.ndarry' object has no attribute I
要在numpy中计算矩阵的逆,比如矩阵M,它应该很简单: print M.I
这是代码:
x = numpy.empty((3,3), dtype=int)
for comb in combinations_with_replacement(range(10), 9):
x.flat[:] = comb
print x.I
我假设,发生此错误是因为 x 现在是平坦的,因此“ I
”命令不兼容。有解决办法吗?
我的目标是打印每个可能的数字矩阵组合的逆矩阵。
原文由 Jake Z 发布,翻译遵循 CC BY-SA 4.0 许可协议
I
属性仅存在于matrix
对象上,而不是ndarray
s。您可以使用numpy.linalg.inv
来反转数组:请注意,您生成矩阵的方式并非所有矩阵都是可逆的。您要么需要更改生成矩阵的方式,要么跳过不可逆的矩阵。
此外,如果您想遍历所有元素从 [0, 10) 中提取的 3x3 矩阵,则需要以下内容:
而不是
combinations_with_replacement
,或者你会跳过像这样的矩阵