Bird
0
0

What will be the labeled array output of this code?

medium📝 Predict Output Q5 of 15
SciPy - Image Processing (scipy.ndimage)
What will be the labeled array output of this code?
import numpy as np
from scipy.ndimage import label

input_array = np.array([[0,1,1,0],
                        [1,1,0,0],
                        [0,0,1,1],
                        [0,1,1,0]])
labeled_array, num_features = label(input_array)
print(labeled_array)
A[[0 1 1 0] [1 1 0 0] [0 0 0 0] [0 0 0 0]]
B[[0 1 1 0] [1 1 0 0] [0 0 1 1] [0 1 1 0]]
C[[0 1 1 0] [1 1 0 0] [0 0 2 2] [0 2 2 0]]
D[[1 1 1 1] [1 1 1 1] [1 1 1 1] [1 1 1 1]]
Step-by-Step Solution
Solution:
  1. Step 1: Identify connected components in input_array

    There are two connected components: one in top-left (label 1), one in bottom-right (label 2).
  2. Step 2: Understand labeling output

    Label 1 assigned to first cluster, label 2 to second cluster, zeros remain zeros.
  3. Final Answer:

    [[0 1 1 0] [1 1 0 0] [0 0 2 2] [0 2 2 0]] -> Option C
  4. Quick Check:

    Labeled array matches connected clusters with distinct labels [OK]
Quick Trick: Labels start at 1 for each connected group [OK]
Common Mistakes:
  • Assuming labels start at 0
  • Not distinguishing separate clusters
  • Confusing input array with labeled output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes