Bird
0
0

What is the issue with this scipy K-means code snippet?

medium📝 Debug Q6 of 15
SciPy - Clustering and Distance
What is the issue with this scipy K-means code snippet?
from scipy.cluster.vq import kmeans2
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6]])
centroids, labels = kmeans2(data, 0)
print(labels)
Akmeans2 requires the data to be normalized first.
BThe data array is not two-dimensional.
CThe number of clusters is zero, which is invalid.
DThe function kmeans2 does not return labels.
Step-by-Step Solution
Solution:
  1. Step 1: Check the number of clusters parameter

    The second argument to kmeans2 is the number of clusters; zero is invalid.
  2. Step 2: Validate data shape

    The data is a valid 2D numpy array, so no issue there.
  3. Step 3: Confirm function output

    kmeans2 returns centroids and labels, so the unpacking is correct.
  4. Final Answer:

    The number of clusters is zero, which is invalid. is the error: zero clusters is not allowed.
  5. Quick Check:

    Clusters must be > 0 [OK]
Quick Trick: Number of clusters must be positive integer [OK]
Common Mistakes:
  • Passing zero or negative clusters
  • Assuming kmeans2 normalizes data automatically
  • Expecting kmeans2 to return only centroids

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes