Javatpoint Logo
Javatpoint Logo

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':

Syntax

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:

Output:

72

Example 2:

Output:

(-34+0j)

Example 3:

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:

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.


Next Topicnumpy.loadtxt()




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA