numpy * dot multiply

有一点建议:当我们需要在python中进行像matlab中的矩阵运算时,最好将ndarray转化成matrix,以免出错

  • *
    需要记住的是: numpy arrays consistently abide by the rule that operations are applied element-wise. Thus, if a and b are numpy arrays, then a*b is the array formed by multiplying the components element-wise。
    即,对于ndarray, * 表示的是multiplying the components element-wise 必要时需要广播,举例:
>>> import numpy as np
>>> a = np.array(range(6)).reshape((2,3))                                                                                                                                                                     
>>> b = np.array([1,0,1])
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> b
array([1, 0, 1])
>>> c= a*b
>>> c
array([[0, 0, 2],
       [3, 0, 5]])
>>> d = a*b.T
>>> d
array([[0, 0, 2],
       [3, 0, 5]])
#按照广播规则进行广播,再进行multiplying the components element-wise

而对于matrix,* 则表示矩阵相乘,运算必须保证矩阵相乘的法则:

>>> A=np.matrix(a)
>>> B=np.matrix(b)
>>> A
matrix([[0, 1, 2],
        [3, 4, 5]])
>>> B
matrix([[1, 0, 1]])
>>> C=A*B
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.py", line 341, in __mul__
    return N.dot(self, asmatrix(other))
ValueError: objects are not aligned
# 必须遵守矩阵相乘的法则
>>> C=A*B.T
>>> C
matrix([[2],
        [8]])

  • np.dot
    官方文档:Dot product of two arrays.
    For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b

所以对于ndarray ,一般情况下,都是进行矩阵乘法或者向量的内积运算。但这仅仅是等价于矩阵相乘,但等于就是矩阵相乘。对于ndarray,有时,dot的运算并不要求操作数像矩阵相乘的要求那么严格,当然相乘的结果的格式也不是矩阵,而是数组,举例:

>>> np.dot(a,b)
array([2, 8]) # a 2-D数组, b 1-D数组,不论b是否转置,得到的都得到相同的1-D数组
>>> np.dot(a,b.T)
array([2, 8])

但是对于matrix,矩阵相乘就是矩阵相乘,铁板丁丁,所以必须满足矩阵相乘的条件,举例:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: objects are not aligned
>>> np.dot(A,B.T)
matrix([[2],
        [8]])
# 必须遵守矩阵相乘的法则, 相乘的结果也保证了格式还是矩阵

1. 总之, Matrix是Array的一个小的分支,包含于Array。
2. 所以matrix 拥有array的所有特性。对于2-D数组,dot等价与矩阵相乘
3. 对于matrix,* 和 dot都表示矩阵相乘,必须遵守矩阵相乘法则


  • multiply
    multiply是numpy的ufunc函数,执行方法是对应元素相乘,而不是线性代数中的矩阵运算方式,类似于matlab中的点乘,当矩阵的维度不相同时,会根据一定的广播规则将维数扩充到一致的形式. 如果不能广播相同的size,multiply就会失败,举例:
>>> np.multiply(a,b)                                                                                                                                                                                          
array([[0, 0, 2],
       [3, 0, 5]])
>>> np.multiply(a,b.T)
array([[0, 0, 2],
       [3, 0, 5]])
>>> np.multiply(A,B)                                                                                                                                                                                          
matrix([[0, 0, 2],
        [3, 0, 5]])
>>> np.multiply(A,B.T)                                                                                                                                                                                        
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,3) (3,1) 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值