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

NumPy: Identity Array

Python library

An identity array is a square two D array where i) All the main diagonal elements from top‒left to bottom‒right are 1. ii) All other elements are 0.

Identity Array

• An identity array is a square two D array where

i) All the main diagonal elements from top‒left to bottom‒right are 1.

ii) All other elements are 0.

For example ‒


NumPy provides a built‒in function

numpy.identity(n,dtype =)

where

n id number of rows and columns to create n*n array

dtype is an optional parameter and it represents the data type of the elements such as int, float and so on.

 

Python program

import numpy as np

I = np.identity(3)

print(I)

Output

[[1. 0. 0.]

[0. 1. 0.]

[0. 0. 1.]]

NumPy automatically uses floating‒point values (1.0, 0.0).

For creating integer identity array

I = np.identity(4,dtype = int)

Will give us

 [[1 0 0 0]

 [0 1 0 0]

 [0 0 1 0]

 [0 0 0 1]]

 

Python for Data Science: Chapter 5: NumPy and Pandas Libraries : Tag: Computer Programming, Python, Data Science : Python library - NumPy: Identity Array


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