用python将列乘以两个矩阵 [英] Multiply two matrix by columns with python

查看:171
本文介绍了用python将列乘以两个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个矩阵:

A = [a11 a12 

     a21 a22]


B = [b11 b12
     b21 b22]

我想将其所有列相乘(无循环)以获得矩阵:

And I want to multiply all its columns (without loops) in order to obtain the matrix:

C =[a11*b11   a11*b12   a12*b11   a12*b12
    a21*b21   a21*b22   a22*b21   a22*b22]

我尝试过

>>> C = np.prod(A,B,axis=0)

,但是prod不接受两个输入矩阵.都不是np.matrix.prod.

but prod doesn't accept two input matrix. Neither np.matrix.prod.

先谢谢了.

推荐答案

我们可以使用

We could use broadcasting for a vectorized solution -

(A[...,None]*B[:,None]).reshape(A.shape[0],-1)

哲学:就矢量化/广播语言而言,我将其描述为 spreading 或将输入数组的第二维彼此相对,同时保持它们的第一维尺寸对齐.通过使用 None/np.newaxis 输入这两个输入,然后彼此简单相乘.

Philosophy : In terms of vectorized/broadcasting language, I would describe this as spreading or putting the second dimension of the input arrays against each other, while keeping their first dimensions aligned. This spreading is done by introducing new axes with None/np.newaxis for these two inputs and then simply multiplying each other.

数学视图:在一个通用示例的帮助下,让我们使用它的一些数学视图.考虑具有不同列数的输入数组-

Mathematical view : Let's use a bit more mathematical view of it with the help of a generic example. Consider input arrays having different number of columns -

In [504]: A = np.random.rand(2,3)

In [505]: B = np.random.rand(2,4)

首先,扩展尺寸并检查其形状-

First off, extend the dimensions and check their shapes -

In [506]: A[...,None].shape
Out[506]: (2, 3, 1)

In [507]: B[:,None].shape
Out[507]: (2, 1, 4)

现在,执行逐元素乘法,它将以广播方式执行这些乘法.仔细看看输出的形状-

Now, perform the element-wise multiplication, which will perform these multiplications in a broadcasted manner. Take a closer look at the output's shape -

In [508]: (A[...,None]*B[:,None]).shape
Out[508]: (2, 3, 4)

因此,使用None/np.newaxis引入的单例尺寸(长度等于1的尺寸)将是各个数组元素在相乘之前在引擎盖下进行广播的尺寸.这种幕后广播与相应的操作(在这种情况下为乘法)配对,可以非常有效地完成.

So, the singleton dimensions (dimension with length = 1) introduced by the use of None/np.newaxis would be the ones along which elements of the respective arrays would be broadcasted under the hood before being multiplied. This under-the-hood broadcasting paired with the respective operation (multiplication in this case) is done in a very efficient manner.

最后,我们将这个3D数组重塑为2D,使行数与原始输入的行数相同.

Finally, we reshape this 3D array to 2D keeping the number of rows same as that of the original inputs.

样品运行:

In [494]: A
Out[494]: 
array([[2, 3],
       [4, 5]])

In [495]: B
Out[495]: 
array([[12, 13],
       [14, 15]])

In [496]: (A[...,None]*B[:,None]).reshape(A.shape[0],-1)
Out[496]: 
array([[24, 26, 36, 39],
       [56, 60, 70, 75]])

NumPy matrix类型作为输入

NumPy matrix type as inputs

对于 NumPy matrix types 作为输入,我们可以使用 np.asmatrix 可以简单地在输入中创建视图.使用这些视图,将执行广播的逐元素乘法,最终在重塑之后导致2D数组.因此,最后一步将是转换回np.matrix类型.让我们使用相同的示例输入来演示实现-

For NumPy matrix types as the inputs, we could use np.asmatrix that would simply create view into the inputs. Using those views, the broadcasted element-wise multiplication would be performed, finally resulting in a 2D array after the reshaping. So, the last step would be to convert back to np.matrix type. Let's use the same sample inputs to demonstrate the implementation -

In [553]: A
Out[553]: 
matrix([[2, 3],
        [4, 5]])

In [554]: B
Out[554]: 
matrix([[12, 13],
        [14, 15]])

In [555]: arrA = np.asarray(A)

In [556]: arrB = np.asarray(B)

In [557]: np.asmatrix((arrA[...,None]*arrB[:,None]).reshape(A.shape[0],-1))
Out[557]: 
matrix([[24, 26, 36, 39],
        [56, 60, 70, 75]])

这篇关于用python将列乘以两个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆