numpy.dot、numpy.trace函数用法

这篇文章帮助学习者了解下numpy的两个函数,dot和trace

numpy.dot  

这个函数实现的是矩阵乘法

官方文档如下:

    numpy.dot(a, b, out=None)
        Dot product of two arrays. Specifically,
    
    If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation)
    If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
    
    If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
    
    If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
    
    If a is an N-D array and b is an M-D array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:

 如果a和b都是一维数组,那么它是向量的内积。  

 如果a和b都是二维数组,则它是矩阵乘法,但最好使用矩阵或a@b。  

 如果a或b是0-d(标量),则它等于乘法,并且优选使用numpy.multiply(a、b)或a*b。  

 如果a是n-d数组,b是一维数组,那么它是a和b的最后一个轴上的和积。  

 如果a是n-d数组,而b是m-d数组(其中为m>=2),则它是a的最后一个轴和b的第二到最后一个轴上的和积

下面给出一些简单的例子

在a和b都是二维数组的情况下,将a,b设置为形状为(3,3)和(3,4)的随机数矩阵,并给出他们的乘法运算

import numpy as np
a = np.random.random((3,3))
b = np.random.random((3,4))

a,b
(array([[0.69680474, 0.59586631, 0.95972126],
        [0.73228572, 0.66192343, 0.90553587],
        [0.31675047, 0.08604421, 0.21743264]]),
 array([[0.88048267, 0.80778826, 0.29459481, 0.83663852],
        [0.91770254, 0.53513741, 0.19388619, 0.00294862],
        [0.80654194, 0.02093392, 0.01940262, 0.8697376 ]]))

np.dot(a,b)
array([[1.93440797, 0.90183176, 0.33942641, 1.41943634],
       [1.98256636, 0.96470821, 0.36163515, 1.40218881],
       [0.53322484, 0.30646451, 0.11421459, 0.4543687 ]])

a @ b
array([[1.93440797, 0.90183176, 0.33942641, 1.41943634],
       [1.98256636, 0.96470821, 0.36163515, 1.40218881],
       [0.53322484, 0.30646451, 0.11421459, 0.4543687 ]])

 

numpy.trace

这个函数实现的是迹运算

官方文档如下:

      numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)

   Return the sum along diagonals of the array.

    If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i.

如果a是二维数组,那么对角线和将会依据所给的offset参数返回。

 

If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.

如果a具有超过两个维度,则由轴1和轴2指定的轴用于确定返回迹的二维子数组。结果阵列的形状与去除轴1和轴2的形状相同。 

示例代码如下,下面的代码展示了四维矩阵a的形状,以及在相关参数下的迹运算的形状以及值

import numpy as np
a = np.arange(3*4*5*6).reshape((3,4,5,6))

a.shape
(3, 4, 5, 6)

a.trace(axis1=2,axis2=1).shape
(3, 6)

a.trace(axis1=2,axis2=1)
array([[ 216,  220,  224,  228,  232,  236],
       [ 696,  700,  704,  708,  712,  716],
       [1176, 1180, 1184, 1188, 1192, 1196]])

a.trace()
array([[450, 453, 456, 459, 462, 465],
       [468, 471, 474, 477, 480, 483],
       [486, 489, 492, 495, 498, 501],
       [504, 507, 510, 513, 516, 519],
       [522, 525, 528, 531, 534, 537]])

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值