在 Python 中计算 N 维数组的逆
要计算 N 维数组的逆,请使用 numpy. Python中的方法。结果是相对于 tensordot 操作的逆,即达到浮点精度,tensordot( , a, ind) 是 tensordot 操作的“恒等”张量。linalg.tensorinv()tensordot(a, b, ind)tensorinv(a)
该方法返回a的张量逆,形状a.shape[ind:] + a.shape[:ind]。第一个参数是 a,要“反转”的张量。它的形状必须是“方形”,即 prod(a.shape[:ind]) == prod(a.shape[ind:])。第二个参数是 ind,即反和中涉及的第一个索引的数量。必须为正整数,默认为 2。
脚步
首先,导入所需的库 -
import numpy as npfromnumpy.linalgimport inv
创建一个数组。返回一个二维数组,其中对角线为 1,其他位置为 0 -numpy.eye()
arr = np.eye(4*6)
更改上面创建的数组的形状 -
arr.shape = (4, 6, 8, 3)
显示数组 -
print("Our Array...\n",arr)
检查尺寸 -
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 -
print("\nDatatype of our Array object...\n",arr.dtype)
获得形状 -
print("\nShape of our Array object...\n",arr.shape)
要计算 N 维数组的逆,请使用 numpy. Python中的方法 -linalg.tensorinv()
print("\nResult...\n",np.linalg.tensorinv(arr))
示例
import numpy as np输出结果fromnumpy.linalgimport inv
#创建一个数组
#numpy.eye() 返回一个二维数组,其中对角线为 1,其他位置为 0
arr = np.eye(4*6)
#更改上面创建的数组的形状
arr.shape = (4, 6, 8, 3)
#显示数组
print("Our Array...\n",arr)
#检查尺寸
print("\nDimensions of our Array...\n",arr.ndim)
#获取数据类型
print("\nDatatype of our Array object...\n",arr.dtype)
#获取形状
print("\nShape of our Array object...\n",arr.shape)
#要计算 N 维数组的逆,请使用 Python 中的 numpy.linalg.tensorinv() 方法。
print("\nResult...\n",np.linalg.tensorinv(arr))
Our Array...[[[[1. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[0. 1. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0.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