Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between numpy dot() and inner()

What is the difference between

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

and

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

all examples I tried returned the same result. Wikipedia has the same article for both?! In the description of inner() it says, that its behavior is different in higher dimensions, but I couldn't produce any different output. Which one should I use?

like image 840
Milla Well Avatar asked Jun 14 '12 12:06

Milla Well


People also ask

What does dot do in Numpy?

dot() will multiply every value of the array by the scalar (i.e., scalar multiplication). If both inputs are 1-dimensional arrays, np. dot() will compute the dot product of the inputs. If both inputs are 2-dimensional arrays, then np.

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 inner in Numpy?

inner() Advertisements. This function returns the inner product of vectors for 1-D arrays. For higher dimensions, it returns the sum product over the last axes.

Is dot product and inner product the same?

An inner product is a generalization of the dot product. In a vector space, it is a way to multiply vectors together, with the result of this multiplication being a scalar.


2 Answers

numpy.dot:

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

numpy.inner:

Ordinary inner product of vectors for 1-D arrays (without complex conjugation), in higher dimensions a sum product over the last axes.

(Emphasis mine.)

As an example, consider this example with 2D arrays:

>>> a=np.array([[1,2],[3,4]]) >>> b=np.array([[11,12],[13,14]]) >>> np.dot(a,b) array([[37, 40],        [85, 92]]) >>> np.inner(a,b) array([[35, 41],        [81, 95]]) 

Thus, the one you should use is the one that gives the correct behaviour for your application.


Performance testing

(Note that I am testing only the 1D case, since that is the only situation where .dot and .inner give the same result.)

>>> import timeit >>> setup = 'import numpy as np; a=np.random.random(1000); b = np.random.random(1000)'  >>> [timeit.timeit('np.dot(a,b)',setup,number=1000000) for _ in range(3)] [2.6920320987701416, 2.676928997039795, 2.633111000061035]  >>> [timeit.timeit('np.inner(a,b)',setup,number=1000000) for _ in range(3)] [2.588860034942627, 2.5845699310302734, 2.6556360721588135] 

So maybe .inner is faster, but my machine is fairly loaded at the moment, so the timings are not consistent nor are they necessarily very accurate.

like image 185
huon Avatar answered Sep 19 '22 15:09

huon


np.dot and np.inner are identical for 1-dimensions arrays, so that is probably why you aren't noticing any differences. For N-dimension arrays, they correspond to common tensor operations.

np.inner is sometimes called a "vector product" between a higher and lower order tensor, particularly a tensor times a vector, and often leads to "tensor contraction". It includes matrix-vector multiplication.

np.dot corresponds to a "tensor product", and includes the case mentioned at the bottom of the Wikipedia page. It is generally used for multiplication of two similar tensors to produce a new tensor. It includes matrix-matrix multiplication.

If you're not using tensors, then you don't need to worry about these cases and they behave identically.

like image 24
marshall.ward Avatar answered Sep 16 '22 15:09

marshall.ward