Numpy - 如何生成以下 n 维矩阵

Numpy - How generate the following n-dimensional matrix

让我们考虑两个矩阵“A,B”$(2 \times 2)$,如何使用 numpy 得到下面的结果“C”?

这里是矩阵 A 和 B 的代码:

import numpy as np

A = np.array([
    [1, 1],
    [2, 2],
])

B = np.array([
    [0.5, 0.3],
    [0.1, 0.6],
])

# My desired output: 
C = [
    [
        [1*0.5, 1*0.3],
        [1*0.1, 1*0.6],
    ],
    [
        [2*0.5, 2*0.3],
        [2*0.1, 2*0.6],
    ],
]

提前致谢!!

您可以 add a new axis 您的第一个数组以便乘以该轴:

C = A[:, np.newaxis]*B