What is Clustering?
Clustering is an unsupervised machine learning technique used to group similar data points together.- Data points in the same cluster are similar.
- Data points in different clusters are different.
- It does not require labeled data.
1. K-Means Clustering
Definition
K-Means divides data into a fixed number (K) of clusters. It tries to keep data points within the same cluster as close to each other as possible.Steps
- Choose the number of clusters K.
- Select K centroids.
- Assign each data point to the nearest centroid.
- Calculate new centroids.
- Repeat until the clusters become stable.
Simple Example
Suppose we have students based on study hours and exam marks:
If
K = 2:
Python Code
Output
Explanation
model.labels_ tells us which cluster each data point belongs to.
model.cluster_centers_ gives the centroid of each cluster.
Important Points
- K must be specified beforehand.
- Uses centroids.
- Works well when clusters are roughly spherical.
- Sensitive to outliers.
- Results can depend on initial centroids.
Applications
- Customer segmentation
- Market analysis
- Image compression
- Grouping similar products
2. Hierarchical Clustering
Definition
Hierarchical Clustering creates a tree-like structure of clusters called a dendrogram. The most common approach is Agglomerative Clustering.Agglomerative Clustering
It follows a bottom-up approach. Initially:Simple Example
Python Code
Output
Explanation
The algorithm identifies two groups:Important Points
- Creates a dendrogram.
- Uses a bottom-up merging process in Agglomerative Clustering.
- Number of clusters can be selected by cutting the hierarchy.
- Can be computationally expensive for large datasets.
Applications
- Gene analysis
- Document classification
- Customer segmentation
- Social network analysis
3. DBSCAN
Full Form
DBSCAN = Density-Based Spatial Clustering of Applications with NoiseDefinition
DBSCAN groups data points based on density.Important Parameters
1. eps
Maximum distance used to find neighboring points.
2. min_samples
Minimum number of nearby points required to form a dense region.
Types of Points
Core Point Has enough neighboring points. Border Point Not dense enough itself but is close to a core point. Noise Point Does not belong to any cluster.Simple Example
× can be detected as noise.
Python Code
Output
Explanation
Important Points
- Does not require K.
- Can find clusters with irregular shapes.
- Can detect noise/outliers.
- Based on density.
- Can struggle when different clusters have very different densities.
Applications
- Geographic/spatial data
- Fraud detection
- Anomaly detection
- GPS/location analysis
- Finding unusual data points
Quick Comparison
Easy way to remember
K-Means = group around centers, Hierarchical = build a cluster tree, DBSCAN = find dense groups and detect noise.
Dimensionality Reduction
What is Dimensionality Reduction?
Dimensionality Reduction is the process of reducing the number of features (dimensions) in a dataset while keeping as much useful information as possible. Example: Suppose a dataset has:- Visualizing high-dimensional data
- Reducing computation
- Removing redundant information
- Improving model performance in some cases
- Making datasets easier to analyze
- PCA
- t-SNE
1. Principal Component Analysis (PCA)
Definition
PCA (Principal Component Analysis) transforms many features into a smaller number of new features called Principal Components. The new components try to preserve the maximum variance (information) from the original data.Simple Example
Suppose we have:Important Terms
Principal Component 1 (PC1) Captures the maximum possible variance. Principal Component 2 (PC2) Captures the next highest variance and is perpendicular to PC1.Python Example
Output
The exact component values can vary slightly depending on numerical precision, but the output will look like:Explanation
Original dataset:n_components=2 means we want 2 principal components.
explained_variance_ratio_ tells us how much information/variance each component captures.
For example:
Important Points
- PCA is a linear dimensionality reduction technique.
- It creates new features called principal components.
- Components are combinations of the original features.
- Usually, data should be standardized before PCA.
- PCA is useful for visualization and reducing features.
- PCA does not use target labels.
Applications
- Data visualization
- Image compression
- Feature reduction
- Noise reduction
- Preprocessing for machine learning
2. t-SNE
Full Form
t-SNE = t-Distributed Stochastic Neighbor EmbeddingDefinition
t-SNE is a dimensionality reduction technique mainly used to visualize high-dimensional data in 2D or 3D. Its main goal is to keep similar data points close together in the reduced space.Simple Idea
Suppose we have 50-dimensional data:Python Example
Output
Explanation
The Iris dataset originally contains:Important Points
- Mainly used for visualization.
- Excellent at showing local relationships.
- Can reduce data to 2D or 3D.
- Results can change depending on parameters and random initialization.
- Computationally expensive for large datasets.
- Unlike PCA, t-SNE is non-linear.
Applications
- Visualizing image datasets
- Visualizing word embeddings
- Exploring clusters
- Analyzing high-dimensional datasets
PCA vs t-SNE
Easy way to remember
One-line summary
PCA = reduce dimensions by preserving variance. t-SNE = reduce dimensions mainly to visualize similar data points.
Cluster Evaluation
What is Cluster Evaluation?
Cluster Evaluation is used to check how good the clusters are after applying a clustering algorithm. For example, after K-Means creates 3 clusters, we need to know:- Is
K = 3a good choice? - Are the clusters well separated?
- Are points inside a cluster similar?
- Elbow Method
- Silhouette Score
1. Elbow Method
Definition
The Elbow Method is used to find a suitable value of K in K-Means clustering. It uses WCSS (Within-Cluster Sum of Squares), also called inertia.Idea
Run K-Means with different values ofK:
Why does WCSS decrease?
When we increase K, clusters become smaller, so points become closer to their cluster centroids. Therefore:Python Example
Output
Important Point
Elbow Method → Find the suitable K for K-Means.
2. Silhouette Score
Definition
The Silhouette Score measures how well each data point fits within its cluster. It considers two things:- How close a point is to points in its own cluster
- How far it is from points in other clusters
Score Range
Interpretation
Therefore:
Higher Silhouette Score = Better clustering
Formula
For a data point:a= average distance from the point to its own clusterb= average distance from the point to the nearest other cluster
Python Example
Output
Elbow Method vs Silhouette Score
Easy way to remember
One-line summary
Elbow Method finds a suitable number of clusters, while Silhouette Score measures how well-separated and cohesive those clusters are.