Intermediate
Clustering
Discover natural groupings in your data using K-Means, hierarchical clustering, DBSCAN, and dimensionality reduction with PCA and t-SNE.
K-Means Clustering
R
# Prepare data (scale features) data <- scale(iris[, 1:4]) # Run K-Means with 3 clusters set.seed(42) km <- kmeans(data, centers = 3, nstart = 25) # Results km$cluster # Cluster assignments km$centers # Cluster centroids km$tot.withinss # Total within-cluster sum of squares # Elbow method to find optimal K library(factoextra) fviz_nbclust(as.data.frame(data), kmeans, method = "wss") # Visualize clusters fviz_cluster(km, data = data)
Hierarchical Clustering
R
# Compute distance matrix dist_mat <- dist(data, method = "euclidean") # Hierarchical clustering hc <- hclust(dist_mat, method = "ward.D2") # Plot dendrogram plot(hc, cex = 0.6) rect.hclust(hc, k = 3) # Draw boxes around 3 clusters # Cut tree to get cluster assignments clusters <- cutree(hc, k = 3) # Better dendrogram with factoextra fviz_dend(hc, k = 3, rect = TRUE, cex = 0.5)
DBSCAN
R
library(dbscan) # DBSCAN - density-based clustering db <- dbscan(data, eps = 0.8, minPts = 5) db$cluster # 0 = noise points # Find optimal eps with k-NN distance plot kNNdistplot(data, k = 5) abline(h = 0.8, col = "red", lty = 2) # Visualize fviz_cluster(db, data = data, geom = "point")
PCA for Dimensionality Reduction
R
# PCA pca <- prcomp(iris[, 1:4], scale. = TRUE) # Variance explained summary(pca) fviz_eig(pca) # Scree plot # Biplot fviz_pca_ind(pca, col.ind = iris$Species, addEllipses = TRUE) fviz_pca_biplot(pca, col.ind = iris$Species)
t-SNE
R
library(Rtsne) # Remove duplicate rows first unique_data <- unique(iris[, 1:4]) set.seed(42) tsne <- Rtsne(as.matrix(unique_data), dims = 2, perplexity = 30) # Plot tsne_df <- data.frame(tsne$Y, Species = iris$Species[!duplicated(iris[,1:4])]) ggplot(tsne_df, aes(X1, X2, color = Species)) + geom_point(size = 2) + theme_minimal() + labs(title = "t-SNE Visualization")
Silhouette Analysis
R
# Silhouette plot for K-Means fviz_silhouette(silhouette(km$cluster, dist_mat)) # Optimal clusters using silhouette fviz_nbclust(as.data.frame(data), kmeans, method = "silhouette")
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX