Array Properties in NumPy Framework

NumPy arrays have several important properties that provide useful information about their structure and content. These properties help in understanding and manipulating arrays effectively.

1. Checking the Shape of an Array: shape

The shape property returns a tuple representing the dimensions of the array.

            import numpy as np
            
            arr = np.array([[1, 2, 3], [4, 5, 6]])
            print("Shape of array:", arr.shape)
        

Output:

            Shape of array: (2, 3)
        

2. Checking the Number of Dimensions: ndim

The ndim property returns the number of dimensions (axes) of the array.

            print("Number of dimensions:", arr.ndim)
        

Output:

            Number of dimensions: 2
        

3. Checking the Data Type: dtype

The dtype property shows the data type of elements stored in the array.

            print("Data type of array:", arr.dtype)
        

Output:

            Data type of array: int64 (varies based on system)
        

4. Checking the Size of an Array: size

The size property returns the total number of elements in the array.

            print("Total number of elements:", arr.size)
        

Output:

            Total number of elements: 6
        

5. Checking the Memory Size of Each Element: itemsize

The itemsize property returns the memory size (in bytes) of each element in the array.

            print("Size of each element in bytes:", arr.itemsize)
        

Output:

            Size of each element in bytes: 8 (varies based on data type and system)
        

6. Checking the Total Memory Consumption: nbytes

The nbytes property returns the total memory used by the array in bytes.

            print("Total memory used by array:", arr.nbytes)
        

Output:

            Total memory used by array: 48 (varies based on system)
        

Conclusion

NumPy provides various properties to inspect arrays, including shape, dimensions, data type, size, and memory usage. These properties are essential for efficient numerical computing.





Advertisement