Difference between Numpy array and Numpy matrix
Last Updated :
21 Nov, 2022
While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same.
What is np.array() in Python
The Numpy array object in Numpy is called ndarray. We can create ndarray using numpy.array() function. It is used to convert a list, tuple, etc. into a Numpy array.
Syntax: numpy.array(object, dtype=None)
Creating a NumPy array using the .array() function.
Python3
# importing numpy module
import numpy as np
# creating numpy array
res = np.array(['G', 'F', 'G'])
print("Numpy Array in python :", res)
print(type(res))
Output:
Numpy Array in python : ['G' 'F' 'G']
<class 'numpy.ndarray'>
What is numpy.matrix() in Python
A matrix in Numpy returns a matrix from a string of data or array-like object. The matrix obtained is a specialized 2D array.
Syntax: numpy.matrix(object, dtype = None) :
Creating a NumPy matrix using the .matrix() function.
Python3
import numpy as np
mat = np.matrix([[1, 2, 3],
[5, 6, 7],
[4, 6, 8]])
print("Matrix is : \n", mat)
print(type(mat))
Output:
Matrix is :
[[1 2 3]
[5 6 7]
[4 6 8]]
<class 'numpy.matrix'>
Difference between Numpy array and Numpy Matrix
Matrix is 2-dimensional while ndarray can be multi-dimensional
Example 1:Â
Here, we can print all the dimensions of an array in np.array.
Python3
import numpy as np
arr1 = np.array([1, 2, 3])
print("1D array\n", arr1)
print("\n")
arr2 = np.array([[1, 2], [3, 4]])
print("2D array\n", arr2)
print("\n")
C = np.array([[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
[[9, 10], [11, 12]]])
print("3D array\n", C)
Output:
1D array
[1 2 3]
2D array
[[1 2]
[3 4]]
3D array
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
Example 2:Â
Matrix works normally for a 2D matrix and if a 1D matrix will convert into 2D Matrix, but if we pass a 3D matrix it will through an error.Â
Python3
import numpy as np
arr1 = np.matrix([1, 2, 3])
print(arr1)
print("Dimensions:", arr1.ndim)
print("\n")
arr2 = np.matrix([[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
[[9, 10], [11, 12]]])
print("2D array\n", arr2)
Output:
 Different functionality of  * operator in ndarray and Matrix
Example 1:
Array * operator does simple multiplication.
Python3
a = np.array([[1, 2],
[3, 4]])
b = np.array([[1, 2],
[3, 4]])
print("Array multiplication: \n", a*b)
Output:
Array multiplication:
[[ 1 4]
[ 9 16]]
Example 2:Â
While it does matrix multiplication.
Python3
a = np.matrix([[1, 2],
[3, 4]])
b = np.matrix([[1, 2],
[3, 4]])
print("Matrix multiplication: \n", a*b)
Output:
Matrix multiplication:
[[ 7 10]
[15 22]]
Matrix has an array.I for inverse, but ndarray has linalg.inv
Example 1:Â
The inverse can be done with array.I in ndarray.
Python3
arr1 = np.matrix([[1, 2],
[3, 4]])
print('Inverse \n', arr1.I)
Output:
Inverse
[[-2. 1. ]
[ 1.5 -0.5]]
Example 2:
The inverse can be done with np.linalg.inv in matrix.
Python3
b = np.array([[1, 2],
[3, 4]])
print('Inverse \n', np.linalg.inv(b))
Output:
Inverse
[[-2. 1. ]
[ 1.5 -0.5]]
Table of Differences between the Numpy Array and Numpy Matrix
Numpy np.array() | Numpy np.matrix()Â |
---|
Syntax: Â numpy.array(object, dtype=None)
| Syntax: numpy.matrix(object, dtype=None)
|
Numpy arrays (nd-arrays) are N-dimensional where, N=1,2,3...
| Numpy matrices are strictly 2-dimensional.
|
nd-arrays are base classes for matrix objects
| Matrix objects are a subclass of nd-array.
|
Matrix objects have arr.I for the inverse.
| Array objects don’t.
|
If a and b are matrices, then a@b or np.dot(a,b) is their matrix product
| If a and b are matrices, then a*b is their matrix product.
|
Similar Reads
Difference between np.asarray() and np.array()? NumPy is a Python library used for dealing with arrays. In Python, we use the list inplace of the array but itâs slow to process. NumPy array is a powerful N-dimensional array object and is used in linear algebra, Fourier transform, and random number capabilities. It provides an array object much fa
3 min read
Difference between NumPy.dot() and '*' operation in Python In Python if we have two numpy arrays which are often referred as a vector. The '*' operator and numpy.dot() work differently on them. It's important to know especially when you are dealing with data science or competitive programming problem. Working of '*' operator '*' operation caries out element
2 min read
Different Ways to Create Numpy Arrays in Python Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create
3 min read
How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem
2 min read
NumPy Array Functions NumPy array functions are a set of built-in operations provided by the NumPy library that allow users to perform various tasks on arrays. With NumPy array functions, you can create, reshape, slice, sort, perform mathematical operations, and much moreâall while taking advantage of the library's speed
3 min read