Here’s an easy way to find the length of a NumPy array.
Array length
In NumPy, you can use the size method to get the length of an array. The size method returns the total number of elements in the array.
For example, the following code creates an array with 12 elements:
import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"Length of my array equals: {my_array.size}")
The output of the size method is 12, which is the length of the array.
Array shape
Now you know the length, but this information might not be enough for you. Let’s see the shape of the array.
import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"Shape of my array: {my_array.shape}")
The shape of an array is 4 x 3, which is 12 in total. This is the output which gives you more information about the size of an array. You just need to remember that to get the length of an array, you need to multiply the output of the shape method.
Array size
Knowing the shape of the array is only the point of view. You might be interested in what the actual size of the array is from the storage point of view. This is how you get information about how many bytes your array consumes:
import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"The array is using {my_array.nbytes} bytes.")
Thanks to the nbytes method, you know that the array is using 48 bytes.
This is how you get the full picture of the size, length, shape, and number of bytes the array requires.
How to Get the Number of Bytes Used by a NumPy Array
In NumPy, you can use the nbytes
method to get the number of bytes used by an array. The nbytes
method returns the total number of bytes used to store the elements of the array.
For example, the following code creates an array with 12 elements:
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print(my_array.nbytes)
The output of the nbytes
method is 48, which is the number of bytes used to store the elements of the array.
The nbytes
method is useful for determining the memory requirements of a NumPy array. For example, if you are creating a large array, you can use the nbytes
method to make sure that you have enough memory available.
Here is a table that summarizes the different ways to get the length of a NumPy array:
Method | Description |
---|---|
size |
Returns the total number of elements in the array. |
shape |
Returns a tuple that contains the number of elements in each dimension. |
shape[0] * shape[1] |
Multiplies the number of elements in each dimension to get the length of the array. |
nbytes |
Returns the total number of bytes used to store the elements of the array. |
Key Takeaways
- To get the length of a NumPy array, you can use the `size` method.
- The `size` method returns the total number of elements in the array.
- You can also use the shape of the array to get the length.
- The shape of an array is a tuple that contains the number of elements in each dimension.
- You can get the length of an array by multiplying the number of elements in each dimension.
- The `nbytes` method returns the total number of bytes used to store the elements of the array.
FAQ
- Q: How do I get the length of a multidimensional NumPy array?
- A: You can use the `shape` method to get the shape of the array. The shape of a multidimensional array is a tuple that contains the number of elements in each dimension. You can then multiply the number of elements in each dimension to get the length of the array.
- Q: What is the difference between the `size` and `nbytes` methods?
- The `size` method returns the total number of elements in the array, while the `nbytes` method returns the total number of bytes used to store the elements of the array.