- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matrix product of two arrays in Numpy
To find the matrix product of two arrays, use the numpy.matmul() method in Python Numpy. If both arguments are 2-D they are multiplied like conventional matrices. Returns the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors.
The out is a location into which the result is stored. If provided, it must have a shape that matches the signature (n,k),(k,m)->(n,m). If not provided or None, a freshly-allocated array is returned.
Steps
At first, import the required library −
import numpy as np
Create two 2D arrays −
arr1 = np.array([[5, 7], [10, 15]]) arr2 = np.array([[11, 12], [19, 20]])
Display the arrays −
print("Array 1...
", arr1) print("
Array 2...
", arr2)
Get the type of the arrays −
print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)
Get the dimensions of the Arrays −
print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)
Get the shape of the Arrays −
print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)
To find the matrix product of two arrays, use the numpy.matmul() method in Python Numpy. If both arguments are 2-D they are multiplied like conventional matrices −
print("
Result (matrix product)...
",np.matmul(arr1, arr2))
Example
import numpy as np # Create two 2D arrays arr1 = np.array([[5, 7], [10, 15]]) arr2 = np.array([[11, 12], [19, 20]]) # Display the arrays print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To find the matrix product of two arrays, use the numpy.matmul() method in Python Numpy # If both arguments are 2-D they are multiplied like conventional matrices. print("
Result (matrix product)...
",np.matmul(arr1, arr2))
Output
Array 1... [[ 5 7] [10 15]] Array 2... [[11 12] [19 20]] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 2) Our Array 2 Shape... (2, 2) Result (matrix product)... [[188 200] [395 420]]
- Related Articles
- Return the inner product of two masked arrays in Numpy
- Return the dot product of two masked arrays in Numpy
- Return the outer product of two masked arrays in Numpy
- Return the inner product of two masked One-Dimensional arrays in Numpy
- Return the inner product of two masked Three Dimensional arrays in Numpy
- Return the outer product of two masked Three-Dimensional Numpy arrays
- Return the outer product of two masked One-Dimensional Numpy arrays
- Return the inner product of two masked arrays with different shapes in Numpy
- Return the outer product of two masked arrays with different shapes in Numpy
- Return the dot product of two masked arrays and set whether masked data is propagated in Numpy
- Get the Outer product of two arrays in Python
- Get the Inner product of two arrays in Python
- Get the Kronecker product of two arrays in Python
- Element wise concatenation of two NumPy arrays of string
- Get the Outer product of two multidimensional arrays in Python
