Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 689
McLawrence Avatar asked Feb 28 '17 19:02

McLawrence


People also ask

What does NumPy dot mean?

numpy.dot(vector_a, vector_b, out = None) returns the dot product of vectors a and b. It can handle 2D arrays but considers them as matrix and will perform matrix multiplication. For N dimensions it is a sum-product over the last axis of a and the second-to-last of b : dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])

What is the difference between NumPy dot and Matmul?

The matmul() function broadcasts the array like a stack of matrices as elements residing in the last two indexes, respectively. The numpy. dot() function, on the other hand, performs multiplication as the sum of products over the last axis of the first array and the second-to-last of the second.

What is NumPy Ndarray dot?

ndarray. dot() function return the dot product of two arrays.

Why is NumPy dot product so fast?

Because np. dot executes the actual arithmetic operations and the enclosing loop in compiled code, which is much faster than the Python interpreter.


1 Answers

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.

like image 109
user2357112 supports Monica Avatar answered Sep 18 '22 13:09

user2357112 supports Monica