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
Recall & Review
beginner
What is the main purpose of K-means clustering?
K-means clustering groups data points into clusters by minimizing the distance between points and their cluster centers.
Click to reveal answer
beginner
Which Python library provides a simple K-means implementation with many options and is widely used in machine learning?
scikit-learn provides a popular and flexible K-means implementation with many options like initialization methods, number of clusters, and max iterations.
Click to reveal answer
intermediate
What function does scipy use for K-means clustering?
scipy uses the function scipy.cluster.vq.kmeans for K-means clustering, which is simpler and less feature-rich than scikit-learn's version.
Click to reveal answer
intermediate
Name one key difference between K-means in scipy and scikit-learn.
scikit-learn's KMeans class supports more features like different initialization methods, convergence criteria, and easy prediction of cluster labels, while scipy's kmeans is more basic.
Click to reveal answer
intermediate
How do you get cluster labels for each data point using scipy's K-means?
After running scipy.cluster.vq.kmeans to get cluster centers, use scipy.cluster.vq.vq to assign each data point to the nearest cluster center to get labels.
Click to reveal answer
Which library's K-means implementation allows easy prediction of cluster labels with a single method call?
Ascikit-learn
Bscipy
Cnumpy
Dpandas
✗ Incorrect
scikit-learn's KMeans class has a predict() method to assign cluster labels easily.
What function in scipy is used to assign data points to clusters after computing centers?
Ascipy.cluster.vq.kmeans
Bscipy.cluster.vq.vq
Cscipy.cluster.vq.whiten
Dscipy.cluster.hierarchy.linkage
✗ Incorrect
scipy.cluster.vq.vq assigns each data point to the nearest cluster center.
Which of these is NOT a feature of scikit-learn's KMeans compared to scipy's kmeans?
AMultiple initialization methods
BBuilt-in label prediction
CConvergence tolerance settings
DHierarchical clustering
✗ Incorrect
Hierarchical clustering is a different method, not part of KMeans in scikit-learn.
What is the main output of scipy.cluster.vq.kmeans function?
ADistance matrix
BCluster labels for each point
CCluster centers
DData normalization
✗ Incorrect
The kmeans function returns the cluster centers after clustering.
Which library would you choose for a quick, simple K-means clustering without extra features?
Ascipy
Bmatplotlib
Cscikit-learn
Dseaborn
✗ Incorrect
scipy's kmeans is simpler and good for quick clustering without extra options.
Explain how K-means clustering works and how scipy and scikit-learn differ in their implementations.
Think about the steps to get clusters and labels in both libraries.
You got /4 concepts.
Describe a situation where you might prefer scipy's K-means over scikit-learn's KMeans.
Consider simplicity and feature needs.
You got /3 concepts.
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
Step 1: Understand K-means steps in scipy
In scipy, you first find centroids using kmeans, then assign labels with vq.
Step 2: Understand K-means in scikit-learn
scikit-learn combines these steps in one KMeans class that fits and predicts labels together.
Final Answer:
scipy requires separate steps for centroid calculation and label assignment, while scikit-learn combines them. -> Option D
Quick Check:
K-means steps differ: separate in scipy, combined in scikit-learn [OK]
Data has two groups: points near (1, y) and points near (10, y). Kmeans with 2 clusters finds centroids near these groups.
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.
Final Answer:
[0, 0, 0, 1, 1, 1] -> Option A
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
Step 1: Check kmeans return values
kmeans returns centroids and distortion value, not labels.
Step 2: Identify correct label assignment
Labels must be assigned using vq with data and centroids after kmeans.
Final Answer:
kmeans returns centroids and distortion, not labels. -> Option A
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
Step 1: Understand label consistency
To compare cluster labels, both methods must use the same number of clusters and fixed random seed for reproducibility.
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.
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
Quick Check:
Matching clusters need same params and fixed seed [OK]
Hint: Fix random_state and n_clusters to compare labels [OK]