NumPy Array Attributes with Examples
This article provides a comprehensive explanation of the key attributes of NumPy arrays, such as shape, dtype, strides, flags, and data, with clear examples and explanations. Whether you're a beginner or an experienced NumPy user, this article will deepen your understanding of the inner workings of NumPy arrays and how to manipulate them.
NumPy is a popular Python library for numerical computation. It provides high-performance multidimensional arrays and tools for working with these arrays. A NumPy array is a grid of values, all of the same type, and is indexed by a tuple of non-negative integers. NumPy arrays are homogeneous - that is, all elements of the array must be of the same data type.
Here's an example of how to create a NumPy array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
#output : [1 2 3 4 5]
In this example, we create a NumPy array using the np.array() function. The array contains the integers 1 through 5.
What are the attributes of NumPy Array ?
In NumPy, arrays are objects with a set of attributes that describe their properties. These attributes provide information about the shape, size, data type, and other properties of the array.
ndim: The number of dimensions of the array.shape: A tuple representing the size of each dimension of the array.size: The total number of elements in the array.dtype: The data type of the elements in the array.itemsize: The size (in bytes) of each element in the array.nbytes: The total number of bytes used by the array.strides: A tuple representing the number of bytes to step in each dimension when traversing the array.flags: An object containing information about the memory layout of the array, such as whether it is contiguous or not.data: A buffer containing the actual elements of the array.
1. ndarray.ndim
This attribute represents the number of dimensions of the array. For example, a 1-dimensional array has an ndim of 1, a 2-dimensional array has an ndim of 2, and so on.
Example of ndim in NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.ndim) # Output: 1
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.ndim) # Output: 2
2. ndarray.shape
This attribute represents the size of each dimension of the array. For example, a 1-dimensional array of length 5 has a shape of (5,), while a 2-dimensional array with 3 rows and 2 columns has a shape of (3, 2).
Example of shape in NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.shape) # Output: (5,)
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.shape) # Output: (3, 2)
b=arr.reshape(2,3) # we can reshape the array dimensions
print(b.shape)
3. ndarray.size
This attribute represents the total number of elements in the array.
Example of size in NumPy
arr = np.array([1, 2, 3, 4, 5])
print(arr.size) # Output: 5
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.size) # Output: 6
4. ndarray.dtype
This attribute represents the data type of the elements in the array.
Example of dtype in NumPy
arr = np.array([1, 2, 3, 4, 5])
print(arr.dtype) # Output: int64
arr = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
print(arr.dtype) # Output: float64
5. ndarray.itemsize
This attribute represents the size (in bytes) of each element in the array.
Example of itemsize in NumPy
arr = np.array([1, 2, 3, 4, 5])
print(arr.itemsize) # Output: 8 (for int64 on a 64-bit system)
arr = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
print(arr.itemsize) # Output: 8 (for float64 on a 64-bit system)
6. ndarray.strides
The strides attribute of a NumPy array describes how many bytes to jump in memory to get to the next element along each dimension of the array. The strides tuple is usually of the same length as the shape tuple, and each element of the strides tuple represents the number of bytes to skip in memory to get to the next element along the corresponding dimension of the array.
For example, let's say we have a 2D array with shape (3, 4) and data type int32:
import numpy as np
arr = np.zeros((3, 4), dtype=np.int32)
print(arr.strides)
# Output: (16, 4)
The strides attribute of arr will be a tuple with two elements, representing the number of bytes to jump in memory to get to the next row and column, respectively:
The first element of the strides tuple, 16, represents the number of bytes to jump to get to the next row. Since the dtype of the array is int32, each element takes up 4 bytes of memory. Therefore, to jump to the next row, we need to skip 4 bytes and the number of columns in the array, which is 4. This gives us a stride of 16 bytes.
The second element of the strides tuple, 4, represents the number of bytes to jump to get to the next column. Since we have a contiguous array with a row-major memory layout, we only need to skip 4 bytes to get to the next column.
7. ndarray.nbytes
The nbytes attribute of a NumPy array returns the total number of bytes used by the array's data. It is calculated by multiplying the size of each element in the array by the total number of elements. The nbytes attribute is a useful tool for checking the memory usage of NumPy arrays and can help identify potential memory issues.
Example of nbytes attribute in NumPy
import numpy as np
arr1=np.array([1,2,3,4,5,6])
print(arr1)
print("Memory taken by array 1 in Bytes=",arr1.nbytes)
arr2=np.array([[2.5,3.5,4.5,5.5],[5,6,7,8]])
print(arr2)
print("Memory taken by array 2 in Bytes=",arr2.nbytes)
8. ndarray.data
The data attribute of a NumPy array returns a buffer object pointing to the start of the array's data. The buffer object is a memory view of the array's data and provides a low-level interface to access the data directly in memory. The data attribute is a read-only attribute and should not be modified directly.
Example of data attribute in NumPy
import numpy as np
arr1 = np.array(([1,2,3],[4,5,6]), dtype=np.int32)
print(arr1.data)
9.ndarray.flags
The flags attribute of a NumPy array returns a dictionary containing a variety of Boolean flags that describe various properties of the array. The flags attribute provides useful information about the array's memory layout, data type, and other attributes that can be used to optimize array operations.
Some of the key flags included in the flags attribute include:
C_CONTIGUOUS: True if the array is in C-contiguous order (row-major).F_CONTIGUOUS: True if the array is in Fortran-contiguous order (column-major).OWNDATA: True if the array owns its own data.WRITEABLE: True if the array is writeable.
Example of Flags attribute
import numpy as np
arr1 = np.array(([1,2,3],[4,5,6]), dtype=np.int32)
print(arr1.flags)