
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Array Itemsize
NumPy Array Itemsize
The itemsize attribute in a NumPy array indicates the size, in bytes, of each element in the array. This size is determined by the data type of the array elements (e.g., integer, float).
By knowing the itemsize, you can estimate the total memory consumption of the array. This is important for understanding the memory layout and storage requirements of arrays, especially when dealing with large datasets.
Accessing Array Itemsize
You can access the itemsize of a NumPy array using the itemsize attribute. This attribute returns an integer representing the size (in bytes) of each element in the array.
Example
In the following example, we are accessing the itemsize of an integer array and a float array −
import numpy as np # Creating arrays with different data types array_int32 = np.array([1, 2, 3], dtype=np.int32) array_float64 = np.array([1.0, 2.0, 3.0], dtype=np.float64) # Checking itemsize print("Itemsize of int32 array:", array_int32.itemsize) print("Itemsize of float64 array:", array_float64.itemsize)
Following is the output obtained −
Itemsize of int32 array: 4 Itemsize of float64 array: 8
Calculating Itemsize Memory Usage
To calculate the total memory occupied by the array, you can multiply the "itemsize" by the total number of elements in the array.
For example, if an array has "1000" elements and an itemsize of "8" bytes, the total memory used by the array would be "1000 * 8" = "8000" bytes.
Example
In this example, we are calculating the total memory usage of a 2D array −
import numpy as np # Create a 2D array array_2d = np.array([[1, 2], [3, 4]], dtype=np.float32) # Calculate total memory usage total_memory_usage = array_2d.size * array_2d.itemsize print(f"Total memory usage: {total_memory_usage} bytes")
This will produce the following result −
Total memory usage: 16 bytes
Itemsize with Different Data Types
In NumPy, different data types have different itemsize values. For example −
- np.int8 has an itemsize of 1 byte.
- np.int16 has an itemsize of 2 bytes.
- np.float64 has an itemsize of 8 bytes.
- np.complex128 has an itemsize of 16 bytes.
Example
In the example below, we are creating arrays with different data types and then checking itemsize of each array −
import numpy as np # Creating arrays with different data types array_int8 = np.array([1, 2, 3], dtype=np.int8) array_int16 = np.array([1, 2, 3], dtype=np.int16) array_uint32 = np.array([1, 2, 3], dtype=np.uint32) array_float16 = np.array([1.0, 2.0, 3.0], dtype=np.float16) array_complex128 = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128) # Checking itemsize print("Itemsize of int8 array:", array_int8.itemsize) print("Itemsize of int16 array:", array_int16.itemsize) print("Itemsize of uint32 array:", array_uint32.itemsize) print("Itemsize of float16 array:", array_float16.itemsize) print("Itemsize of complex128 array:", array_complex128.itemsize)
The output obtained is as shown below −
Itemsize of int8 array: 1 Itemsize of int16 array: 2 Itemsize of uint32 array: 4 Itemsize of float16 array: 2 Itemsize of complex128 array: 16
Changing Itemsize by Modifying Data Types
By modifying the data type of the array, you can change its itemsize, which affects how much memory each element occupies and, consequently, the total memory usage of the array.
You can change the data type of a NumPy array using the astype() function. This function accepts the target data type to which you want to convert the array.
Example
In the following example, we are changing the data type of an array from "int32" to "int8" and checking the itemsize of that array −
import numpy as np # Original array with int32 array_original = np.array([1, 2, 3], dtype=np.int32) print(f"Original itemsize: {array_original.itemsize} bytes") # Change data type to int8 array_new = array_original.astype(np.int8) print(f"New itemsize: {array_new.itemsize} bytes")
After executing the above code, we get the following output −
Original itemsize: 4 bytes New itemsize: 1 bytes