w3resource
NumPy Exercises

NumPy: Create a 3x3 identity matrix

NumPy: Basic Exercise-16 with Solution

Write a NumPy program to create a 3x3 identity matrix.

Sample Solution :

Python Code :

import numpy as np
array_2D=np.identity(3)
print('3x3 matrix:')
print(array_2D)

Sample Output:

3x3 matrix:
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]                         

Pictorial Presentation:

NumPy: Create a 3x3 identity matrix.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to create an array of all the even integers from 30 to 70.
Next: Write a NumPy program to generate a random number between 0 and 1.

What is the difficulty level of this exercise?