🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML What Is Unsupervised Learning?

Unsupervised learning finds structure in data with no labels. The data has no answers attached, so the model looks for groups and patterns instead.

Labels are the answers

The Iris data has 150 rows and four measurements. The label column names the species of each flower.

from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data.shape)
print(iris.target.shape)
print([str(name) for name in iris.target_names])

Drop the labels and the groups stay visible

Petal length alone splits the flowers into two groups. The short-petal group and the long-petal group have very different means.

import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
petal = iris.data[:, 2]
short = petal < 2.5
print("Short rows:", int(short.sum()))
print("Short mean:", round(float(petal[short].mean()), 2))
print("Long rows:", int((~short).sum()))
print("Long mean:", round(float(petal[~short].mean()), 2))

One column can find a real group

The short-petal group holds 50 setosa flowers and nothing else. The split found a true group without using labels.

import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
petal = iris.data[:, 2]
short = petal < 2.5
short_species = iris.target[short]
print("Short group setosa:", int((short_species == 0).sum()))
print("Short group versicolor:", int((short_species == 1).sum()))
print("Short group virginica:", int((short_species == 2).sum()))

Example

import numpy as np
from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.target
names = [str(name) for name in iris.target_names]

# No labels are used to make the groups, only petal length.
petal = X[:, 2]
short = petal < 2.5
long = ~short

print("Short-petal rows:", int(short.sum()))
print("Long-petal rows:", int(long.sum()))
print("Short mean petal length:", round(float(petal[short].mean()), 2))
print("Long mean petal length:", round(float(petal[long].mean()), 2))
# The labels are used only after the split, to check what was found.
for code in range(3):
    print("Short group", names[code] + ":", int(((y == code) & short).sum()))
for code in range(3):
    print("Long group", names[code] + ":", int(((y == code) & long).sum()))

Key points

  • Unsupervised learning: learning from data with no labels
  • Label: the answer attached to a row, like a species name
  • Cluster: a group of rows that look alike
  • Use clustering to find groups in the data
  • Dimensionality reduction: shrinking many columns into a few that keep the shape of the data
  • Use dimensionality reduction to plot or compress many columns
  • Supervised learning needs labels, while unsupervised learning works without them

In this example, the Iris data loads with its four measurements. Petal length is column 2. Rows with a petal under 2.5 go in the short group, and the rest go in the long group. The split uses no labels at all. The short group holds 50 rows with a mean petal length of 1.46. The long group holds 100 rows with a mean of 4.91. Only then do the labels come out to check the groups. The short group is all setosa, so a simple split found a real group in the data. That is unsupervised learning: the structure was in the data, not in the answers.

Note

The next lessons teach K-Means, hierarchical clustering, DBSCAN, and PCA, and each one finds structure in a different way.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What is the key difference between supervised and unsupervised learning?