Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
SciPy - Linear Algebra (scipy.linalg)
What is wrong with this code snippet?
import numpy as np
from scipy.linalg import svd
A = np.array([[1, 2], [3, 4]])
U, s, VT = svd(A)
print(s[2])
AU and VT are not returned by svd
Bs is not returned by svd
CIndexError because s has only 2 elements
Dprint statement syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Check shape of s

    For a 2x2 matrix, s has length 2 (two singular values).
  2. Step 2: Identify index error

    Accessing s[2] causes IndexError as valid indices are 0 and 1.
  3. Final Answer:

    IndexError because s has only 2 elements -> Option C
  4. Quick Check:

    Index out of range error = s length 2 [OK]
Quick Trick: s length equals min(matrix rows, columns) [OK]
Common Mistakes:
MISTAKES
  • Accessing out-of-range index
  • Thinking s is not returned
  • Misunderstanding print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes