![]() |
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 PythonThe 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.
Creating a NumPy array using the .array() function. Python3
Output: Numpy Array in python : ['G' 'F' 'G'] <class 'numpy.ndarray'> What is numpy.matrix() in PythonA matrix in Numpy returns a matrix from a string of data or array-like object. The matrix obtained is a specialized 2D array.
Creating a NumPy matrix using the .matrix() function. Python3
Output: Matrix is : [[1 2 3] [5 6 7] [4 6 8]] <class 'numpy.matrix'> Difference between Numpy array and Numpy MatrixMatrix is 2-dimensional while ndarray can be multi-dimensionalExample 1: Here, we can print all the dimensions of an array in np.array. Python3
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
Output: ![]()
Different functionality of * operator in ndarray and MatrixExample 1: Array * operator does simple multiplication. Python3
Output: Array multiplication: [[ 1 4] [ 9 16]] Example 2: While it does matrix multiplication. Python3
Output: Matrix multiplication: [[ 7 10] [15 22]] Matrix has an array.I for inverse, but ndarray has linalg.invExample 1: The inverse can be done with array.I in ndarray. Python3
Output: Inverse [[-2. 1. ] [ 1.5 -0.5]] Example 2: The inverse can be done with np.linalg.inv in matrix. Python3
Output: Inverse [[-2. 1. ] [ 1.5 -0.5]] Table of Differences between the Numpy Array and Numpy Matrix
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 8 |