14

Is there a difference between

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

and

a.dot(b)

internally? I wasn't able to find any documentation on the latter method.

McLawrence
  • 4,975
  • 7
  • 39
  • 51
  • 1
    [Here's the documentation you couldn't find](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.dot.html), which mostly boils down to "equivalent to `np.dot`". The method only exists on arrays, not arbitrary array-likes, though. – user2357112 Feb 28 '17 at 19:39
  • 1
    `np.dot([1,2,3],[4,5,6])` works, Generally if there's both a function and method version, the function delegates to the method after a bit of argument massaging. In the `dot` case both are compiled so it's harder to study the details. Use whichever form looks prettier in your code. – hpaulj Feb 28 '17 at 21:03
  • 1
    If you plan to use some sophisticated external libs with your numpy-code, consider using the np.dot() variant. This is at least true for [autograd](https://github.com/HIPS/autograd/blob/master/docs/tutorial.md): ```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.``` – sascha Feb 28 '17 at 22:14
  • @user2357112: Does anyone want to formulate an answer, so that the question stays no longer unanswered? – McLawrence Dec 09 '17 at 19:16

1 Answers1

8

If a is an array, they're equivalent. The docs you couldn't find for the dot method are here, and they boil down to "see numpy.dot".

If type(a) is not numpy.ndarray, then numpy.dot will convert a to an array and use the array for the multiplication, while a.dot will do whatever a's type says it does, or raise an AttributeError if a doesn't have a dot method.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • so for `A.shape = (47,1), B.shape = (47,3)` and `A.dot(B)` that gives a `C` that is shape `(47,3)` by transposing A to do the dot product? – mLstudent33 Aug 07 '19 at 11:22
  • 2
    @mLstudent33: What? No. `A.dot(B)` gives an error in that case. – user2357112 Aug 07 '19 at 15:19
  • `error.shape = (100,1)` and `X.shape = (100,3)`, I am able to do `error.dot(X)` check `ln[10]` and `ln[11]` here: https://github.com/suraggupta/coursera-machine-learning-solutions-python/blob/master/Exercise2/exercise2.ipynb – mLstudent33 Aug 10 '19 at 12:41
  • @mLstudent33: There's no `error` or `error.dot(X)` in those cells or anywhere in that notebook. (Also, it's `In`, as in "input", not `ln`.) – user2357112 Aug 10 '19 at 19:17
  • I apologize, the error is `(h-y)` both `h` and `y` are vectors shape `(100, 1)` which results in a vector of shape `(100,1)`. `X` is of shape `(100, 3)`. So it must transpose automatically when necessary? I noticed `np.dot` seems to do so as I tried executing with `.T` and without and got the same result. – mLstudent33 Aug 11 '19 at 07:32
  • 1
    @mLstudent33: Those are 1D arrays, not 2D. There is no second dimension of length 1. – user2357112 Aug 11 '19 at 07:46
  • Got it thanks! I was thinking of math where dot product even for a 1D array is explicit with regards to transpose. – mLstudent33 Aug 11 '19 at 10:57