如何将 Python 中的三维数组乘以第三维中的一维数组

How to multiply a 3-Dimensional Array in Python by 1-Dimensional Array in The Third Dimension

Python (python 3) 的新手,我一直在为我认为应该非常简单的操作而苦苦挣扎。我有一个 3D 数组,尺寸为:

3d_array =(1, 65436, 8)

我有一组 8 个值,我使用以下方法创建一维数组: force_type_3d = np.array([7206, 7207, 7208, 7211, 7212, 7213, 7214, 7215])

我想将三维数组的第三个维度乘以这些值,这样维度 3 的每一行的维度 2 的每个值都相同。我认为这很简单,但是 Python 数组乘法对我来说似乎非常违反直觉。谢谢

您可以使用 np.multiply:

ono = np.ones((2,2,8))
ono

array([[[1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.]],

       [[1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.]]])

np.multiply(ono,force_type_3d)

array([[[7206., 7207., 7208., 7211., 7212., 7213., 7214., 7215.],
        [7206., 7207., 7208., 7211., 7212., 7213., 7214., 7215.]],

       [[7206., 7207., 7208., 7211., 7212., 7213., 7214., 7215.],
        [7206., 7207., 7208., 7211., 7212., 7213., 7214., 7215.]]])

我们在学龄期的数学中都学过“集合”。只需将 3D numpy 数组视为“集合”的形成。

x = np.zeros((1, 65436, 8)) #or
x = np.ones((1, 65436, 8),dtype=np.int16)

函数zeros创建一个全为0的数组,函数ones创建一个全为ones.By默认值的数组,创建的数组的数据类型为float64。

表示:

1 Sets, 65436 Rows per Set, 8 Columns

那么你可以使用:

result =np.multiply(x1, x2) #or
result = x1*x2