0
0
NumPydata~10 mins

np.eye() for identity matrices in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a 3x3 identity matrix using numpy.

NumPy
import numpy as np
matrix = np.[1](3)
print(matrix)
Drag options to blanks, or click blank then click option'
Aones
Beye
Czeros
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.ones() or np.zeros() instead of np.eye()
Forgetting to import numpy as np
2fill in blank
medium

Complete the code to create a 4x4 identity matrix with float type values.

NumPy
import numpy as np
matrix = np.eye(4, dtype=[1])
print(matrix)
Drag options to blanks, or click blank then click option'
Afloat
Bint
Cstr
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using integer type which will show 1 and 0 without decimals
Using string or boolean types which are not suitable here
3fill in blank
hard

Fix the error in the code to create a 5x5 identity matrix with numpy.

NumPy
import numpy as np
matrix = np.eye([1])
print(matrix)
Drag options to blanks, or click blank then click option'
A5
Bfive
C'5'
Dnp
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the size as a string instead of an integer
Passing a variable name that is not defined
4fill in blank
hard

Fill both blanks to create a 3x5 identity-like matrix with ones on the diagonal.

NumPy
import numpy as np
matrix = np.eye([1], [2])
print(matrix)
Drag options to blanks, or click blank then click option'
A3
B4
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping rows and columns
Using equal numbers for both parameters
5fill in blank
hard

Fill all three blanks to create a 4x4 identity matrix with ones on the diagonal shifted by 1 above the main diagonal.

NumPy
import numpy as np
matrix = np.eye([1], [2], k=[3])
print(matrix)
Drag options to blanks, or click blank then click option'
A4
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative k which shifts below the main diagonal
Using different sizes for rows and columns