numpy.dot() in Python

The numpy module of Python provides a function to perform the dot product of two arrays.

  • If both the arrays 'a' and 'b' are 1-dimensional arrays, the dot() function performs the inner product of vectors (without complex conjugation).
  • If both the arrays 'a' and 'b' are 2-dimensional arrays, the dot() function performs the matrix multiplication. But for matrix multiplication use of matmul or 'a' @ 'b' is preferred.
  • If either 'a' or 'b' is 0-dimensional (scalar), the dot() function performs multiplication. Also, the use of numpy.multiply(a, b) or a *b method is preferred.
  • If 'a' is an N-dimensional array and 'b' is a 1-dimensional array, then the dot() function performs the sum-product over the last axis of a and b.
  • If 'a' is an M-dimensional array and 'b' is an N-dimensional array (where N>=2), then the dot() function performs the sum-product over the last axis of 'a' and the second-to-last axis of 'b':
snippet
dot(a, b)[i,j,k,n] = sum(a[i,j,:] * b[k,:,n])

Syntax

snippet
numpy.dot(a, b, out=None)

Parameters

a: array_like

This parameter defines the first array.

b: array_like

This parameter defines the second array.

out: ndarray(optional)

It is an output argument. It should have the exact kind which would be returned in the case when it was not used. Particularly, it should meet the performance feature, i.e., it must contain the right type, i.e., it must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b). Thus, if it does not meet these specified conditions, it raises an exception.

Returns

This function returns the dot product of 'a' and 'b'. This function returns a scalar if 'a' and 'b' are both scalars or 1-dimensional; otherwise, it returns an array. If 'out' is given, then it is returned.

Raises

The ValueError occurs when the last dimension of 'a' is not having the same size as the second-to-last dimension of 'b'.

Example 1:

snippet
import numpy as np
a=np.dot(6,12)
a

Output:

Output
72

Example 2:

snippet
import numpy as np
a=np.dot([2j, 3j], [5j, 8j])
a

Output:

Output
(-34+0j)

Example 3:

snippet
import numpy as np
a = [[1, 2], [4, 1]]
b = [[4, 11], [2, 3]]
c=np.dot(a, b)
c

Output:

Output
array([[ 8, 17], [18, 47]])

In the above code

  • We have imported numpy with alias name np.
  • We have created two 2-dimensional arrays 'a' and 'b'.
  • We have declared the variable 'c' and assigned the returned value of np.dot() function.
  • Lastly, we tried to print the value of 'c'.

In the output, it shows the matrix product as an array.

Example 4:

snippet
import numpy as np
x = np.arange(3*4*5*6).reshape((3,4,5,6))
y = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
p=np.dot(a, b)[2,3,2,1,2,2]
q=sum(a[2,3,2,:] * b[1,2,:,2])
p
q

Output:

Output
499128 499128

In the above code

  • We have imported numpy with alias name np.
  • We have created two arrays 'a' and 'b' using np.arange() function and change the shape of both the arrays using reshape() function.
  • We have declared the variable 'c' and assigned the returned value of np.dot() function
  • Lastly, we tried to print the 'c' value.

In the output, it shows the matrix product as an array.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +