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 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-
4 min read
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 List and Array in Python In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.Operations Difference in Lists and ArraysAccessi
6 min read
Difference between NumPy and SciPy in Python There are two important packages in Python: NumPy and SciPy. In this article, we will delve into the key differences between NumPy and SciPy, their features, and their integration into the ecosystem. and also get to know which one is better. What is NumPy?NumPy also known as Numerical Python, is a f
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
Differences between Flatten() and Ravel() Numpy Functions We have two similar kinds of ways to convert a ndarray to a 1D array of Flatten() and Ravel() Numpy function in Python programming language. Example of Flatten() Numpy function Here, we will create a Numpy array, and then by using flatten function we have changed the element in the flattened 1D NumP
3 min read
Difference between Pandas VS NumPy Python is one of the most popular languages for Machine Learning, Data Analysis, and Deep learning tasks. It is powerful because of its libraries that provide the user full command over the data. Today, we will look into the most popular libraries i.e. NumPy and Pandas in Python, and then we will co
3 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
Flatten a Matrix in Python using NumPy Let's discuss how to flatten a Matrix using NumPy in Python. By using ndarray.flatten() function we can flatten a matrix to one dimension in python. Syntax:numpy_array.flatten(order='C') order:'C' means to flatten in row-major.'F' means to flatten in column-major.'A' means to flatten in column-major
1 min read
How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin
3 min read