w3resource logo
Python Exercises

Python NumPy: Create a 3-D array with ones on the diagonal and zeros elsewhere

Python NumPy: Array Object Exercise-42 with Solution

Write a Python program to create a 3-D array with ones on the diagonal and zeros elsewhere.

Sample Solution:-

Python Code:

import numpy as np
x = np.eye(3)
print(x)

Sample Output:

[[ 1.  0.  0.]                                                         
 [ 0.  1.  0.]                                                         
 [ 0.  0.  1.]]

Python Code Editor:

import numpy as np
x = np.eye(3)
print(x)

Improve this sample solution and post your code through Disqus