6 Key differences between np.ndarray and np.matrix objects | by Rukshan Pramoditha | Towards Data Science

Numpy is the foundational Python library that is widely used for numerical calculations and linear algebra. ndarray and matrix objects are commonly used numpy objects. ndarray objects are created from the numpy ndarray class. matrix objects are created from the numpy matrix class. If you’re new to numpy, you may get confused with numpy ndarray and numpy matrix objects. They are two different things if they seem to be the same in appearance. Today, we’ll discuss 6 such differences between them.
I recommend you to read the following content written by me.
NumPy for Data Science: Part 1 — NumPy Basics and Array Creation
We can use np.array() function to create an ndarray object.
import numpy as npA = np.array([[1, 2],              [3, 4]])print(A)print()print(type(A))
We can use the np.matrix() function to create a matrix object.
import numpy as npB = np.matrix([[1, 2],               [3, 4]])print(B)print()print(type(B))
Even if both ndarray and matrix objects are the same in appearance, they belong to two different classes have different functionalities. Let’s discuss them now.
We can create 1d, 2d, 3d, even 4d, 5d (but they’re hard to imagine) ndarray objects, but we can only create 2d matrix objects.
1-dimensional array
import numpy as npA = np.array([1, 2, 3])print(A)print('\nDimensions:', A.shape)print('No. of Dimensions:', A.ndim)
2-dimensional array
import numpy as npB = np.array([[1, 2],              [3, 4]])print(B)print('\nDimensions:', B.shape)print('No. of Dimensions:', B.ndim)
3-dimensional array
import numpy as npC = np.array([[[1, 2], [3, 4]],             [[5, 6], [7, 8]],             [[9, 10], [11, 12]]])print(C)print('\nDimensions:', C.shape)print('No. of Dimensions:', C.ndim)
Matrix objects are strictly 2-dimensional. If we try to create a 1d matrix, it automatically creates a 2d matrix.
import numpy as npA = np.matrix([1, 2, 3])print(A)print()print(type(A))print('Dimensions:', A.shape)print('No. of Dimensions:', A.ndim)
If we try to create a 3d matrix, it gives an error.
np.matrix([[[1, 2], [3, 4]],           [[5, 6], [7, 8]],           [[9, 10], [11, 12]]])
When we multiply two ndarray objects using the * operator, the result is the element-by-element multiplication.
a = np.array([[1, 2],              [3, 4]])b = np.array([[5, 6],              [8, 9]])print("a", type(a))print(a)print("\nb", type(b))print(b)print("\n* operation on two ndarray objects (Elementwise)")print(a * b)
When we multiply two matrix objects using the * operator, the result is the dot (matrix) product.
c = np.matrix([[1, 2],               [3, 4]])d = np.matrix([[5, 6],               [8, 9]])print("c", type(c))print(c)print("\nd", type(d))print(d)print("\n* operation on two matrix objects")print(c * d)
When we use the ** operator on two ndarray objects, the result is the elementwise squared values of each element.
a = np.array([[1, 2],              [3, 4]])print("a", type(a))print(a)print("\n** operation on two ndarray objects (Elementwise)")print(a ** 2)
When we use the ** operator on two matrix objects, the result is a matrix multiplication!
b = np.matrix([[1, 2],               [3, 4]])print(b)print("\n** operation on two matrix objects")print(b ** 2)
Matrix objects inherit all the attributes and methods of ndarray objects.
a = np.matrix([[1, 2],               [3, 4]])print(a)print('\nInverse')print(a.I)
b = np.array([[1, 2],              [3, 4]])print(b)print('\nInverse')print(b.I)
To get the inverse of an ndarray object, use np.linalg.inv() function.
np.linalg.inv(b)
Here is what numpy documentation says about the usage of these two classes.
It is no longer recommended to use the matrix class, even for linear algebra. Instead, use regular numpy arrays. The matrix class may be removed in the future.
ndarray and matrix classes behave differently. It may be much easier to use the matrix class because it is like using matrices in linear algebra. If you need to work on multi-dimensional arrays, you should use the ndarray objects as they are multi-dimensional. Numpy documentation recommends you using ndarray objects instead of matrix objects. You can always convert one class type into another by using an appropriate function. Therefore, use the np.asmatrix() function to convert an ndarray object into a matrix object.
a = np.array([[1, 2],              [3, 4]])print(a)print(type(a))print()b = np.asmatrix(a)print(b)print(type(b))
Also, you can use the np.asarray() function to convert a matrix object into an ndarray object.
a = np.matrix([[1, 2], [3, 4]])print(a)print(type(a))print()b = np.asarray(a)print(b)print(type(b))
Thanks for reading!
This tutorial was designed and created by Rukshan Pramoditha, the Author of Data Science 365 Blog.
Read my other articles at https://rukshanpramoditha.medium.com