It creates an identity matrix of size n x n. This matrix has 1s on the diagonal and 0s elsewhere.
Click to reveal answer
beginner
How can you create a 4x4 identity matrix using np.eye()?
Use np.eye(4). This returns a 4 by 4 matrix with 1s on the diagonal and 0s elsewhere.
Click to reveal answer
intermediate
What is the difference between np.eye(n) and np.identity(n)?
np.eye(n) can create rectangular matrices by specifying rows and columns, while np.identity(n) only creates square identity matrices of size n x n.
Click to reveal answer
intermediate
How do you create a 3x5 matrix with 1s on the diagonal using np.eye()?
Use np.eye(3, 5). This creates a matrix with 3 rows and 5 columns, with 1s on the diagonal and 0s elsewhere.
Click to reveal answer
intermediate
What parameter in np.eye() controls the diagonal offset?
The k parameter controls which diagonal to place the 1s on. k=0 is the main diagonal, k>0 is above, and k<0 is below the main diagonal.
Click to reveal answer
What does np.eye(3) produce?
AA 3x3 matrix of ones
BA 3x3 matrix of zeros
CA 3x3 identity matrix
DA 3x3 diagonal matrix with twos
✗ Incorrect
np.eye(3) creates a 3x3 identity matrix with 1s on the diagonal and 0s elsewhere.
How do you create a 2x4 matrix with 1s on the diagonal using np.eye()?
Anp.eye(2, 4)
Bnp.eye(4, 2)
Cnp.eye(2)
Dnp.eye(4)
✗ Incorrect
np.eye(2, 4) creates a matrix with 2 rows and 4 columns with 1s on the diagonal.
What does the parameter k in np.eye(n, m, k) do?
ASets the diagonal offset where 1s are placed
BSets the number of rows
CSets the number of columns
DSets the data type
✗ Incorrect
The k parameter shifts the diagonal where 1s appear: 0 is main diagonal, positive is above, negative is below.
Which function only creates square identity matrices?
Anp.zeros()
Bnp.identity()
Cnp.eye()
Dnp.ones()
✗ Incorrect
np.identity() creates only square identity matrices, while np.eye() can create rectangular ones.
What will np.eye(3, 3, k=1) produce?
AA 3x3 matrix with 1s below the main diagonal
BA 3x3 identity matrix
CA 3x3 matrix of zeros
DA 3x3 matrix with 1s on the diagonal above the main diagonal
✗ Incorrect
Setting k=1 places 1s on the diagonal just above the main diagonal.
Explain how to create an identity matrix and how to adjust its size and shape using np.eye().
Think about rows, columns, and diagonal offset.
You got /4 concepts.
Describe the difference between np.eye() and np.identity().
Focus on shape flexibility.
You got /3 concepts.
Practice
(1/5)
1. What does the function np.eye(4) create in NumPy?
easy
A. A 4x4 identity matrix with 1s on the main diagonal
B. A 4x4 matrix filled with zeros
C. A 4x4 matrix filled with ones
D. A 4x4 diagonal matrix with 1s on the first upper diagonal
Solution
Step 1: Understand np.eye() default behavior
By default, np.eye(n) creates an n x n identity matrix with 1s on the main diagonal and 0s elsewhere.
Step 2: Apply np.eye(4)
Calling np.eye(4) creates a 4x4 matrix with 1s on the main diagonal and 0s elsewhere.
Final Answer:
A 4x4 identity matrix with 1s on the main diagonal -> Option A
Quick Check:
np.eye(4) = 4x4 identity matrix [OK]
Hint: np.eye(n) makes nxn identity matrix by default [OK]
Common Mistakes:
Confusing np.eye() with np.ones() or np.zeros()
Thinking np.eye() fills all elements with 1
Assuming 1s are on an off-diagonal by default
2. Which of the following is the correct syntax to create a 3x5 identity matrix with 1s on the main diagonal using np.eye()?
easy
A. np.eye(5, 3)
B. np.eye(3, 5)
C. np.eye(3)
D. np.eye(5)
Solution
Step 1: Understand np.eye() parameters
The first parameter is the number of rows, the second is columns. So np.eye(3, 5) creates a 3x5 matrix.
Step 2: Check the options
np.eye(3, 5) correctly uses np.eye(3, 5) to create a 3x5 matrix with 1s on the main diagonal.
Final Answer:
np.eye(3, 5) -> Option B
Quick Check:
np.eye(rows, cols) = correct syntax [OK]
Hint: First arg = rows, second = columns in np.eye() [OK]
Common Mistakes:
Swapping rows and columns parameters
Using only one parameter for non-square matrix
Confusing np.eye() with np.identity()
3. What is the output of the following code?
import numpy as np
print(np.eye(3, k=1))
medium
A. [[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]]
B. [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
C. [[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
D. [[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
Solution
Step 1: Understand the k parameter in np.eye()
The parameter k=1 shifts the diagonal of 1s one above the main diagonal.
Step 2: Visualize the 3x3 matrix with 1s on the first upper diagonal
The 1s appear at positions (0,1) and (1,2), zeros elsewhere.
Final Answer:
[[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]] -> Option D
Quick Check:
k=1 shifts diagonal up by 1 [OK]
Hint: k shifts diagonal: 0=main, 1=above, -1=below [OK]
Common Mistakes:
Ignoring the k parameter effect
Assuming k=1 means main diagonal
Confusing rows and columns in output
4. The code below throws an error. What is the mistake?
import numpy as np
matrix = np.eye(4, 3, k=4)
print(matrix)
medium
A. np.eye() does not accept three arguments
B. The matrix must be square for np.eye()
C. The value of k is too large for the matrix shape
D. The print statement is incorrect
Solution
Step 1: Check the meaning of k relative to matrix size
The parameter k=4 means the diagonal of 1s is shifted 4 above the main diagonal.
Step 2: Compare k with matrix dimensions
For a 4x3 matrix, the maximum upper diagonal shift is 2 (columns - 1). k=4 is invalid and causes an error.
Final Answer:
The value of k is too large for the matrix shape -> Option C
Quick Check:
k must be within valid diagonal range [OK]
Hint: k must fit within matrix diagonal limits [OK]
Common Mistakes:
Assuming any k value works
Thinking np.eye() only creates square matrices
Blaming print instead of parameter error
5. You want to create a 5x5 matrix with 1s on the diagonal just below the main diagonal using np.eye(). Which code achieves this and what is the resulting matrix?
hard
A. np.eye(5, k=-1) resulting in 1s at positions (1,0), (2,1), (3,2), (4,3)
B. np.eye(5, k=1) resulting in 1s at positions (0,1), (1,2), (2,3), (3,4)
C. np.eye(5) resulting in 1s on the main diagonal
D. np.eye(5, k=-2) resulting in 1s two diagonals below the main
Solution
Step 1: Understand k parameter for diagonal shift
Negative k shifts the diagonal of 1s below the main diagonal. k=-1 means one diagonal below.
Step 2: Apply np.eye(5, k=-1)
This creates a 5x5 matrix with 1s at positions (1,0), (2,1), (3,2), (4,3) and zeros elsewhere.
Final Answer:
np.eye(5, k=-1) with 1s just below main diagonal -> Option A
Quick Check:
k=-1 shifts diagonal down by 1 [OK]
Hint: Use negative k for diagonals below main [OK]
Common Mistakes:
Using positive k for below main diagonal
Confusing positions of 1s in matrix
Assuming np.eye() only creates main diagonal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.