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.
Author by
McLawrence
Updated on June 14, 2022Comments
-
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 almost 5 yearsso for
A.shape = (47,1), B.shape = (47,3)
andA.dot(B)
that gives aC
that is shape(47,3)
by transposing A to do the dot product? -
user2357112 almost 5 years@mLstudent33: What? No.
A.dot(B)
gives an error in that case. -
mLstudent33 almost 5 years
error.shape = (100,1)
andX.shape = (100,3)
, I am able to doerror.dot(X)
checkln[10]
andln[11]
here: github.com/suraggupta/… -
user2357112 almost 5 years@mLstudent33: There's no
error
orerror.dot(X)
in those cells or anywhere in that notebook. (Also, it'sIn
, as in "input", notln
.) -
mLstudent33 almost 5 yearsI apologize, the error is
(h-y)
bothh
andy
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 noticednp.dot
seems to do so as I tried executing with.T
and without and got the same result. -
user2357112 almost 5 years@mLstudent33: Those are 1D arrays, not 2D. There is no second dimension of length 1.
-
mLstudent33 almost 5 yearsGot it thanks! I was thinking of math where dot product even for a 1D array is explicit with regards to transpose.