Numpy中关于dot的用法详细分析

dot的用法比较搞,主要是因为要分情况,a,b的位置不同,结果就不同。 其中重要的不仅仅是对于a,b的维度判断,因为这对于a,b哪个axis做alignment很重要(否则就要报错),然后对于产生结果的shape也有直接影响。废话不说,直接列出以下所有情况:

#%% dot of array:
# 情况1: If both a and b are 1-D arrays, it is inner product of vectors (without complex conjugation).
# 本来也就是处理a,b形状一样的情况 也就是shape=(n,)
# 不需要转置直接内积,最后得到一个scalar
# a,b 严格来说既不是column vector也不是row vector, 但是可以act like either one based on the position in a dot product.
a = np.array([1,2,3])
b = np.array([1,2,3])
c = np.dot(a,b)
print("c as inner product of vectors:", c) # c = 14

# 情况2:If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.
a = np.arange(1,5).reshape((2,2))
b = np.arange(5,9).reshape((2,2))
c = np.dot(a,b)
print(c) # [[19 22],[43 50]]


# 情况3:If both a and b are 0-D arrays,  it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
a = 1
b = 2
c = np.dot(1,2)
print(c) # 2

# 情况4: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.
a=np.arange(3).reshape((1,3))
b= np.arange(3) # 这里b是1D,shape(3,)
c= np.dot(a,b) # 用a的最后一个axis也就是3 去align b的唯一axis3, 匹配,然后分别乘法相加
print(c) # 5 as shape(1,) 因为上一步a和b的3都align 相当于抵了,剩下一个a的(1,)就作为c的shape
print(c.shape)

# 情况5: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:
# 这种情况就是需要第一个变量的最后axis去匹配第二个变量的倒数第二个axis
d=np.arange(12).reshape((3,4))
c= np.dot(b,d) # b: shape(3,) d: shape(3,4)
print(c) # array([20, 23, 26, 29]) 其中用b的3 去匹配倒数第二个axis也就是3,那么匹配,所以相互乘法后相加
print(c.shape) # 相互约去3之后,只有d剩一个(,4)这种情况放在shape里就是(4,)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 基础篇NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(...
    oyan99阅读 10,573评论 0 18
  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 13,269评论 0 13
  • 先决条件 在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看Python Tutoria...
    舒map阅读 7,475评论 1 13
  • 前言 numpy是支持 Python语言的数值计算扩充库,其拥有强大的高维度数组处理与矩阵运算能力。除此之外,nu...
    开发者也阅读 8,529评论 0 35
  • 介绍 NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和...
    喔蕾喔蕾喔蕾蕾蕾阅读 5,762评论 0 5

友情链接更多精彩内容