Length of Numpy Array
One of the fundamental attributes of an array is its length, which represents the number of elements it contains. In this article, we will explore various ways to determine the length of a numpy array.
Numpy is a popular Python library used for scientific computing. It provides a high-performance multidimensional array object, called ndarray
, which enables efficient storage and manipulation of arrays.
Using the len()
determine the length of Numpy array
In Python, the len()
function is commonly used to determine the length of an object, including numpy arrays. The len()
function returns the number of elements in the outermost dimension of the array. Let’s consider the following examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5])
print(len(arr1)) # Output: 5
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(len(arr2)) # Output: 2
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(len(arr3)) # Output: 2
Output:
As shown in the examples, the len()
function correctly determines the number of elements in the outermost dimension of the arrays.
Using the ndarray.shape
attribute determine the length of Numpy array
The ndarray
object in numpy has an attribute called shape
, which returns a tuple representing the dimensions of the array. The length of this tuple can be used to determine the number of dimensions in the array. To find the length of the array, we can simply take the first element of the shape tuple. Consider the following examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr1.shape[0]) # Output: 9
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(arr2.shape[0]) # Output: 2
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3.shape[0]) # Output: 2
Output:
In each example, we access the first element of the shape
tuple using the index [0]
, which corresponds to the length of the array.
Using the ndarray.size
attribute determine the length of Numpy array
The size
attribute of the ndarray
object returns the total number of elements in the array. Therefore, it can be used to determine the length of the array as well. Let’s see some examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1.size) # Output: 5
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(arr2.size) # Output: 4
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3.size) # Output: 8
Output:
As shown in the examples, the size
attribute provides the total number of elements in the arrays, which corresponds to their length.
Using the ndarray.ndim
attribute determine the length of Numpy array
Another attribute of the ndarray
object, ndim
, returns the number of dimensions of the array. The length of the array can be determined by checking the value of this attribute. Consider the following examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1.ndim) # Output: 1
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(arr2.ndim) # Output: 2
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3.ndim) # Output: 3
Output:
In the examples above, the ndim
attribute returns the number of dimensions in each array, which is equal to their length.
Using the ndarray.__len__()
determine the length of Numpy array
The ndarray
object in numpy also provides a method called len()
, which returns the length of the array. This method can be used to determine the number of elements in the first dimension of the array. Let’s look at some examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1.__len__()) # Output: 5
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(arr2.__len__()) # Output: 2
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr3.__len__()) # Output: 2
Output:
As demonstrated in the examples, the len()
method provides the length of the arrays, which corresponds to the number of elements in their first dimension.
Using the numpy.size()
function determine the length of Numpy array
The Numpy library also offers a function called numpy.size()
that can be used to determine the total number of elements in an array. This function is similar to the ndarray.size
attribute, discussed earlier, but it can also be applied to other Python iterables. Let’s examine some examples:
import numpy as np
# Example 1
arr1 = np.array([1, 2, 3, 4, 5])
print(np.size(arr1)) # Output: 5
# Example 2
arr2 = np.array([[1, 2], [3, 4]])
print(np.size(arr2)) # Output: 4
# Example 3
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(np.size(arr3)) # Output: 8
Output:
The numpy.size()
function correctly provides the number of elements in the arrays, which corresponds to their length.