Bird
0
0

What will be the output of the following code snippet?

medium📝 Predict Output Q4 of 15
SciPy - Clustering and Distance
What will be the output of the following code snippet?
import numpy as np
from scipy.cluster.vq import kmeans2

np.random.seed(0)
data = np.random.rand(5, 2)
centroids, labels = kmeans2(data, 2, minit='points')
print(labels)
A[0 1 0 1 0]
B[1 1 0 0 0]
C[0 0 1 1 1]
D[1 0 1 0 1]
Step-by-Step Solution
Solution:
  1. Step 1: Understand data and kmeans2 call

    Data is 5 points with 2 features, kmeans2 clusters into 2 groups with initial points as centroids.
  2. Step 2: Run kmeans2 and check labels

    Running kmeans2 with seed 0 assigns labels as [0 1 0 1 0].
  3. Final Answer:

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

    Labels output = [0 1 0 1 0] [OK]
Quick Trick: Seed fixes labels; first points cluster 0, others cluster 1 [OK]
Common Mistakes:
  • Ignoring random seed effect
  • Mixing cluster labels order
  • Assuming labels are probabilities

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes