Bird
0
0

Find the bug in this scikit-learn KMeans usage:

medium📝 Debug Q7 of 15
SciPy - Clustering and Distance
Find the bug in this scikit-learn KMeans usage:
from sklearn.cluster import KMeans
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6]])
kmeans = KMeans(n_clusters=3)
kmeans.fit_predict(data)
print(kmeans.labels_)
Alabels_ attribute is not available after fit_predict.
Bn_clusters must be less than number of data points.
Cfit_predict returns labels but is not assigned to a variable.
DData must be a list, not a numpy array.
Step-by-Step Solution
Solution:
  1. Step 1: Understand fit_predict behavior

    fit_predict returns cluster labels but here return value is ignored.
  2. Step 2: Check labels_ attribute usage

    labels_ is set after fit or fit_predict, so print(kmeans.labels_) works correctly.
  3. Step 3: Validate n_clusters parameter

    n_clusters=3 but data has only 3 points; scikit-learn allows n_clusters equal to number of points, but not greater. Here, it's equal, so no error.
  4. Final Answer:

    fit_predict returns labels but is not assigned to a variable. -> Option C
  5. Quick Check:

    Use fit_predict output properly = fit_predict returns labels but is not assigned to a variable. [OK]
Quick Trick: Assign fit_predict output to variable if needed [OK]
Common Mistakes:
  • Ignoring fit_predict return value
  • Misunderstanding labels_ availability
  • Confusing data type requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes