Difference between numpy.dot and a.dot(b)

18,393

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.

Share:
18,393
McLawrence
Author by

McLawrence

Updated on June 14, 2022

Comments

  • McLawrence
    McLawrence almost 2 years

    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.

  • mLstudent33
    mLstudent33 almost 5 years
    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?
  • user2357112
    user2357112 almost 5 years
    @mLstudent33: What? No. A.dot(B) gives an error in that case.
  • mLstudent33
    mLstudent33 almost 5 years
    error.shape = (100,1) and X.shape = (100,3), I am able to do error.dot(X) check ln[10] and ln[11] here: github.com/suraggupta/…
  • user2357112
    user2357112 almost 5 years
    @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.)
  • mLstudent33
    mLstudent33 almost 5 years
    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.
  • user2357112
    user2357112 almost 5 years
    @mLstudent33: Those are 1D arrays, not 2D. There is no second dimension of length 1.
  • mLstudent33
    mLstudent33 almost 5 years
    Got it thanks! I was thinking of math where dot product even for a 1D array is explicit with regards to transpose.