Sub topic 3
Unsupervised Learning: Finding Structure Without Labels
Assume you know Python, arrays, tables, functions, and basic mean and variance. This chapter focuses on data without a known target answer.
Unsupervised Learning
Unsupervised learning finds patterns in unlabeled data. Each example has features, but no label says what the correct result should be. The model may discover groups, unusual examples, or a smaller representation.
Clustering: Grouping Similar Examples
Clustering assigns similar examples to the same group. K-means clustering is a common method. It chooses a fixed number of cluster centers, assigns each example to its nearest center, and moves the centers until the assignments stabilize.
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
X, _ = load_iris(return_X_y=True) # Ignore the species labels.
model = KMeans(n_clusters=3, n_init=10, random_state=0)
groups = model.fit_predict(X)
print(groups[:10])
print(model.cluster_centers_)The numbers in groups are cluster IDs, not guaranteed meanings such as species names. You must inspect the features or compare with external information before interpreting them.
Dimensionality Reduction
Dimensionality reduction replaces many features with fewer summary features while preserving important variation. Principal component analysis (PCA) creates new directions called components. The first component captures the most variance, the second captures the next most, and so on.
| Task | Input | Output |
|---|---|---|
| Clustering | Unlabeled examples | Group assignments |
| PCA | Many numeric features | Fewer numeric components |
PCA can make data easier to visualize or reduce storage. The new components may be harder to explain than the original features.
Choosing an Approach
Use supervised learning when you have reliable labels and need predictions, such as a category or price. Use unsupervised learning when labels are absent or when discovering structure is the goal. Clustering answers “which examples are similar?” PCA answers “which compact representation preserves variation?”
Beyond the Two
Semi-supervised learning combines a small labeled set with a larger unlabeled set. Self-supervised learning creates training targets from the data itself, such as hiding part of a sentence. Reinforcement learning learns actions from rewards and penalties while interacting with an environment.
Exercise
You have customer records with no fraud labels and want to find unusual groups. Which approach could you try first?
Show answer
Try clustering or another unsupervised method, then validate whether the discovered groups are useful.