The Wayback Machine - https://web.archive.org/web/20240621185405/https://www.geeksforgeeks.org/difference-between-numpy-array-and-numpy-matrix/
Open In App

Difference between Numpy array and Numpy matrix

Last Updated : 21 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python | Numpy numpy.matrix.any()
With the help of Numpy numpy.matrix.any() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison if any of the element matches it return true. Syntax : numpy.matrix.any() Return : Return true if any match found else false Example #1 : In this example we can see
1 min read
Python | Numpy numpy.matrix.all()
With the help of Numpy numpy.matrix.all() method, we are able to compare each and every element of one matrix with another or we can provide the axis on the we want to apply comparison. Syntax : numpy.matrix.all() Return : Return true if found match else false Example #1 : In this example we can see that with the help of matrix.all() method, we are
1 min read
Python | Numpy numpy.matrix.A()
With the help of Numpy numpy.matrix.A() method, we can get the same matrix as self. It means through this method we can get the identical matrix. Syntax : numpy.matrix.A() Return : Return self matrix Example #1 : In this example we can see that with the help of matrix.A() method, we are able to get the self matrix. # import the important module in
1 min read
Python | Numpy numpy.matrix.T()
With the help of Numpy numpy.matrix.T() method, we can make a Transpose of any matrix either having dimension one or more than more. Syntax : numpy.matrix.T() Return : Return transpose of every matrix Example #1 : In this example we can see that with the help of matrix.T() method, we are able to transform any type of matrix. # import the important
1 min read
Python | Numpy numpy.matrix.H()
With the help of Numpy numpy.matrix.H() method, we can make a conjugate Transpose of any complex matrix either having dimension one or more than more. Syntax : numpy.matrix.H() Return : Return conjugate transpose of every complex matrix Example #1 : In this example we can see that with the help of matrix.H() method, we are able to transform any typ
1 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
Calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis
Let's see how to calculate the difference between the maximum and the minimum values of a given NumPy array along the second axis. Here, the Second axis means row-wise. So firstly for finding the row-wise maximum and minimum elements in a NumPy array we are using numpy.amax() and numpy.amin() functions of NumPy library respectively. then After that
2 min read
Difference between numpy.array shape (R, 1) and (R,)
Difference between numpy.array shape (R, 1) and (R,). In this article, we are going to see the difference between NumPy Array Shape (R,1) and (R,). Prerequisite: NumPy Array Shape What is (R, ) ?(R, ) is a shape tuple of the 1-D array in Python. This shape tuple shows the number of columns in the 1-D array, i.e., the number of elements in that arra
4 min read
NumPy ndarray.tolist() Method | Convert NumPy Array to List
The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars. Data items are converted to the nearest compatible built-in Python type. Example C/C++ Code import numpy as np gfg = np.array([1, 2, 3, 4, 5]) print(gfg.tolist()) Output[1, 2, 3, 4, 5] SyntaxSynt
1 min read
NumPy ndarray.__abs__() | Find Absolute Value of Elements in NumPy Array
The ndarray.__abs__() method returns the absolute value of every element in the NumPy array. It is automatically invoked when we use Python's built-in method abs() on a NumPy array. Example C/C++ Code import numpy as np gfg = np.array([1.45, 2.32, 3.98, 4.41, 5.55, 6.12]) print(gfg.__abs__()) Output[ 1 2 3 4 5 6] SyntaxSyntax: ndarray.__abs__() Ret
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg