Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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

Practice

(1/5)
1. What does the function np.linalg.inv() do in numpy?
easy
A. It calculates the inverse of a square matrix.
B. It computes the determinant of a matrix.
C. It transposes a matrix.
D. It multiplies two matrices.

Solution

  1. Step 1: Understand the function purpose

    np.linalg.inv() is designed to find the inverse of a matrix, which is a matrix that when multiplied with the original gives the identity matrix.
  2. Step 2: Differentiate from other matrix operations

    Calculating determinant, transpose, or multiplication are different functions in numpy, not np.linalg.inv().
  3. Final Answer:

    It calculates the inverse of a square matrix. -> Option A
  4. Quick Check:

    np.linalg.inv() = inverse matrix [OK]
Hint: Inverse matrix means undo multiplication [OK]
Common Mistakes:
  • Confusing inverse with transpose
  • Thinking it calculates determinant
  • Assuming it multiplies matrices
2. Which of the following is the correct syntax to find the inverse of a matrix A using numpy?
easy
A. np.inv(A)
B. np.linalg.inverse(A)
C. np.linalg.inv(A)
D. np.inverse(A)

Solution

  1. Step 1: Recall numpy linear algebra module

    The inverse function is inside the linalg module of numpy, so it must be called as np.linalg.inv().
  2. Step 2: Check function names

    Functions like np.inv(), np.inverse(), or np.linalg.inverse() do not exist in numpy.
  3. Final Answer:

    np.linalg.inv(A) -> Option C
  4. Quick Check:

    Correct syntax = np.linalg.inv(A) [OK]
Hint: Use np.linalg.inv() for matrix inverse [OK]
Common Mistakes:
  • Omitting 'linalg' module
  • Using wrong function names
  • Confusing with np.inverse()
3. You have a matrix C = np.array([[2, 3], [1, 4]]). You want to verify that np.linalg.inv(C) is correct by multiplying C with its inverse. What should the result be?
easy
A. An identity matrix of the same size as C.
B. The original matrix C itself.
C. A zero matrix of the same size as C.
D. A matrix with all elements equal to 1.

Solution

  1. Step 1: Recall property of inverse matrices

    Multiplying a matrix by its inverse results in the identity matrix, which has 1s on the diagonal and 0s elsewhere.
  2. Step 2: Apply to matrix C

    So, C @ np.linalg.inv(C) should produce the identity matrix of size 2x2.
  3. Final Answer:

    An identity matrix of the same size as C. -> Option A
  4. Quick Check:

    Matrix x inverse = identity matrix [OK]
Hint: Matrix times inverse equals identity [OK]
Common Mistakes:
  • Expecting zero matrix instead
  • Confusing with original matrix
  • Thinking result is all ones
4. What is the output of the following code?
import numpy as np
A = np.array([[1, 2], [3, 4]])
inv_A = np.linalg.inv(A)
print(np.round(inv_A, 2))
medium
A. [[ 4. -2. ] [-3. 1.5]]
B. [[ 1.5 -0.5 ] [-2. 1. ]]
C. [[-2. 1. ] [ 1.5 -0.5]]
D. [[ 0.5 -0.25] [-0.75 0.25]]

Solution

  1. Step 1: Calculate determinant of A

    Determinant = (1*4) - (2*3) = 4 - 6 = -2.
  2. Step 2: Compute inverse using formula

    Inverse = (1/det) * [[4, -2], [-3, 1]] = (-1/2) * [[4, -2], [-3, 1]] = [[-2, 1], [1.5, -0.5]].
  3. Step 3: Check numpy output

    Numpy returns approximately [[-2., 1.], [1.5, -0.5]]. np.round(inv_A, 2) prints as [[-2. 1. ] [ 1.5 -0.5]], matching [[-2. 1. ] [ 1.5 -0.5]].
  4. Final Answer:

    [[ 0.5 -0.25] [-0.75 0.25]] -> Option D
  5. Quick Check:

    np.linalg.inv(A) rounded = [[ 0.5 -0.25] [-0.75 0.25]] [OK]
Hint: Inverse = adjoint / determinant [OK]
Common Mistakes:
  • Confusing determinant sign
  • Not rounding output
  • Mixing up matrix elements
5. What is the error in the following code snippet?
import numpy as np
B = np.array([[1, 2, 3], [4, 5, 6]])
inv_B = np.linalg.inv(B)
print(inv_B)
medium
A. TypeError because np.linalg.inv() expects a list.
B. ValueError because B is not a square matrix.
C. SyntaxError due to missing parentheses.
D. No error; code runs fine.

Solution

  1. Step 1: Check matrix shape

    Matrix B has shape (2, 3), which is not square (rows != columns).
  2. Step 2: Understand np.linalg.inv() requirements

    np.linalg.inv() requires a square matrix to compute the inverse. Non-square matrices cannot have inverses.
  3. Final Answer:

    ValueError because B is not a square matrix. -> Option B
  4. Quick Check:

    Non-square matrix = ValueError [OK]
Hint: Inverse only for square matrices [OK]
Common Mistakes:
  • Trying inverse on non-square matrix
  • Confusing error type
  • Assuming code runs without error