如何将numpy 2D数组与numpy 1D数组相乘?

Ash*_*ppa 21 python numpy

两个数组:

a = numpy.array([[2,3,2],[5,6,1]])
b = numpy.array([3,5])
c = a * b
Run Code Online (Sandbox Code Playgroud)

我想要的是:

c = [[6,9,6],
     [25,30,5]]
Run Code Online (Sandbox Code Playgroud)

但是,我收到了这个错误:

ValueError: operands could not be broadcast together with shapes (2,3) (2)
Run Code Online (Sandbox Code Playgroud)

如何 nD数组乘以 1D数组,在哪里len(1D-array) == len(nD array)

HYR*_*YRY 25

您需要将数组b转换为(2,1)形状数组,使用None或numpy.newaxis在索引元组中:

import numpy
a = numpy.array([[2,3,2],[5,6,1]])
b = numpy.array([3,5])
c = a * b[:, None]
Run Code Online (Sandbox Code Playgroud)

这是文件.