在 Python 中获取两个多维数组的内积
要获取两个多维数组的内积,请在 Python 中使用 numpy.inner() 方法。1-D 数组向量的普通内积,在更高维度中,是对最后几个轴进行求和乘积。参数是 1 和 b,两个向量。如果 a 和 b 是非标量,它们的最后维度必须匹配。
步骤
首先,导入所需的库 -
import numpy as np
使用 numpy array() 方法创建两个二维数组 −
arr1 = np.array([[5, 10], [15, 20]]) arr2 = np.array([[6, 12], [18, 24]])
显示数组 −
print("Array1...\n",arr1) print("\nArray2...\n",arr2)
检查两个数组的维度 −
print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim)
检查两个数组的形状 −
print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape)
要获取两个多维数组的内积,请在 Python 中使用 numpy.inner() 方法。1-D 数组向量的普通内积,在更高维度中,是对最后几个轴进行求和乘积 −
print("\nResult (Inner Product)...\n",np.inner(arr1, arr2))
示例
import numpy as np # Creating two numpy Two-Dimensional array using the array() method arr1 = np.array([[5, 10], [15, 20]]) arr2 = np.array([[6, 12], [18, 24]]) # Display the arrays print("Array1...\n",arr1) print("\nArray2...\n",arr2) # Check the Dimensions of both the arrays print("\nDimensions of Array1...\n",arr1.ndim) print("\nDimensions of Array2...\n",arr2.ndim) # Check the Shape of both the arrays print("\nShape of Array1...\n",arr1.shape) print("\nShape of Array2...\n",arr2.shape) # To get the Inner product of two multi-dimensional arrays, use the numpy.inner() method in Python print("\nResult (Inner Product)...\n",np.inner(arr1, arr2))
输出
Array1... [[ 5 10] [15 20]] Array2... [[ 6 12] [18 24]] Dimensions of Array1... 2 Dimensions of Array2... 2 Shape of Array1... (2, 2) Shape of Array2... (2, 2) Result (Inner Product)... [[150 330] [330 750]]
广告