numpy dot用法解释

NumPy库中dot()函数语法定义:

import numpy as np

np.dot(a, b, out=None) #该函数的作用是获取两个元素a,b的乘积.

数组的运算是元素级的,数组相乘的结果是各对应元素的积组成的数组,而对于矩阵而言,需要求的是点积,这里NumPy库提供了用于矩阵乘法的dot函数。

在jupyter notebook中执行的代码运算如下:

dot函数的详解

>>> import numpy as np

>>> np.dot(5,8)

40

如果arr1和arr都是一维数组,那么它返回的就是向量的内积。

>>> arr1 = np.array([2,3])

>>> arr2 = np.array([4,5])

>>> np.dot(arr1,arr2)

23

arr3是二维数组,arr4是向量,具体看向量是在矩阵左边还是右边,然后返回的是矩阵乘法

>>> arr3 =  np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> arr3
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> arr4 = np.array([1,2,3])
>>> arr4
array([1, 2, 3])
>>> print(arr3.shape,arr4.shape)
(3, 3) (3,)
>>> print(np.dot(arr3,arr4))  #此时arr4当作一个3x1的列向量
[14 32 50]
>>> print(np.dot(arr4,arr3))  #此时arr4当作一个1x3的行向量
[30 36 42]

如果arr5和arr6都是二维数组,那么它返回的是矩阵乘法。

dot()函数可以通过NumPy库调用,也可以由数组实例对象进行调用。例如:a.dot(b) 与 np.dot(a,b)效果相同。但矩阵积计算不遵循交换律,np.dot(a,b) 和 np.dot(b,a) 得到的结果是不一样的。

>>> arr5 = np.array([[2,3],[4,5]])

>>> arr5

array([[2, 3],

       [4, 5]])

>>> arr6 = np.array([[6,7],[8,9]])

>>> arr6

array([[6, 7],

       [8, 9]])

>>> np.dot(arr5,arr6)

array([[36, 41],

       [64, 73]])
>>> arr7 = np.array([[2,3,4],[5,6,7]])

>>> arr7

array([[2, 3, 4],

       [5, 6, 7]])

>>> arr8 = np.arange(9).reshape(3,3)

>>> arr8

array([[0, 1, 2],

       [3, 4, 5],

       [6, 7, 8]])

>>> np.dot(arr7,arr8)

array([[33, 42, 51],

       [60, 78, 96]])

多维情况例子如下:

>>> arr9 = np.arange(2*3*4).reshape(2,3,4)

>>> arr9

array([[[ 0,  1,  2,  3],

        [ 4,  5,  6,  7],

        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],

        [16, 17, 18, 19],

        [20, 21, 22, 23]]])

>>> arr10 = np.arange(2*4*5).reshape(2,4,5)

>>> arr10

array([[[ 0,  1,  2,  3,  4],

        [ 5,  6,  7,  8,  9],

        [10, 11, 12, 13, 14],

        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],

        [25, 26, 27, 28, 29],

        [30, 31, 32, 33, 34],

        [35, 36, 37, 38, 39]]])

>>> np.dot(arr9,arr10)

array([[[[  70,   76,   82,   88,   94],

         [ 190,  196,  202,  208,  214]],

        [[ 190,  212,  234,  256,  278],

         [ 630,  652,  674,  696,  718]],

        [[ 310,  348,  386,  424,  462],

         [1070, 1108, 1146, 1184, 1222]]],

       [[[ 430,  484,  538,  592,  646],

         [1510, 1564, 1618, 1672, 1726]],

        [[ 550,  620,  690,  760,  830],

         [1950, 2020, 2090, 2160, 2230]],

        [[ 670,  756,  842,  928, 1014],

         [2390, 2476, 2562, 2648, 2734]]]])

>>> np.dot(arr9,arr10).shape

(2, 3, 2, 5)
>>> arr11 = np.ones((1,3,5))

>>> arr11

array([[[1., 1., 1., 1., 1.],

        [1., 1., 1., 1., 1.],

        [1., 1., 1., 1., 1.]]])

>>> arr12 = np.ones((5,6))*3

>>> arr12

array([[3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.]])

>>> np.dot(arr11,arr12)

array([[[15., 15., 15., 15., 15., 15.],

        [15., 15., 15., 15., 15., 15.],

        [15., 15., 15., 15., 15., 15.]]])

>>> np.dot(arr11,arr12).shape

(1, 3, 6)
>>> arr13 = np.ones((1,3,4,5))

>>> arr13

array([[[[1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.],

         [1., 1., 1., 1., 1.]]]])

>>> arr14 = np.ones((5,6))*3

>>> arr14

array([[3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.],

       [3., 3., 3., 3., 3., 3.]])

>>> np.dot(arr13,arr14)

array([[[[15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.]],

        [[15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.]],

        [[15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.],

         [15., 15., 15., 15., 15., 15.]]]])

>>> np.dot(arr13,arr14).shape

(1, 3, 4, 6)
>>> arr15 = np.ones((1,3,4,5))

>>> arr16 = np.ones((2,5,4))*3

>>> np.dot(arr15,arr16).shape

(1, 3, 4, 2, 4)

a.shape

b.shape

np.dot(a,b).shape

(2,3,4)

(2,4,5)

(2,3,2,5)

(1,3,5)

(5,6)

(1,3,6)

(1,3,4,5)

(5,6)

(1, 3, 4, 6)

(1,3,4,5)

(2,5,4)

(1, 3, 4, 2, 4)

只需a矩阵的最后一维dim等于b矩阵倒数第二维dim即可,对应二维情况就是第一个的列数等于第二个矩阵行数;也就是说点积发生在a,b矩阵最后两个维度上;

注意:不要使用秩为1的数组!!!

例如: x1 = np.array([1, 2, 3])

应该这么写: x1 = np.array([[1, 2, 3]]),包含两个中括号

也可以这么写:x1 = np.array([1, 2, 3]).reshpe((1, -1))

初始化矩阵时:

>>> arr17 = np.random.randn(5) #创建一个5个随机高斯数

>>> print(arr17.shape)

(5,)

应该使用行向量或者列向量创建

>>> arr17 = np.random.randn(1,5) #创建时使用行向量或者列向量

>>> print(arr17.shape)

(1, 5)

>>> arr17 = np.random.randn(5,1)

>>> print(arr17.shape)

(5, 1)

或者使用reshape函数

>>> arr17 = np.random.randn(5).reshape(1,5)

>>> print(arr17.shape)

(1, 5)

>>> arr17 = np.random.randn(5).reshape(5,1)

>>> print(arr17.shape)

(5,1)

矩阵运算时(使用keepdims = True):

A = np.random.randn(4,3)

B = np.sum(A, axis = 1, keepdims = True) # axis=0意思是沿竖直方向

we use (keepdims = True) to make sure that A.shape is (4,1) and not (4, ). It makes our code more rigorous.

我们使用(keepdims = True)来确保A.shape是(4,1)而不是(4,),它使我们的代码更加严格。

主要参考以下文章,如有不妥的地方欢迎大家指正。

Original: https://blog.csdn.net/xiaocainiao521521/article/details/118701232
Author: 白鸥何处去
Title: numpy dot用法解释

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/760149/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球