zoukankan      html  css  js  c++  java
  • Python 中的几种矩阵乘法 np.dot, np.multiply, *

    Python 中的几种矩阵乘法 np.dot, np.multiply, *

    结论:

    元素乘法:np.multiply(a,b)
    矩阵乘法:np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b) 或直接用 a @ b !
    唯独注意:*,在 np.array 中重载为元素乘法,在 np.matrix 中重载为矩阵乘法!

    对于 np.array 对象

    >>> a
    array([[1, 2],
           [3, 4]])

    元素乘法 用 a*b 或 np.multiply(a,b) ,

    >>> a*a
    array([[ 1,  4],
           [ 9, 16]])
    
    >>> np.multiply(a,a)
    array([[ 1,  4],
           [ 9, 16]])

    矩阵乘法 用 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)。

    >>> np.dot(a,a)
    array([[ 7, 10],
           [15, 22]])
    
    >>> np.matmul(a,a)
    array([[ 7, 10],
           [15, 22]])
    
    >>> a.dot(a)
    array([[ 7, 10],
           [15, 22]])

    对于 np.matrix 对象

    >>> A
    matrix([[1, 2],
            [3, 4]])

    元素乘法 用 np.multiply(a,b)

    >>> np.multiply(A,A)
    matrix([[ 1,  4],
            [ 9, 16]])

    矩阵乘法 用 a*b 或 np.dot(a,b) 或 np.matmul(a,b) 或 a.dot(b)。

    >>> A*A
    matrix([[ 7, 10],
            [15, 22]])
    >>> np.dot(A,A)
    matrix([[ 7, 10],
            [15, 22]])
    >>> np.matmul(A,A)
    matrix([[ 7, 10],
            [15, 22]])
    >>> A.dot(A)
    matrix([[ 7, 10],
            [15, 22]])

    参考链接:https://blog.csdn.net/itnerd/article/details/83444867

  • 相关阅读:
    CSS属性之定位
    CSS选择器区别
    HTML属性及其相关区别
    HTML标签区别
    HTML其他概念
    CSS3新特性
    HTML5新标签
    HTML5新特性
    params修饰符的用法
    C#中引用参数ref和输出参数out
  • 原文地址:https://www.cnblogs.com/Timeouting-Study/p/12395689.html
Copyright © 2011-2022 走看看