Unsupervised Learning
Understand clustering techniques like K-Means and hierarchical clustering.
Unsupervised Learning is a free Python Academy lesson on CoddyKit — lesson 3 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Unsupervised Learning
Unsupervised learning is a type of machine learning where the algorithm learns patterns from unlabeled data. It is often used for clustering, dimensionality reduction, and anomaly detection.
In this lesson, you’ll explore clustering techniques like K-Means and hierarchical clustering.

2
What is Unsupervised Learning?
Unsupervised learning involves analyzing and grouping data without predefined labels. It aims to identify hidden structures or patterns in the data.
Key tasks include:
- Clustering: Grouping similar data points.
- Dimensionality Reduction: Reducing the number of features while retaining important information.
3
K-Means Clustering
K-Means is a popular clustering algorithm that partitions data into K clusters based on feature similarity. Let’s see how it works:
# Example: K-Means Clustering
from sklearn.cluster import KMeans
import numpy as np
# Sample data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=2, random_state=0)
kmeans.fit(X)
print("Cluster Centers:", kmeans.cluster_centers_)
print("Labels:", kmeans.labels_)4
Hierarchical Clustering
Hierarchical clustering builds a tree-like structure (dendrogram) to group similar data points. It doesn’t require a predefined number of clusters.
# Example: Hierarchical Clustering
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# Sample data
X = [[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]
linked = linkage(X, 'single')
dendrogram(linked)
plt.show()5
Dimensionality Reduction
Dimensionality reduction simplifies data by reducing the number of features while retaining important information. Principal Component Analysis (PCA) is a common technique for this task.
# Example: PCA
from sklearn.decomposition import PCA
import numpy as np
# Sample data
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
pca = PCA(n_components=1)
X_reduced = pca.fit_transform(X)
print("Reduced Data:", X_reduced)6
Applications of Unsupervised Learning
Unsupervised learning is used in:
- Customer Segmentation: Grouping customers based on behavior.
- Anomaly Detection: Identifying unusual patterns in data.
- Image Compression: Reducing image size while retaining quality.
7
Visualizing Clusters
Visualizing clusters helps in understanding the grouping of data points. Matplotlib and Seaborn are useful for this purpose:
# Example: Visualizing clusters
import matplotlib.pyplot as plt
# Sample data and labels
X = [[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]
labels = [0, 0, 0, 1, 1, 1]
for i in range(len(X)):
plt.scatter(X[i][0], X[i][1], c='r' if labels[i] == 0 else 'b')
plt.show()8
Challenges in Unsupervised Learning
Unsupervised learning faces several challenges, including:
- Defining the Number of Clusters: K-Means requires you to specify the number of clusters beforehand.
- Interpretability: Clusters may not always have clear meanings.
- Scalability: Handling large datasets can be computationally intensive.
9
10
Common Mistakes in Unsupervised Learning
Here are some mistakes to avoid:
- Choosing the wrong number of clusters in K-Means.
- Failing to preprocess data, such as scaling features.
- Overinterpreting clusters without validation.
11
What Did We Learn?
In this lesson, you learned:
- The basics of unsupervised learning and its key tasks.
- How to implement K-Means and hierarchical clustering.
- The concept of dimensionality reduction using PCA.
- Applications, challenges, and common mistakes in unsupervised learning.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Unsupervised Learning” lesson free?
Yes — the full text of “Unsupervised Learning” is free to read here on the web, and the Python Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unsupervised Learning”?
Understand clustering techniques like K-Means and hierarchical clustering. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Unsupervised Learning” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.