Unsupervised Learning
English translation
Execution setup
Run import numpy as np and import matplotlib.pyplot as plt in a code cell before the practice code. Use np for array operations and plt for plots.
Learning objectives
Changing the number of clusters and the scale can change how the same data are grouped. Interpret the structure without assigning meaning to cluster numbers themselves.
Cluster Count and Inertia
Starting conditions
Use make_blobs(n_samples=180,centers=3,cluster_std=.8,random_state=7) and .
Procedure
- Train
KMeans(random_state=7,n_init=10)for each K. - Plot cluster centers and assignments, and record Inertia and Silhouette.
Results to retain
- Three cluster plots and a metrics table.
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
X,_=make_blobs(n_samples=180,centers=3,cluster_std=.8,random_state=7)
model=KMeans(n_clusters=3,random_state=7,n_init=10).fit(X)
print(model.inertia_,silhouette_score(X,model.labels_))
Record results and self-check
K:
Inertia:
Silhouette:
Observations by cluster count:
Points to check
- Did you avoid interpreting cluster numbers themselves as ground truth classes?
Changing feature units
Starting conditions
Multiply only X's first column by 10, and compare the original data, modified data, and results of column-wise standardization.
Procedure
- Prepare the original
X,X2with its first column multiplied by 10, andX2standardized column by column. - Retrain KMeans with on each dataset. Keep
random_state=7andn_init=10unchanged. - Plot cluster assignments and centers for each data condition, and record Inertia and Silhouette by K in tables with the same format. Use the plots to judge whether scaling up the first column changed assignments and centers, and explain your evidence.
Results to retain
- Cluster plots for all three data conditions and a metrics table by K.
import numpy as np
from sklearn.preprocessing import StandardScaler
X2=X.copy();X2[:,0]*=10
Record results and self-check
Data condition and K:
Cluster plot and centers:
Inertia:
Interpretation of Silhouette and cluster direction:
Points to check
- Did you use the same K and Seed for all three data conditions?
Checking Other Inputs
Starting conditions
Compare the same K values on data with cluster_std=1.6.
Procedure
- In
make_blobs, change onlycluster_stdto 1.6. Keep the sample count, number of centers, and Seed unchanged. - Train KMeans for the same three K values and obtain cluster plots, Inertia, and Silhouette. Record results for each K alongside the corresponding
cluster_std=.8results. - Distinguish the fact that the data were generated with three centers from whether the scatter plot visibly shows three clearly separated groups.
Results to retain
- Compare plots and metrics for the two cluster-variance conditions at the same K.
# Use the preceding activity's data and functions to check the specified new examples.
Record results and self-check
cluster_std·K:
Cluster plot:
Inertia·Silhouette:
Separation patterns in the two datasets:
Points to check
- Did you compare overlap between datasets at the same K?