Course materials · Introduction to Machine Learning

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 K∈{2,3,4}K\in\{2,3,4\}.

Procedure

  1. Train KMeans(random_state=7,n_init=10) for each K.
  2. Plot cluster centers and assignments, and record Inertia and Silhouette.

Results to retain

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

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

  1. Prepare the original X, X2 with its first column multiplied by 10, and X2 standardized column by column.
  2. Retrain KMeans with K∈{2,3,4}K\in\{2,3,4\} on each dataset. Keep random_state=7 and n_init=10 unchanged.
  3. 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

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

Checking Other Inputs

Starting conditions

Compare the same K values on data with cluster_std=1.6.

Procedure

  1. In make_blobs, change only cluster_std to 1.6. Keep the sample count, number of centers, and Seed unchanged.
  2. 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=.8 results.
  3. 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

# 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