numpy中的 multiply * 和 dot

1.    我们先定义好秩不为1的2个数组2个矩阵

 1 >>>import numpy as np
 2 >>>a = np.arange(0,4).reshape(2,2)
 3        a
 4 >>>array([[0, 1],                  #数组
 5                  [2, 3]])
 6 >>> b = a
 7         b
 8 >>>array([[0, 1],
 9                  [2, 3]])
10 >>>c = np.mat(a)
11        d = c
12 >>>c
13 >>>matrix([[0, 1],                 #矩阵
14                    [2, 3]])
15 >>>d
16 >>>matrix([[0, 1],
17                    [2, 3]])

   2.分别对秩不为1的矩阵数组进行  3 种运算

>>>np.mautiply(a,b)
>>>array([[0, 1],          #对应位置的元素相乘
            [4, 9]])

>>>np.dot(a,b)            #线性代数中矩阵相乘的法则
>>>array([[ 2,  3],       
            [ 6, 11]])

>>>a * b
>>>array([[0, 1],          #对应位置的元素相乘
            [4, 9]])

### **************************************###
>>>np.mautiply(c,d)
>>>matrix([[0, 1],             #对应位置的元素相乘(注意返回的是矩阵)
            [4, 9]])

>>>np.dot(c,d)
>>>matrix([[ 2,  3],       #    进行矩阵乘法运算
            [ 6, 11]])

>>> c * d
>>>matrix([[ 2,  3],       #    进行矩阵乘法运算
            [ 6, 11]])

 3.上述我们还有一个问题,为什么要分为秩为一和不为一,因为对于秩为1的数组np.dot另有计算方法

>>>import numpy as np
>>>f = np.arange(0,2)
>>>g = f                       #两种相同秩为1的数组
>>>np.dot(f,g)
>>>1                             #对应位置相乘再求和

猜你喜欢

转载自www.cnblogs.com/Pigsss/p/12411400.html