NumPy Matrix Multiplication
NumPy Matrix Multiplication
Let’s learn how to perform matrix multiplication in NumPy. We can use the matmul() function, dot() function, or @ operator tp perform the multiplication.
NumPy Matrix Multiplication
For multiplying two matrices using the dot product:
AxB
If the dimension of A is MxN, then the dimension of B must be NxP, resulting in a matrix of MxP.
Using matmul() function
z = np.matmul(x, y)
Using the dot() function
z = np.dot(x, y)
Using @ operator
We can also use the @ operator to perform the matrix multiplication as shown:
z = x @ y
Example
The code shows matrix multiplication on two matrices.
# NumPy Matrix Multiplication
# NumPy Tutorials – www.TestingDocs.com
import numpy as np
x = np.array([[1,5,3], [2,6,9]])
y = np.array([[3,8,2,9],[2,19,2,7],
[1,35,2,8]])
# NumPy Matrix Multiplication
z= np.matmul(x, y)
print(“—————“)
print(“x:”,x)
print(“—————“)
print(“y:”,y)
print(“—————“)
print(“z:”,z)
print(“—————“)
print(x.shape, y.shape, z.shape)
Add the code and then run the code using the IDE run option. The IDE used is Spyder IDE. We can run the code using the following option.
Run >> Run
Verify the output of the program.
—
Python Tutorials
Python Tutorial on this website can be found at:
https://www.testingdocs.com/python-tutorials/
More information on Python is available at the official website: