gpt4 book ai didi

python - 矩阵和数组的 NumPy 点积

转载 作者:行者123 更新时间:2023-12-04 17:20:16 25 4
gpt4 key购买 nike

来自 numpy 的文档:https://numpy.org/doc/stable/reference/generated/numpy.dot.html#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 的最后一个轴是列,对吗?
然而,当我在 Python IDLE 中尝试这个时,这是我的输出:
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> b
[1, 2]
>>> a.dot(b)
array([ 5, 11, 17])
我预计这会引发错误,因为 a 的列的维度大于 b 的行的维度。

最佳答案

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的最后一个轴有尺寸 2:
>>> a.shape
(3, 2)
匹配 b s 大小。
请记住:numpy 中多维数组的第一个轴是“向下”。一维数组在 numpy 中水平显示,但我认为通常最好将它们视为垂直向量。这是正在计算的内容:
enter image description here

关于python - 矩阵和数组的 NumPy 点积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66619157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com