Bird
0
0

Examine the code below:

medium📝 Debug Q6 of 15
SciPy - Image Processing (scipy.ndimage)
Examine the code below:
import numpy as np
from scipy.ndimage import label

arr = np.array([[1, 0], [0, 1]])
labeled, count = label(arr, connectivity=3)
print(count)

What is the issue with this code?
AThe connectivity parameter value 3 is invalid; it must be 1 or 2 for 2D arrays
BThe array contains zeros which cannot be labeled
CThe label function does not return two values
DThe import statement is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand connectivity parameter

    For 2D arrays, connectivity can only be 1 (4-connectivity) or 2 (8-connectivity).
  2. Step 2: Identify invalid parameter

    Using connectivity=3 is invalid and will raise an error.
  3. Final Answer:

    The connectivity parameter value 3 is invalid; it must be 1 or 2 for 2D arrays -> Option A
  4. Quick Check:

    Connectivity must be 1 or 2 in 2D [OK]
Quick Trick: Connectivity in 2D is 1 or 2 only [OK]
Common Mistakes:
  • Assuming any integer connectivity is allowed
  • Thinking zeros cannot be labeled
  • Believing label returns a single value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes