Python for Data Science: Chapter 5: NumPy and Pandas Libraries

NumPy: Attributes of Arrays

Python library

Question: Explain attributes of NumPy arrays with the help of Python programs. Index: 1) .ndim 2) .shape 3) .size 4) .dtype 5) .itemsize

Attributes of Arrays

• Every NumPy array object has some attributes that describe its structure.


1) .ndim:

It means number of dimensions. It tells how many dimensions (axes) an array has. In simple words, it shows whether the array is 1D, 2D or 3D

Python program

import numpy as np

arr1 = np.array([10,20,30])

arr2 = np.array([[1,2,3],[4,5,6]])

print("Dimension of arr1: ",arr1.ndim)

print("Dimension of arr2: ",arr2.ndim)

Output

Dimension of arr1: 1

Dimension of arr2: 2


2) .shape:

It gives the size of each dimension as a tuple. The first value indicates the number of rows and second value indicates the number of columns.

Python program

import numpy as np

arr = np.array([[10,20,30],[40,50,60]])

print(arr.shape)

Output

(2, 3)

Here the array is created as follows

10 20 30

40 50 60

That means there are two rows and three columns.


3) .size:

The size attribute tells how many elements are present in the entire array. This attribute is useful when checking the total number of values stored ‒ especially for large dataset.

Python program

import numpy as np

ап = np.array([[10,20,30],[40,50,60]])

print(arr.size)

Output

6


4) .dtype:

This attribute tells what type of values are stored in the array. For instance ‒ integer, float, string, booleans etc)

Python program

import numpy as np

arr = np.array([1,2,3], dtype = float)

print(arr.dtype)

Output

float64

 

5) .itemsize:

It shows how much memory(in bytes) each element of array takes. For instance ‒ float64 takes 8 bytes per element, while int32 takes 4 bytes.

Python program

import numpy as np

arr1 = np.array([1,2,3], dtype=np.int32)

arr2= np.array([1.0,2.0,3.0], dtype=np.float64)

print(arr1.itemsize)

print(arr2.itemsize) do

Output

4

8

Note that, Each integer in arr1 uses 4 bytes of memory. Each floating‒point number in arr2 uses 8 bytes.


Review Question

1. Explain attributes of NumPy arrays with the help of Python programs.

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - NumPy: Attributes of Arrays


Python for Data Science: Chapter 5: NumPy and Pandas Libraries



Under Subject


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Python for Data Science - Laboratory

AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation