【问题标题】:Difference between numpy.dot and a.dot(b)numpy.dot 和 a.dot(b) 的区别
【发布时间】:2017-07-19 22:15:41
【问题描述】:

有没有区别

import numpy as np
np.dot(a,b)

a.dot(b)

在内部? 我找不到关于后一种方法的任何文档。

【问题讨论】:

  • Here's the documentation you couldn't find,主要归结为“相当于np.dot”。但是,该方法只存在于数组上,而不是任意的类似数组。
  • np.dot([1,2,3],[4,5,6]) 有效,通常如果同时有函数和方法版本,则函数会在经过一些参数按摩后委托给方法。在dot 案例中,两者都已编译,因此更难研究细节。使用在您的代码中看起来更漂亮的表单。
  • 如果您打算在您的 numpy 代码中使用一些复杂的外部库,请考虑使用 np.dot() 变体。 autograd 至少是这样:Similarly, we don't support the syntax A.dot(B); use the equivalent np.dot(A, B) instead. The reason we don't support the first way is that subclassing ndarray raises a host of issues. As another consequence of not subclassing ndarray, some subclass checks can break, like isinstance(x, np.ndarray) can return False.
  • @user2357112:有没有人想提出一个答案,让这个问题不再悬而未决?

标签: python numpy matrix-multiplication


【解决方案1】:

如果a 是一个数组,它们是等价的。您找不到 dot 方法的文档是 here,它们归结为“请参阅 numpy.dot”。

如果是type(a) is not numpy.ndarray,那么numpy.dot 会将a 转换为一个数组并使用该数组进行乘法运算,而a.dot 将执行a 的类型所说的任何操作,或者在以下情况下引发一个AttributeError a 没有 dot 方法。

【讨论】:

  • 那么对于A.shape = (47,1), B.shape = (47,3)A.dot(B),通过转置A 来进行点积,得到C 形状为(47,3)
  • @mLstudent33:什么?否。A.dot(B) 在这种情况下会出错。
  • error.shape = (100,1)X.shape = (100,3),我可以在 error.dot(X) 检查 ln[10]ln[11] 这里:github.com/suraggupta/…
  • @mLstudent33:这些单元格中或该笔记本中的任何位置都没有errorerror.dot(X)。 (另外,它是In,如“输入”,而不是ln。)
  • @mLstudent33:这些是一维数组,不是二维的。没有长度为 1 的第二维。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-03
  • 2015-02-21
  • 2016-03-16
相关资源
最近更新 更多