NumPy Matrix Multiplication in PythonMultiplication of matrix is an operation which produces a single matrix by taking two matrices as input and multiplying rows of the first matrix to the column of the second matrix. Note that we have to ensure that the number of rows in the first matrix should be equal to the number of columns in the second matrix. In Python, the process of matrix multiplication using NumPy is known as vectorization. The main objective of vectorization is to remove or reduce the for loops which we were using explicitly. By reducing 'for' loops from programs gives faster computation. The build-in package NumPy is used for manipulation and array-processing. These are three methods through which we can perform numpy matrix multiplication. - First is the use of multiply() function, which perform element-wise multiplication of the matrix.
- Second is the use of matmul() function, which performs the matrix product of two arrays.
- Last is the use of the dot() function, which performs dot product of two arrays.
Example 1: Element-wise matrix multiplicationIn the above code - We have imported numpy with alias name np.
- We have created an array1 and array2 using numpy.array() function with dimension 3.
- We have created a variable result and assigned the returned value of np.multiply() function.
- We have passed both the array array1 and array2 in np.multiply().
- Lastly, we tried to print the value of the result.
In the output, a three-dimensional matrix has been shown whose elements are the result of the element-wise multiplication of both array1 and array2 elements. Output: array([[[ 9, 16, 21],
[24, 25, 24],
[21, 16, 9]]])
Example 2: Matrix productOutput: array([[[ 30, 24, 18],
[ 84, 69, 54],
[138, 114, 90]]])
In the above code - We have imported numpy with alias name np.
- We have created array1 and array2 using numpy.array() function with dimension 3.
- We have created a variable result and assigned the returned value of the np.matmul() function.
- We have passed both the array array1 and array2 in np.matmul().
- Lastly, we tried to print the value of the result.
In the output, a three-dimensional matrix has been shown whose elements are the product of both array1 and array2 elements. Example 3: Dot productThese are the following specifications for numpy.dot: - When both a and b are 1-D (one dimensional) arrays-> Inner product of two vectors (without complex conjugation)
- When both a and b are 2-D (two dimensional) arrays -> Matrix multiplication
- When either a or b is 0-D (also known as a scalar) -> Multiply by using numpy.multiply(a, b) or a * b.
- When a is an N-D array and b is a 1-D array -> Sum product over the last axis of a and b.
- When a is an N-D array and b is an M-D array provided that M>=2 -> Sum product over the last axis of a and the second-to-last axis of b:
Also, dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
In the above code - We have imported numpy with alias name np.
- We have created array1 and array2 using numpy.array() function with dimension 3.
- We have created a variable result and assigned the returned value of the np.dot() function.
- We have passed both the array array1 and array2 in np.dot().
- Lastly, we tried to print the value of the result.
In the output, a three-dimensional matrix has been shown whose elements are the dot product of both array1 and array2 elements. Output: array([[[[ 30, 24, 18]],
[[ 84, 69, 54]],
[[138, 114, 90]]]])
|