NumPy Matrix Multiplication in Python
Multiplication 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 buildin package NumPy is used for manipulation and arrayprocessing.
These are three methods through which we can perform numpy matrix multiplication.
 First is the use of multiply() function, which perform elementwise 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: Elementwise matrix multiplication
In 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 threedimensional matrix has been shown whose elements are the result of the elementwise multiplication of both array1 and array2 elements.
Output:
array([[[ 9, 16, 21],
[24, 25, 24],
[21, 16, 9]]])
Example 2: Matrix product
Output:
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 threedimensional matrix has been shown whose elements are the product of both array1 and array2 elements.
Example 3: Dot product
These are the following specifications for numpy.dot:
 When both a and b are 1D (one dimensional) arrays> Inner product of two vectors (without complex conjugation)
 When both a and b are 2D (two dimensional) arrays > Matrix multiplication
 When either a or b is 0D (also known as a scalar) > Multiply by using numpy.multiply(a, b) or a * b.
 When a is an ND array and b is a 1D array > Sum product over the last axis of a and b.
 When a is an ND array and b is an MD array provided that M>=2 > Sum product over the last axis of a and the secondtolast 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 threedimensional 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]]]])
