
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Size of Numpy Array
Numpy is often used along with packages such as SciPy and matplotlib in python. Additionally, arrays in Numpy are faster compared to lists in Python due to which this module is extensively used for complex mathematical algorithms and problems. It has various functions, and methods, to make our task easy and simple for matrix operations.
In Numpy arrays there are two types of array ?
Single Dimensional Array
Multi-Dimensional Array
Single Dimensional Array (1D array)
The datatypes of the elements stored in the array are - strings, integers, boolean or floating points. These arrays are called single or 1 Dimensional arrays. The shape of the so-defined array will be linear and it is more often represented as a 1D array.
Multi Dimensional Array
These arrays can store elements of any datatype - strings, integers, array, boolean etc. They have arrays inside arrays which are also called nested arrays and have a non-linear structure.
Usually, an array is measured in two types ?
Size of the array
Memory size of the array
The array size refers to the length of an array or the total number of independent data elements present in that particular array.
Whereas, memory size of an array refers to the space (usually in bytes for smaller arrays) that the array has occupied in one's system hardware to store data present in that particular array.
Method 1: Using Itemsize() Function
The itemsize() function returns the memory space occupied by each element in the array. The value returned is considered to be in bytes.
Syntax
<array_object_name>.itemsize
Returns integer value which is considered as no. of bytes.
Example 1
In the following example, we will compute memory occupied for each element stored in 1 Dimensional numpy array using the itemsize() function. Along with the size, length and total memory space occupied, the array is also displayed.
Algorithm
Step 1 ? Define the array
Step 2 ? Declare an arra
Step 3 ? Print size
Step 4 ? Print itemsize
import numpy #creation of a numpy array arr = numpy.array([1,2,3,4,5]) #Displays length of the array print(f"Length of the array: {numpy.size(arr)}") #Displays memory size in bytes print(f"Space occupied by one element: {arr.itemsize} bytes") #Total memory space occupied print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes")
Output
Length of the array: 5 Space occupied by one element: 8 bytes Total memory occupied by the element: 40 bytes
Example 2
In the following example, we compute the same information for a multi-dimensional numpy array.
import numpy #creation of a numpy array arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]]) arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']]) #Displays length of the array print(f'Length of the 2D array: {numpy.size(arr)}') #Displays memory size in bytes print(f"Space occupied by one element: {arr.itemsize} bytes") #Total memory space occupied print(f"Total memory occupied by the element: {arr.itemsize*numpy.size(arr)} bytes") print() print(f"Length of the 3D array: {numpy.size(arr_2)}") print(f"Space occupied by one element: {arr_2.itemsize} bytes") print(f"Total memory occupied by the element: {arr_2.itemsize*numpy.size(arr_2)} bytes")
Output
Length of 2D array: 2 Space occupied by one element: 8 bytes Total memory occupied by the element: 16 bytes Length of 3D array: 2 Space occupied by one element: 8 bytes Total memory occupied by the element: 16 bytes
Method 2: Using Nbytes() Function
nbytes() is a method in the numpy module in python. This function returns the total space (in bytes) occupied by the numpy array, unlike the itemsize function which returns the memory space occupied by only one element.
Syntax
<array_object_name>.nbytes
Returns integer value which is considered as total byte space occupied by the array.
Example 1
In this example, a 1D numpy array is created to explain the memory size occupied, using the nbytes function. The length of the array is also displayed alongwith.
import numpy arr = numpy.array([1,2,3,4,5]) #Displays length of the array print(f"Length of the array: {numpy.size(arr)}") #Total memory space occupied print(f"Total memory occupied by the element: {arr.nbytes} bytes")
Output
Length of the array: 5 Total memory occupied by the element: 40 bytes
Example 2
In the following example, we compute the length and memory size of multi-dimensional arrays with the same data values used in the above example.
import numpy arr = numpy.array([['a','b','c','d'], [1,2,3,4,5]]) arr_2 = numpy.array([ [[1,2,3], 'Tutorials Point'], ['a', 'e', 'i', 'o', 'u']]) #Displays length of the array print(f'Length of the 2D array: {numpy.size(arr)}') #Total memory space occupied print(f"Total memory occupied by the element: {arr.nbytes} bytes") print() print(f"Length of the 3D array: {numpy.size(arr_2)}") print(f"Total memory occupied by the element: {arr_2.nbytes} bytes")
Output
Length of 2D array: 2 Total memory occupied by the element: 16 bytes Length of 3D array: 2 Total memory occupied by the element: 16 bytes
Conclusion
Numpy arrays consume less memory compared to other data structures which is why they're used in handling large datasets and perform complex mathematical calculations. The itemsize function would compute the size of each element whereas the nbytes function computes the memory space occupied by the entire array.
They're used in various fields such as machine learning, data analyst and computational physics, transformation of data into various forms and many more. It is also used in various numerical and theoretical calculations of the data.