Difference Between Numpy Dot() and Python 3.5+ Matrix Multiplication @

Difference between numpy dot() and Python 3.5+ matrix multiplication @

The @ operator calls the array's __matmul__ method, not dot. This method is also present in the API as the function np.matmul.

>>> a = np.random.rand(8,13,13)
>>> b = np.random.rand(8,13,13)
>>> np.matmul(a, b).shape
(8, 13, 13)

From the documentation:

matmul differs from dot in two important ways.

  • Multiplication by scalars is not allowed.
  • Stacks of matrices are broadcast together as if the matrices were elements.

The last point makes it clear that dot and matmul methods behave differently when passed 3D (or higher dimensional) arrays. Quoting from the documentation some more:

For matmul:

If either argument is N-D, N > 2, it is treated as a stack of matrices residing in the last two indexes and broadcast accordingly.

For np.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

Why is a.dot(b) faster than a@b although Numpy recommends a@b

Your premise is incorrect. You should use larger matrices to measure performance to avoid function calls dwarfing insignificant calculations.

Using Python 3.60 / NumPy 1.11.3 you will find, as explained here, that @ calls np.matmul and both outperform np.dot.

import numpy as np

n = 500
a = np.arange(n**2).reshape(n, n)
b = np.arange(n**2).reshape(n, n)

%timeit a.dot(b) # 134 ms per loop
%timeit a @ b # 71 ms per loop
%timeit np.matmul(a,b) # 70.6 ms per loop

Also note, as explained in the docs, np.dot is functionally different to @ / np.matmul. In particular, they differ in treatment of matrices with dimensions greater than 2.

What is the difference between operators numpy.dot(), * and @ when working on numpy arrays?

The * operator in numpy differs from matrix multiplication. When you do a_inv * b (in your case), you are creating a 3x3 array as follows:

[[a[0,0] * b[0], a[0,1] * b[0], a[0,2] * b[0]],
[a[1,0] * b[1], a[1,1] * b[1] ...]] #etc.

Asides from dot you can do matrix multiplication with np.matmul(a_inv, b), or you can explicitly tell numpy that your arrays are matrices by using np.matrix:

a_inv = np.matrix(a_inv)
b = np.matrix(b).T # note that b will be a row vector, so you need to transpose it to make it a column vector
a_inv * b #now numpy will interpret '*' as matrix multiplication
>>[[-2. ]
[ 0.5]
[-1. ]]

Numpy function not differentiating between dot product and matrix multiplication

Note that 1-dimensional numpy arrays are not column vectors (and operations such as transposition do not make sense). If you want to obtain a column vector you should define your array as a 2-dimensional array (with the second dimension size equal to 1).

However, you don't need to define a column vector, as numpy offers functions to do what you want by manipulating an 1D array as follows

P = np.outer(a,a)/np.inner(a,a)

Difference between np.matmul und PEP 465

That is correct. As you can read in the np.matmul documentation:

Notes

The matmul function implements the semantics of the @ operator introduced in Python 3.5 following PEP465.

What is the difference between using matrix multiplication with np.matrix arrays, and dot()/tensor() with np.arrays?

The main advantage of working with matrices is that the *symbol performs a matrix multiplication, whereas it performs an element-wise multiplications with arrays. With arrays you need to use dot. See:
http://wiki.scipy.org/NumPy_for_Matlab_Users
What are the differences between numpy arrays and matrices? Which one should I use?

If m is a one dimensional array, you don't need to transpose anything, because for 1D arrays, transpose doesn't change anything:

In [28]: m.T.shape, m.shape
Out[28]: ((3,), (3,))
In [29]: m.dot(C)
Out[29]: array([15, 18, 21])

In [30]: C.dot(m)
Out[30]: array([ 5, 14, 23])

This is different if you add another dimension to m:

In [31]: mm = m[:, np.newaxis]

In [32]: mm.dot(C)
--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-28253c9b8898> in <module>()
----> 1 mm.dot(C)

ValueError: objects are not aligned

In [33]: (mm.T).dot(C)
Out[33]: array([[15, 18, 21]])

In [34]: C.dot(mm)
Out[34]:
array([[ 5],
[14],
[23]])

What are the differences between numpy arrays and matrices? Which one should I use?

As per the official documents, it's not anymore advisable to use matrix class since it will be removed in the future.

https://numpy.org/doc/stable/reference/generated/numpy.matrix.html

As other answers already state that you can achieve all the operations with NumPy arrays.



Related Topics



Leave a reply



Submit