本文介绍了如何将不同尺寸的2 numpy数组相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将2个矩阵x,y与形状(41)和(41,6)相乘因为应该将单个矩阵广播到多维中的每个箭头

I try to multiply 2 matrix x,y with shape (41) and (41,6)as it is supposed to broadcast the single matrix to every arrow in the multi-dimensions

我想这样做:

x*y

但是我得到这个错误

ValueError: operands could not be broadcast together with shapes (41,6) (41,) 

有什么我想念的吗?

推荐答案

不确定,您要实现的目标.也许您可以举例说明您的输入和预期的输出.一种可能性是:

Not exactly sure, what you are trying to achieve. Maybe you could give an example of your input and your expected output. One possibility is:

import numpy as np

x = np.array([[1, 2], [1, 2], [1, 2]])
y = np.array([1, 2, 3])
res = x * np.transpose(np.array([y,]*2))

这会将x的每一列与y相乘,因此上面示例的结果是:

This will multiply each column of x with y, so the result of the above example is:

array([[1, 2],
       [2, 4],
       [3, 6]])

这篇关于如何将不同尺寸的2 numpy数组相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:50