简体   繁体   English

numpy矩阵乘法

[英]numpy matrix multiplication

I am trying to figure out how to do a kind of scalar matrix multiplication in numpy. 我试图找出如何在numpy中进行一种标量矩阵乘法。

I have 我有

a = array(((1,2,3),(4,5,6)))
b = array((11,12))

and i want to do 而且我想做

a op b

to result in 以导致

array(((1*11,2*11,3*11),(4*12,5*12,6*12))

right now I am using the following expression 现在我使用以下表达式

c= a * array((b, b, b)).transpose() c = a * array((b,b,b))。transpose()

It seems like there must be a more efficient way of doing this though 似乎必须有一种更有效的方法来做到这一点

利用广播

(a.T * b).T

The alternative to transposing a is to change the shape of b to make broadcasting give the result you're looking for: 转置a的替代方法是更改b的形状以使广播产生您正在寻找的结果:

a * b[:, np.newaxis]

Note that adding the new axis to b gives the following array: 请注意,将新轴添加到b会给出以下数组:

array([[11],
       [12]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM