0
0
NumPydata~20 mins

np.linalg.inv() for matrix inverse in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Inverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of matrix inverse calculation
What is the output of this code snippet using np.linalg.inv()?
NumPy
import numpy as np
A = np.array([[4, 7], [2, 6]])
inv_A = np.linalg.inv(A)
print(inv_A)
A
[[ 1.5 -1.7]
 [-0.2  0.4]]
B
[[ 0.6  0.7]
 [ 0.2  0.4]]
C
[[ 0.6 -0.7]
 [ 0.2 -0.4]]
D
[[ 0.6 -0.7]
 [-0.2  0.4]]
Attempts:
2 left
💡 Hint
Recall the formula for 2x2 matrix inverse: 1/det * [[d, -b], [-c, a]]
data_output
intermediate
1:00remaining
Shape of inverse matrix
Given a square matrix B of shape (3, 3), what is the shape of np.linalg.inv(B)?
A(9,)
B(3,)
C(3, 3)
D(1, 3)
Attempts:
2 left
💡 Hint
The inverse of a square matrix has the same shape as the original matrix.
🔧 Debug
advanced
1:30remaining
Error raised by singular matrix inverse
What error does this code raise when trying to invert a singular matrix?
NumPy
import numpy as np
C = np.array([[1, 2], [2, 4]])
inv_C = np.linalg.inv(C)
ATypeError: unsupported operand type(s)
Bnumpy.linalg.LinAlgError: Singular matrix
CValueError: shapes not aligned
DIndexError: index out of bounds
Attempts:
2 left
💡 Hint
A singular matrix has determinant zero and cannot be inverted.
🧠 Conceptual
advanced
2:00remaining
Matrix inverse property check
If D is an invertible matrix, which expression correctly checks if inv_D is the inverse of D?
Anp.allclose(np.dot(D, inv_D), np.eye(D.shape[0]))
Bnp.dot(D, inv_D) == np.eye(D.shape[0])
Cnp.array_equal(np.dot(D, inv_D), np.eye(D.shape[0]))
Dnp.isclose(np.dot(D, inv_D), np.eye(D.shape[0]))
Attempts:
2 left
💡 Hint
Use a function that allows for floating point tolerance when comparing arrays.
🚀 Application
expert
2:30remaining
Inverse of a matrix product
Given two invertible matrices E and F, which expression correctly computes the inverse of their product E @ F?
Anp.linalg.inv(F) @ np.linalg.inv(E)
Bnp.linalg.inv(E) @ np.linalg.inv(F)
Cnp.linalg.inv(E @ F)
Dnp.linalg.inv(F) + np.linalg.inv(E)
Attempts:
2 left
💡 Hint
Recall the rule: (AB)^-1 = B^-1 A^-1