
- 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 Size
NumPy Array Size
The size of a NumPy array refers to the total number of elements contained within the array. Understanding the size of an array is important for performing various operations, such as reshaping, broadcasting, and iterating through elements.
In this tutorial, we will discuss how to determine and manipulate the size of NumPy arrays.
Checking Array Size
NumPy provides the size attribute to check the number of elements in an array. This attribute returns an integer representing the total number of elements, regardless of its shape. Whether the array is one-dimensional or multi-dimensional, size will always provide the count of all elements present.
Knowing the size of an array can be useful in various scenarios, such as iterating over elements, reshaping arrays, and optimizing memory usage.
Example: Iterating Over Elements
When you need to iterate through each element in an array, knowing the size helps you define the range for your loop −
import numpy as np # Creating a 1D array array = np.array([1, 2, 3, 4, 5]) # Iterating using the size attribute for i in range(array.size): print(array[i])
Following is the output obtained −
1 2 3 4 5
Example: Memory Calculation
You can calculate the memory consumption of an array by multiplying its size by the size of each element using the "itemsize" attribute in NumPy −
import numpy as np array = np.ones((1000, 1000), dtype=np.float64) memory_usage = array.size * array.itemsize print("Memory usage in bytes:", memory_usage)
This will produce the following result −
Memory usage in bytes: 8000000
NumPy Array Size vs. Array Shape
While the size attribute gives the total number of elements in an array, the shape attribute provides the dimensions of the array. The size of an array can be calculated from the shape by multiplying all the dimensions.
Example
In this example, we are retrieving the shape and size of an array using the NumPy "shape" and "size" attribute respectively −
import numpy as np # Creating a 3D array array = np.zeros((2, 3, 4)) # Checking the shape and size print("Array shape:", array.shape) print("Array size:", array.size)
The shape of the array is (2, 3, 4), and the size is 2 * 3 * 4 = 24 −
Array shape: (2, 3, 4) Array size: 24
Resizing Arrays
The resize() function in NumPy is used to resize an array. This function changes the shape and size of the array in-place, and the new shape must be compatible with the total number of elements.
If the new shape is larger than the original, the new array is filled with repeated copies of the original array. If the new shape is smaller, the original array is truncated.
Unlike the reshape() function, which only changes the shape of an array without changing its data, the resize() function can change the size of the array as well, adding new elements or removing existing ones.
Example: Resizing to a Larger Array
In this example, the original array is resized to a "2x5" array. The new elements are filled by repeating the original array −
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3]) # Resizing the array to a larger size resized_array = np.resize(array_1d, (2, 5)) print("Original array:\n", array_1d) print("Resized array:\n", resized_array)
After executing the above code, we get the following output −
Original array: [1 2 3] Resized array: [[1 2 3 1 2] [3 1 2 3 1]]
Example: Resizing to a Smaller Array
Here, the original "2x3" array is resized to a "2x2" array. The excess elements are truncated −
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Resizing the array to a smaller size resized_array = np.resize(array_2d, (2, 2)) print("Original array:\n", array_2d) print("Resized array:\n", resized_array)
The result produced is as follows −
Original array: [[1 2 3] [4 5 6]] Resized array: [[1 2] [3 4]]
Example: In-Place Resizing
In this example, the original array is resized in place to a "2x5" array. The new elements are filled with zeros −
import numpy as np # Creating a 1D array array_1d = np.array([1, 2, 3]) # In-place resizing the array to a larger size array_1d.resize((2, 5)) print("Resized array:\n", array_1d)
We get the output as shown below −
Resized array: [[1 2 3 0 0] [0 0 0 0 0]]