The world’s leading publication for data science, AI, and ML professionals.

6 Key differences between np.ndarray and np.matrix objects

These two are technically different even if they seem to be the same in appearance

Photo by 𝓴𝓘𝓡𝓚 𝕝𝔸𝕀 on Unsplash
Photo by 𝓴𝓘𝓡𝓚 𝕝𝔸𝕀 on Unsplash

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.

Prerequisites

I recommend you to read the following content written by me.

Creating an ndarray object

We can use np.array() function to create an ndarray object.

import Numpy as np
A = np.array([[1, 2], 
             [3, 4]])
print(A)
print()
print(type(A))
(Image by author)
(Image by author)

Creating a matrix object

We can use the np.matrix() function to create a matrix object.

import numpy as np
B = np.matrix([[1, 2], 
              [3, 4]])
print(B)
print()
print(type(B))
(Image by author)
(Image by author)

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.

Difference 1: matrix objects are strictly 2-dimensional while ndarray objects can be multi-dimensional

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 np
A = np.array([1, 2, 3])
print(A)
print('nDimensions:', A.shape)
print('No. of Dimensions:', A.ndim)
(Image by author)
(Image by author)

2-dimensional array

import numpy as np
B = np.array([[1, 2], 
             [3, 4]])
print(B)
print('nDimensions:', B.shape)
print('No. of Dimensions:', B.ndim)
(Image by author)
(Image by author)

3-dimensional array

import numpy as np
C = 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)
(Image by author)
(Image by author)

Matrix objects are strictly 2-dimensional. If we try to create a 1d matrix, it automatically creates a 2d matrix.

import numpy as np
A = np.matrix([1, 2, 3])
print(A)
print()
print(type(A))
print('Dimensions:', A.shape)
print('No. of Dimensions:', A.ndim)
(Image by author)
(Image by author)

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]]])
(Image by author)
(Image by author)

Difference 2: ndarray and matrix objects behave differently with the * (single star) operator

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)
(Image by author)
(Image by author)

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)
(Image by author)
(Image by author)

Difference 3: ndarray and matrix objects behave differently with the ** (double stars) operator

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)
(Image by author)
(Image by author)

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)
(Image by author)
(Image by author)

Difference 4: matrix class is a subclass of ndarray class

Matrix objects inherit all the attributes and methods of ndarray objects.

Difference 5: matrix objects have .I for inverse, but ndarray objects don’t

a = np.matrix([[1, 2], 
              [3, 4]])
print(a)
print('nInverse')
print(a.I)
(Image by author)
(Image by author)
b = np.array([[1, 2], 
             [3, 4]])
print(b)
print('nInverse')
print(b.I)
(Image by author)
(Image by author)

To get the inverse of an ndarray object, use np.linalg.inv() function.

np.linalg.inv(b)
(Image by author)
(Image by author)

Difference 6: usage – ndarray class is commonly used rather than matrix class

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.

Summary

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))
(Image by author)
(Image by author)

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))
(Image by author)
(Image by author)

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

2021–05–08


Related Articles