有一点建议:当我们需要在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)