NUMPY BASIC EXERCISE 16


Write a solution in numpy which contains an identity matrix of 10×10

In this exercise, we are going to print 10×10 identity matrix.

Note: np.identity(n, dtype=None). Return the identity array.
The identity array is a square array with ones on the main diagonal.

Code

import numpy as np
arr = np.identity(10)
print("Creating an identity matrix")
print(arr)

Output

Creating an identity matrix
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]

Code Editor

import numpy as np arr = np.identity(10) print("Creating an identity matrix") print(arr)