NumPy dot() Function:
np.dot function: The dot() method in the NumPy module is used to do the dot product of two arrays. Specifically,
- It is the inner product of vectors if both a and b are 1-D arrays (without complex conjugation).
- Matrix multiplication is used when both a and b are 2-D arrays, however, matmul or a @ b is preferred.
- If either a or b is 0-D (scalar), it is equivalent to multiply and using numpy.multiply(a, b) or a * b is preferred.
- It is a sum product over the last axis of a and b if “a” is an N-D array and “b” is a 1-D array.
- It is a sum product over the final axis of a and the second-to-last axis of b if “a” is an N-D array and “b” is an M-D array (where M>=2).
dot(a, b)[i, j, k, m] = sum(a[i, j, :] * b[k, : ,m])
Syntax:
numpy.dot(a, b, out=None)
Parameters
a: This is required. It is the first array_like parameter given as input.
b: This is required. It is the second array_like parameter given as input.
out: This is optional. It is the location where the result will be stored.
Return Value:
.dot() python: The dot product of a and b is returned. A scalar is returned if a and b are both scalars or both 1-D arrays; otherwise, an array is returned. If out is specified, it will be returned.
Note: If the last dimension of a is not the same size as the second-to-last dimension of b, the ValueError exception is thrown.
NumPy dot() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Pass two random numbers as the arguments to the dot() function of numpy module to calculate the dot product of the given two numbers.
- Store it in a variable.
- Print the dot product of the given two numbers.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass two random numbers as the arguments to the dot() function # of numpy module to calculate the dot product of given two numbers # Store it in a variable. rslt = np.dot(4, 6) # Print the dot product of the given two numbers print("The dot product of given two numbers = ", rslt)
Output:
The dot product of given two numbers = 24
Example2
Approach:
- Import numpy module using the import keyword.
- Give the first array as static input and store it in a variable.
- Give the second array as static input and store it in another variable.
- Pass the given two array’s as an argument to the dot() function of numpy module to get the inner dot product of the given two arrays.
- Store it in another variable.
- Print the inner dot product of the given two arrays.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Give the first array as static input and store it in a variable. fst_arry = [4, 9, 2] # Give the second array as static input and store it in another variable. scnd_arry = [6, 3, 5] # Pass the given two array's as an argument to the dot() function of numpy module # to get the inner dot product of the given two arrays. # Store it in another variable. rslt = np.dot(fst_arry, scnd_arry) # Print the inner dot product of the given two arrays. print("The inner dot product of the given two arrays = ", rslt)
Output:
The inner dot product of the given two arrays = 61
Example3
Numpy dot function: Here dot() function is used to calculate the dot product for the complex numbers
# Import numpy module using the import keyword import numpy as np # Pass the random list of complex numbers as an argument to the array() function # to create an array. # Store it in a variable. fst_arry = np.array([2+4j, 1+3j]) # Pass the random list of complex numbers as an argument to the array() function # to create another array. # Store it in another variable. scnd_arry = np.array([1+2j, 3+2j]) # Pass the given two array's as the argument to the dot() function of numpy module # to get the dot product of the given two arrays of complex numbers. # Store it in another variable. rslt = np.dot(fst_arry, scnd_arry) # Print the dot product of the given two arrays of complex numbers. print("The dot product of the given two arrays of complex numbers = ") print(rslt)
Output:
The dot product of the given two arrays of complex numbers = (-9+19j)
Example4
Dot function python: Here dot() function is used to calculate the dot product for the 2D arrays i.e matrix.
The dot() function returns matrix multiplication when two matrices are given.
# Import numpy module using the import keyword import numpy as np # Pass the random list(2D) as an argument to the array() function # to create an array. # Store it in a variable. fst_arry = np.array([[5, 6], [1, 7]]) # Pass the random list(2D) as an argument to the array() function # to create another array. # Store it in another variable. scnd_arry = np.array([[2, 4], [8, 7]]) # Pass the given two array's as the argument to the dot() function of numpy module # to get the dot product of the given two 2D arrays # Here it returns the matrix multiplcation of given arrays(matrices) # Store it in another variable. rslt = np.dot(fst_arry, scnd_arry) # Print the matrix multiplcation of given two arrays(matrices) print("The matrix multiplcation of given two arrays(matrices) = ") print(rslt)
Output:
The matrix multiplcation of given two arrays(matrices) = [[58 62] [58 53]]