0
0
NumPydata~20 mins

np.eye() for identity matrices in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Identity Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.eye() with default parameters
What is the output of this code snippet?
import numpy as np
print(np.eye(3))
NumPy
import numpy as np
print(np.eye(3))
A
[[0. 1. 0.]
 [1. 0. 1.]
 [0. 1. 0.]]
B
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
C
[[1 0 0]
 [0 1 0]
 [0 0 1]]
D
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
Attempts:
2 left
💡 Hint
np.eye(n) creates an n x n matrix with 1s on the main diagonal and 0s elsewhere.
Predict Output
intermediate
2:00remaining
Effect of k parameter in np.eye()
What is the output of this code?
import numpy as np
print(np.eye(4, k=1))
NumPy
import numpy as np
print(np.eye(4, k=1))
A
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]
B
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
C
[[0. 0. 0. 0.]
 [1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
D
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [1. 0. 0. 0.]]
Attempts:
2 left
💡 Hint
The k parameter shifts the diagonal by k positions above (positive) or below (negative) the main diagonal.
data_output
advanced
2:00remaining
Shape of np.eye() with different rows and columns
What is the shape of the array created by this code?
import numpy as np
arr = np.eye(3, 5)
print(arr.shape)
NumPy
import numpy as np
arr = np.eye(3, 5)
print(arr.shape)
A(5, 3)
B(5, 5)
C(3, 5)
D(3, 3)
Attempts:
2 left
💡 Hint
The first argument is the number of rows, the second is the number of columns.
visualization
advanced
2:00remaining
Visual pattern of np.eye() with negative k
Which option shows the correct printed output of this code?
import numpy as np
print(np.eye(4, k=-1))
NumPy
import numpy as np
print(np.eye(4, k=-1))
A
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
B
[[0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [0. 0. 0. 0.]]
C
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [1. 0. 0. 0.]]
D
[[0. 0. 0. 0.]
 [1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]]
Attempts:
2 left
💡 Hint
Negative k shifts the diagonal below the main diagonal.
🧠 Conceptual
expert
2:00remaining
Understanding dtype effect in np.eye()
What will be the dtype and output of this code?
import numpy as np
arr = np.eye(2, dtype=int)
print(arr)
print(arr.dtype)
NumPy
import numpy as np
arr = np.eye(2, dtype=int)
print(arr)
print(arr.dtype)
A
[[1 0]
 [0 1]]
int64
B
[[1 0]
 [0 1]]
float64
C
[[1. 0.]
 [0. 1.]]
float64
D
[[1. 0.]
 [0. 1.]]
int32
Attempts:
2 left
💡 Hint
dtype=int forces the matrix elements to be integers instead of floats.