numpy矩阵向量乘法 [英] numpy matrix vector multiplication

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

问题描述

当我将两个大小为 (n x n)*(n x 1) 的 numpy 数组相乘时,我得到一个大小为 (n x n) 的矩阵.按照正常的矩阵乘法规则,需要一个 (n x 1) 向量,但我在 Python 的 Numpy 模块中找不到任何有关如何完成此操作的信息.

问题是我不想手动实现它以保持程序的速度.

示例代码如下所示:

a = np.array([[5, 1, 3], [1, 1, 1], [1, 2, 1]])b = np.array([1, 2, 3])打印 a*b>>[[5 2 9][1 2 3][1 4 3]]

我想要的是:

打印 a*b>>[16 6 8]

解决方案

最简单的解决方案

使用 numpy.dota.dot(b).请参阅此处的文档.

<预><代码>>>>a = np.array([[ 5, 1 ,3],[ 1, 1 ,1],[ 1, 2 ,1]])>>>b = np.array([1, 2, 3])>>>打印 a.dot(b)数组([16, 6, 8])

发生这种情况是因为 numpy 数组不是矩阵,并且标准操作 *、+、-、/ 在数组上按元素工作.

请注意,虽然您可以使用 numpy.matrix(截至 2021 年初)其中 * 将被视为标准矩阵乘法,numpy.matrix 已弃用,并且可能会在以后的版本中删除..请参阅其文档中的注释(转载如下):><块引用>

不再推荐使用这个类,即使是线性代数.而是使用常规数组.将来可能会删除该类.

感谢@HopeKing.


其他解决方案

还知道还有其他选择:

  • 如下所述,如果使用 python3.5+,@ 操作符会按您的预期工作:

    <预><代码>>>>打印(a@b)数组([16, 6, 8])

  • 如果你想要矫枉过正,你可以使用numpy.einsum.该文档将让您了解它的工作原理,但老实说,直到阅读这个答案之前,我才完全理解如何使用它 并自己玩弄它.

    <预><代码>>>>np.einsum('ji,i->j', a, b)数组([16, 6, 8])

  • 截至 2016 年年中(numpy 1.10.1),您可以尝试实验性的 numpy.matmul,它的工作原理与 numpy.dot 类似,但有两个主要例外:没有标量乘法,但它有效带有一堆矩阵.

    <预><代码>>>>np.matmul(a, b)数组([16, 6, 8])

  • numpy.innernumpy.dot 的功能相同 对于矩阵-向量乘法,但对于矩阵-矩阵和张量乘法的行为不同(有关 numpy.dot 之间的差异,请参阅维基百科a href="https://en.wikipedia.org/wiki/Inner_product_space" rel="noreferrer">内积和点积一般或有关 numpy 的实现,请参阅此 SO 答案.

    <预><代码>>>>np.inner(a, b)数组([16, 6, 8])# 小心使用矩阵-矩阵乘法!>>>b = a.T>>>np.dot(a, b)数组([[35, 9, 10],[ 9, 3, 4],[10, 4, 6]])>>>np.inner(a, b)数组([[29, 12, 19],[ 7, 4, 5],[ 8, 5, 6]])


边缘情况的罕见选项

  • 如果你有张量(维度大于或等于一的数组),你可以使用 numpy.tensordot 带有可选参数 axes=1:

    <预><代码>>>>np.tensordot(a, b,axis=1)数组([16, 6, 8])

  • 不要使用numpy.vdot 如果您有一个复数矩阵,因为该矩阵将被展平为一维数组,然后它会尝试找到复共轭点展平矩阵和向量之间的乘积(由于 n*mn 的大小不匹配而失败).

When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x n). Following normal matrix multiplication rules, an (n x 1) vector is expected, but I simply cannot find any information about how this is done in Python's Numpy module.

The thing is that I don't want to implement it manually to preserve the speed of the program.

Example code is shown below:

a = np.array([[5, 1, 3], [1, 1, 1], [1, 2, 1]])
b = np.array([1, 2, 3])

print a*b
   >>
   [[5 2 9]
   [1 2 3]
   [1 4 3]]

What I want is:

print a*b
   >>
   [16 6 8]

解决方案

Simplest solution

Use numpy.dot or a.dot(b). See the documentation here.

>>> a = np.array([[ 5, 1 ,3], 
                  [ 1, 1 ,1], 
                  [ 1, 2 ,1]])
>>> b = np.array([1, 2, 3])
>>> print a.dot(b)
array([16, 6, 8])

This occurs because numpy arrays are not matrices, and the standard operations *, +, -, / work element-wise on arrays.

Note that while you can use numpy.matrix (as of early 2021) where * will be treated like standard matrix multiplication, numpy.matrix is deprecated and may be removed in future releases.. See the note in its documentation (reproduced below):

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

Thanks @HopeKing.


Other Solutions

Also know there are other options:

  • As noted below, if using python3.5+ the @ operator works as you'd expect:

    >>> print(a @ b)
    array([16, 6, 8])
    

  • If you want overkill, you can use numpy.einsum. The documentation will give you a flavor for how it works, but honestly, I didn't fully understand how to use it until reading this answer and just playing around with it on my own.

    >>> np.einsum('ji,i->j', a, b)
    array([16, 6, 8])
    

  • As of mid 2016 (numpy 1.10.1), you can try the experimental numpy.matmul, which works like numpy.dot with two major exceptions: no scalar multiplication but it works with stacks of matrices.

    >>> np.matmul(a, b)
    array([16, 6, 8])
    

  • numpy.inner functions the same way as numpy.dot for matrix-vector multiplication but behaves differently for matrix-matrix and tensor multiplication (see Wikipedia regarding the differences between the inner product and dot product in general or see this SO answer regarding numpy's implementations).

    >>> np.inner(a, b)
    array([16, 6, 8])
    
    # Beware using for matrix-matrix multiplication though!
    >>> b = a.T
    >>> np.dot(a, b)
    array([[35,  9, 10],
           [ 9,  3,  4],
           [10,  4,  6]])
    >>> np.inner(a, b) 
    array([[29, 12, 19],
           [ 7,  4,  5],
           [ 8,  5,  6]])
    


Rarer options for edge cases

  • If you have tensors (arrays of dimension greater than or equal to one), you can use numpy.tensordot with the optional argument axes=1:

    >>> np.tensordot(a, b, axes=1)
    array([16,  6,  8])
    

  • Don't use numpy.vdot if you have a matrix of complex numbers, as the matrix will be flattened to a 1D array, then it will try to find the complex conjugate dot product between your flattened matrix and vector (which will fail due to a size mismatch n*m vs n).

这篇关于numpy矩阵向量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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