What is Unsupervised Learning?
Unsupervised Learning is a type of machine learning where the algorithm learns patterns from unlabeled data. Unlike supervised learning, there are no correct answers to guide the learning process.
Key Concept
The algorithm must find hidden structure in data on its own, without being told what to look for.
Supervised Learning
Data: Images + Labels (cat, dog)
Goal: Predict labels for new images
Unsupervised Learning
Data: Images only (no labels)
Goal: Group similar images together
Clustering
Clustering groups similar data points together. It's one of the most common unsupervised learning techniques.
K-Means Clustering
from sklearn.cluster import KMeans
import numpy as np
# Sample customer data: [age, annual_income, spending_score]
customers = np.array([
[25, 40000, 80],
[30, 45000, 75],
[45, 80000, 20],
[50, 90000, 25],
[35, 60000, 50],
])
# Create K-Means with 2 clusters
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans.fit(customers)
# Get cluster assignments
print("Cluster labels:", kmeans.labels_)
# Output: [0, 0, 1, 1, 0]
# Cluster 0: Young, high spenders
# Cluster 1: Older, low spenders
Applications of Clustering
Customer Segmentation
Image Compression
Document Grouping
Dimensionality Reduction
Reduce the number of features while preserving important information. Essential for visualization and dealing with high-dimensional data.
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
# Load iris dataset (4 features)
iris = load_iris()
X = iris.data # Shape: (150, 4)
# Reduce to 2 dimensions for visualization
pca = PCA(n_components=2)
X_reduced = pca.fit_transform(X)
print(f"Original shape: {X.shape}")
print(f"Reduced shape: {X_reduced.shape}")
print(f"Variance explained: {pca.explained_variance_ratio_.sum():.2%}")
# Original shape: (150, 4)
# Reduced shape: (150, 2)
# Variance explained: 97.77%
Anomaly Detection
Identify unusual patterns that don't conform to expected behavior. Critical for fraud detection, system monitoring, and security.
Fraud Detection
Detect unusual credit card transactions that might be fraudulent.
System Monitoring
Identify server anomalies before they cause outages.
Advanced Algorithms
DBSCAN
Density-based clustering that can find arbitrarily shaped clusters and identify outliers.
Hierarchical Clustering
Creates a tree of clusters (dendrogram) showing relationships between groups.
t-SNE
Powerful visualization technique for high-dimensional data.
Ready for Reinforcement Learning?
Learn how agents learn through trial and error.