Bird
Raised Fist0
SciPydata~20 mins

K-means via scipy vs scikit-learn - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
K-means Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of K-means clustering with scipy.cluster.vq
What is the output of the following code snippet that uses scipy's kmeans and vq functions?
SciPy
import numpy as np
from scipy.cluster.vq import kmeans, vq

np.random.seed(0)
data = np.vstack((np.random.normal(0, 1, (5, 2)), np.random.normal(5, 1, (5, 2))))
centroids, distortion = kmeans(data, 2)
labels, _ = vq(data, centroids)
print(labels.tolist())
A[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
B[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
C[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
D[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
Attempts:
2 left
💡 Hint
Remember that kmeans clusters points based on proximity to centroids, and vq assigns labels accordingly.
Predict Output
intermediate
2:00remaining
Output of KMeans from scikit-learn with fixed random state
What is the output of the following code that uses scikit-learn's KMeans to cluster the same data?
SciPy
import numpy as np
from sklearn.cluster import KMeans

np.random.seed(0)
data = np.vstack((np.random.normal(0, 1, (5, 2)), np.random.normal(5, 1, (5, 2))))
kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
print(kmeans.labels_.tolist())
A[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
B[1, 1, 1, 1, 1, 0, 0, 0, 0, 0]
C[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
D[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
Attempts:
2 left
💡 Hint
scikit-learn's KMeans also clusters points based on proximity and uses random_state for reproducibility.
data_output
advanced
2:00remaining
Number of unique clusters from scipy kmeans labels
After running scipy's kmeans and vq on a dataset with 3 clusters, how many unique cluster labels will the labels array contain?
SciPy
import numpy as np
from scipy.cluster.vq import kmeans, vq

np.random.seed(1)
data = np.vstack((np.random.normal(0, 1, (4, 2)), np.random.normal(5, 1, (4, 2)), np.random.normal(10, 1, (4, 2))))
centroids, _ = kmeans(data, 3)
labels, _ = vq(data, centroids)
unique_labels = len(set(labels))
print(unique_labels)
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
The number of clusters requested is 3, so expect 3 unique labels.
visualization
advanced
3:00remaining
Visual difference between scipy and scikit-learn K-means centroids
Which option correctly describes the difference in centroid positions when clustering the same dataset with scipy.cluster.vq.kmeans and sklearn.cluster.KMeans?
SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.vq import kmeans
from sklearn.cluster import KMeans

np.random.seed(42)
data = np.vstack((np.random.normal(0, 1, (10, 2)), np.random.normal(5, 1, (10, 2))))

centroids_scipy, _ = kmeans(data, 2)
kmeans_sklearn = KMeans(n_clusters=2, random_state=42).fit(data)
centroids_sklearn = kmeans_sklearn.cluster_centers_

plt.scatter(data[:,0], data[:,1], c='gray', label='Data points')
plt.scatter(centroids_scipy[:,0], centroids_scipy[:,1], c='red', marker='x', s=100, label='Scipy centroids')
plt.scatter(centroids_sklearn[:,0], centroids_sklearn[:,1], c='blue', marker='o', s=100, label='Sklearn centroids')
plt.legend()
plt.title('Centroids from scipy vs sklearn K-means')
plt.show()
ASklearn centroids are always the mean of all data points, while scipy centroids are medians.
BScipy centroids and sklearn centroids overlap exactly because both use the same algorithm and initialization.
CScipy centroids are always closer to the origin than sklearn centroids.
DScipy centroids and sklearn centroids differ slightly because sklearn uses k-means++ initialization and iterative refinement.
Attempts:
2 left
💡 Hint
Consider differences in initialization and algorithm details between scipy and sklearn implementations.
🧠 Conceptual
expert
3:00remaining
Key difference in output between scipy.cluster.vq.kmeans and sklearn.cluster.KMeans
Which statement best describes a key difference in the outputs of scipy.cluster.vq.kmeans and sklearn.cluster.KMeans when applied to the same dataset?
AScipy's kmeans always produces deterministic results, while sklearn's KMeans results vary randomly every run.
BScipy's kmeans returns centroids and distortion, while sklearn's KMeans returns centroids, labels, and inertia with iterative convergence.
CSklearn's KMeans uses hierarchical clustering internally, while scipy's kmeans uses flat clustering.
DScipy's kmeans returns labels directly, while sklearn's KMeans only returns centroids without labels.
Attempts:
2 left
💡 Hint
Think about what each function returns and how the algorithms run.

Practice

(1/5)
1. What is the main difference between K-means clustering in scipy and scikit-learn?
easy
A. scikit-learn does not support K-means clustering.
B. scikit-learn requires manual centroid initialization, but scipy does not.
C. scipy automatically plots clusters, but scikit-learn does not.
D. scipy requires separate steps for centroid calculation and label assignment, while scikit-learn combines them.

Solution

  1. Step 1: Understand K-means steps in scipy

    In scipy, you first find centroids using kmeans, then assign labels with vq.
  2. Step 2: Understand K-means in scikit-learn

    scikit-learn combines these steps in one KMeans class that fits and predicts labels together.
  3. Final Answer:

    scipy requires separate steps for centroid calculation and label assignment, while scikit-learn combines them. -> Option D
  4. Quick Check:

    K-means steps differ: separate in scipy, combined in scikit-learn [OK]
Hint: Remember: scipy splits steps, scikit-learn combines [OK]
Common Mistakes:
  • Thinking scikit-learn lacks K-means
  • Assuming scipy auto-assigns labels
  • Confusing plotting features with clustering steps
2. Which of the following is the correct way to import K-means functions from scipy for clustering?
easy
A. import scipy.kmeans as km
B. from scipy.kmeans import cluster
C. from scipy.cluster.vq import kmeans, vq
D. from sklearn.cluster import kmeans

Solution

  1. Step 1: Recall scipy K-means import syntax

    The correct import for K-means in scipy is from scipy.cluster.vq importing kmeans and vq.
  2. Step 2: Check other options

    Options A and B use incorrect module names, and D is from scikit-learn, not scipy.
  3. Final Answer:

    from scipy.cluster.vq import kmeans, vq -> Option C
  4. Quick Check:

    Correct scipy import = from scipy.cluster.vq import kmeans, vq [OK]
Hint: Use scipy.cluster.vq for K-means imports [OK]
Common Mistakes:
  • Confusing sklearn imports with scipy
  • Using wrong module names like scipy.kmeans
  • Trying to import cluster from scipy directly
3. Given the code below, what will be the output of labels?
import numpy as np
from scipy.cluster.vq import kmeans, vq

data = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
centroids, _ = kmeans(data, np.array([[1, 2], [10, 2]]))
labels, _ = vq(data, centroids)
print(labels.tolist())
medium
A. [0, 0, 0, 1, 1, 1]
B. [1, 1, 1, 0, 0, 0]
C. [0, 1, 0, 1, 0, 1]
D. [1, 0, 1, 0, 1, 0]

Solution

  1. Step 1: Understand data and centroids

    Data has two groups: points near (1, y) and points near (10, y). Kmeans with 2 clusters finds centroids near these groups.
  2. Step 2: Assign labels with vq

    Points near (1, y) get label 0, points near (10, y) get label 1. So first three points labeled 0, last three labeled 1.
  3. Final Answer:

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

    Clusters split by x-coordinate: left=0, right=1 [OK]
Hint: Group points by centroid proximity for labels [OK]
Common Mistakes:
  • Assuming labels are reversed
  • Mixing up label order
  • Expecting labels to be random
4. What is wrong with this code snippet using scipy for K-means clustering?
import numpy as np
from scipy.cluster.vq import kmeans

data = np.array([[1, 2], [3, 4], [5, 6]])
centroids, labels = kmeans(data, 2)
print(labels)
medium
A. kmeans returns centroids and distortion, not labels.
B. Data array shape is invalid for kmeans.
C. kmeans requires 3 clusters, not 2.
D. Missing import for vq function.

Solution

  1. Step 1: Check kmeans return values

    kmeans returns centroids and distortion value, not labels.
  2. Step 2: Identify correct label assignment

    Labels must be assigned using vq with data and centroids after kmeans.
  3. Final Answer:

    kmeans returns centroids and distortion, not labels. -> Option A
  4. Quick Check:

    kmeans output ≠ labels; use vq for labels [OK]
Hint: Remember: kmeans returns centroids, not labels [OK]
Common Mistakes:
  • Expecting kmeans to return labels
  • Not using vq to assign labels
  • Confusing distortion with labels
5. You want to cluster a dataset using K-means and compare results between scipy and scikit-learn. Which approach correctly ensures comparable cluster labels?
hard
A. Run scipy's kmeans only, then run scikit-learn's KMeans without setting random_state, compare labels directly.
B. Run scipy's kmeans and vq, then run scikit-learn's KMeans with same n_clusters and random_state, compare labels directly.
C. Run scikit-learn's KMeans only, then assign labels manually using scipy's vq with random centroids.
D. Run scipy's kmeans and assign labels randomly, then run scikit-learn's KMeans with default settings.

Solution

  1. Step 1: Understand label consistency

    To compare cluster labels, both methods must use the same number of clusters and fixed random seed for reproducibility.
  2. Step 2: Apply correct procedure

    Use scipy's kmeans and vq with fixed initialization, and scikit-learn's KMeans with same n_clusters and random_state. Then compare labels.
  3. Final Answer:

    Run scipy's kmeans and vq, then run scikit-learn's KMeans with same n_clusters and random_state, compare labels directly. -> Option B
  4. Quick Check:

    Matching clusters need same params and fixed seed [OK]
Hint: Fix random_state and n_clusters to compare labels [OK]
Common Mistakes:
  • Not fixing random_state causing label mismatch
  • Assigning labels randomly in scipy
  • Comparing labels without same cluster count