Updated March 23, 2023
Introduction to Matrix in NumPy
Matrix is a subclass within ndarray class in the Numpy python library. It is primarily used to convert a string or an array-like object into a 2D matrix. The matrix so returned is a specialized 2D array. Coming to the syntax, a matrix function is written as follows:
Syntax:
numpy.matrix(data, dtype, copy)
Important Parameters:
- Data: Data should be in the form of an array-like an object or a string separated by commas
- Dtype: Data type of the returned matrix
- Copy: This a flag like an object. It determines whether if data is already an array. The flag determines whether the data is copied or whether a new view is constructed.
How to Create a Matrix in NumPy?
We can create a matrix in Numpy using functions like array(), ndarray() or matrix(). Matrix function by default creates a specialized 2D array from the given input. The input should be in the form of a string or an array object-like. Let’s demonstrate matrix creation using a matrix() with string as an input type.
Code:
import numpy as np
#creating matrix from string
A = np.matrix('1 2 3; 4 5 6')
print("Array created using string is :\n", A)
Output:
Now, let’s demonstrate matrix creation using a matrix() with an array-like object as the input type.
Code:
import numpy as np
#creating matrix from array like object
B = np.matrix([[1, 2, 3], [4, 5, 6]])
print("Array created using array like object is :\n", B)
Output:
Matrix Functions in NumPy with Examples
Having discussed Numpy matrix creation, now let’s discuss some important functions in this class. All the functions in the matrix subclass are very similar to the ndarray class.
Function | Description |
matrix.T | Returns transpose of the input matrix |
matrix.H | Returns complex conjugate transpose of the input matrix |
matrix.I | Returns the multiplicative inverse of the matrix |
matrix.A | Return the input matrix as a ndarray object. |
matrix.all() | Checks whether all matrix elements along a given axis evaluate to True. It is similar to ndarray.all() function. |
matrix.any() | Checks whether any of the matrix elements along a given axis evaluate to True. |
matrix.argmax() | Returns the indices of the maximum values along an axis in the input matrix |
matrix.argmin() | Returns the indices of the minimum values along an axis in the input matrix |
matrix.argsort() | Returns the indices that would sort the matrix |
matrix.astype() | Returns the copy of the input matrix after type change |
matrix.choose() | Returns a new matrix with chosen indices from the input matrix |
matrix.clip() | Returns a new matrix within chosen limits from the input matrix |
matrix.compress() | Returns the selected slice of a matrix along the given axis |
matrix.conj() | Returns the complex conjugate of the given matrix |
matrix.cumprod() | Returns cumulative product of elements in the given matrix along a given axis |
matrix.cumsum() | Returns the cumulative sum of elements in the given matrix along with the given axis |
matrix.diagonal() | Returns diagonal elements of the matrix |
matrix.dot() | The returns dot product of two matrices |
Few examples to illustrate matrix functions
Example #1 – Program to Find Transpose and Multiplicative Inverse of a Given Matrix
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6')
print("Matrix is :\n", A)
#Transpose of matrix
print("The transpose of matrix A is :\n", A.getT())
#Complex conjugate transpose
print("Complex transpose of matrix A is :\n", A.getH())
#Multiplicative inverse
print("Multiplicative inverse of matrix A is :\n", A.getI())
Output:
Example #2 – Program to Find the Maximum and Minimum Indices in A Given Matrix
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6')
print("Matrix is :\n", A)
#maximum indices
print("Maximum indices in A :\n", A.argmax(0))
#minimum indices
print("Minimum indices in A :\n", A.argmin(0))
Output:
Example #3 – Program to Illustrate Clipping in A Given Matrix
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6')
print("Matrix is :\n", A)
#clipping matrix
print("Clipped matrix is :\n", A.clip(1,4))
Output:
Clip, slice, compress and choose are similar functions that can be used to select a specific part of the input matrix/array.
Example #4 – Program to Find Cumulative Sum and Product of a Given Matrix
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6; 7,8,9')
print("Matrix is :\n", A)
#cumulative product along axis = 0
print("Cumulative product of elements along axis 0 is : \n", A.cumprod(0))
#cumulative sum along axis = 0
print("Cumulative sum of elements along axis 0 is : \n", A.cumsum(0))
Output:
Example #5 – Program to Find the Dot Product and Diagonal Values of a Given Matrix
In order to find the diagonal values of a given matrix, we can use a diagonal function with attributes such as offset, axis 1 and axis 2.
Code:
import numpy as np
A = np.matrix('1 2 3; 4 5 6; 7,8,9')
print("Matrix is :\n", A)
#diagonal values
print("Diagonal of matrix A :\n", A.diagonal(0,0,1))
#dot product
print("Dot product of matrix A with 2 :\n", A.dot(2))
Output:
Advantages of Matrix in NumPy
- One of the main advantages of using the NumPy matrix is that they take less memory space and provide better runtime speed when compared with similar data structures in python(lists and tuples).
- NumPy matrix support some specific scientific functions such as element-wise cumulative sum, cumulative product, conjugate transpose, and multiplicative inverse, etc. The python lists or strings fail to support these features.
Conclusion
A NumPy matrix is a specialized 2D array created from a string or an array-like object. It is immensely helpful in scientific and mathematical computing. However, we should remember that a matrix is a subclass within the ndarray class in numpy. As such all the functions in the matrix subclass can be performed using ndarray class. Hence, in order to be an efficient data scientist or machine learning engineer, one must be very comfortable with numpy ndarrays.
Recommended Articles
This is a guide to Matrix in NumPy. Here we discuss the basic concept, matrix function, advantages and how to Create a Matrix in NumPy along with examples and code implementation. You may also look at the following articles to learn more –