Dimensionality Reduction
and Clustering
[Link]
Agenda
Principal Introduction
Component to Clustering
Analysis (PCA) Algorithms
Applied Data Science – R programming 2
Introduction to PCA
Applied Data Science – R programming 3
# Load necessary libraries
library(ggfortify)
library(ggplot2) Definition
# Use mtcars dataset
data(mtcars)
Principal Component Analysis (PCA)
# Perform PCA with scaling
pca_result <- prcomp(mtcars, scale. = TRUE) is a technique for reducing the
# Summary of PCA dimensionality of datasets, increasing
summary(pca_result)
interpretability while minimizing
# Scree plot to visualize variance explained
plot(pca_result, type = "l", main = "Scree Plot") information loss.
# Visualize first two principal components
autoplot(pca_result, data = mtcars, colour = 'cyl',
loadings = TRUE, [Link] = TRUE,
[Link] = 'blue') +
ggtitle("PCA: mtcars Dataset (PC1 vs PC2)") +
theme_minimal() Applied Data Science – R programming 4
[Link](123)
# Simulate 100 images with 1024 pixels each (e.g.,
32x32 images)
Purpose
images <- matrix(rnorm(100 * 1024), nrow = 100, ncol =
1024)
# Perform PCA (scale pixels) PCA helps in simplifying the
pca_images <- prcomp(images, scale. = TRUE)
complexity in high-dimensional data
# Variance explained by components
summary(pca_images) while retaining trends and patterns.
# Plot scree plot to see how many components to keep
plot(pca_images, type = "l", main = "Scree Plot for Image
Data")
# Project images onto first 10 principal components
reduced_data <- pca_images$x[, 1:10]
head(reduced_data) # Reduced dimension
representation of images
Applied Data Science – R programming 5
How PCA Works
Applied Data Science – R programming 6
# Sample data: height (cm) and weight (kg)
df <- [Link](
height = c(170, 165, 180, 175, 160),
Identifying
)
weight = c(65, 60, 80, 75, 55)
Principal
# Perform PCA
pca_res <- prcomp(df, scale. = TRUE) Components
# Print loadings (coefficients of original variables in PCs)
print(pca_res$rotation) PCA identifies the directions
# Print principal component scores (transformed data) (principal components) in which
print(pca_res$x)
the data varies the most.
Applied Data Science – R programming 7
# Sample data
df <- [Link](
Orthogonality of
height = c(170, 165, 180, 175, 160),
weight = c(65, 60, 80, 75, 55)
Components
)
The principal components are
# Perform PCA
pca_res <- prcomp(df, scale. = TRUE)
orthogonal to each other, ensuring
# PC loadings (coefficients)
unique information capture.
print(pca_res$rotation)
# PC scores (new variables)
print(pca_res$x)
# Check correlation between PCs (should be near zero)
cor(pca_res$x[,1], pca_res$x[,2])
Applied Data Science – R programming 8
Steps in PCA
Applied Data Science – R programming 9
# Sample data with different scales
df <- [Link](
weight_kg = c(70, 80, 65, 90, 75),
height_cm = c(170, 180, 160, 190, 175),
Standardize the
income_thousands = c(50, 80, 40, 100, 60)
)
Data
# Perform PCA WITHOUT standardization
pca_no_scale <- prcomp(df, scale. = FALSE)
summary(pca_no_scale)
# Perform PCA WITH standardization Adjust the data to have a mean of
pca_scaled <- prcomp(df, scale. = TRUE)
summary(pca_scaled) zero and a standard deviation of
# Compare proportion of variance explained one.
plot(pca_no_scale, main = "Without Scaling")
plot(pca_scaled, main = "With Scaling")
Applied Data Science – R programming 10
# Sample data
Compute
df <- [Link](
height = c(170, 165, 180, 175, 160),
weight = c(65, 60, 80, 75, 55),
Covariance
age = c(30, 25, 35, 40, 22)
)
Matrix
# Standardize the data
df_scaled <- scale(df)
# Compute covariance matrix
cov_matrix <- cov(df_scaled) Calculate the covariance matrix to
print(cov_matrix)
understand the relationships
# Visualize covariance matrix (optional)
library(corrplot) between variables.
corrplot(cov_matrix, method = "circle", type = "lower",
[Link] = "black", [Link] = 45)
Applied Data Science – R programming 11
Calculating
Eigenvectors
and
Eigenvalues
Big Data Fundamentals 12
# Sample data: height and weight
df <- [Link](
height = c(170, 165, 180, 175, 160),
weight = c(65, 60, 80, 75, 55)
)
# Standardize data
df_scaled <- scale(df)
# Covariance matrix
cov_mat <- cov(df_scaled)
Eigenvectors
# Compute eigenvalues and eigenvectors Determine the directions of the
eig <- eigen(cov_mat)
new feature space.
# Eigenvectors matrix (directions)
print(eig$vectors)
# Eigenvalues (variance explained by each eigenvector)
print(eig$values)
Applied Data Science – R programming 13
# Sample data: height and weight
df <- [Link](
height = c(170, 165, 180, 175, 160),
weight = c(65, 60, 80, 75, 55)
)
# Standardize data
df_scaled <- scale(df)
# Covariance matrix
Eigenvalues
cov_mat <- cov(df_scaled)
# Compute eigenvalues and eigenvectors
eig <- eigen(cov_mat)
# Eigenvalues Determine the magnitude of
print(eig$values)
variance in these directions.
# Proportion of variance explained by each eigenvalue
prop_var <- eig$values / sum(eig$values)
print(prop_var)
# Cumulative variance explained
cum_var <- cumsum(prop_var)
print(cum_var)
Applied Data Science – R programming 14
: Selecting
Principal
Components
Applied Data Science – R programming 15
# Sample data
df <- [Link](
height = c(170, 165, 180, 175, 160),
weight = c(65, 60, 80, 75, 55),
age = c(30, 25, 35, 40, 22)
)
# Standardize data
df_scaled <- scale(df)
# PCA
Variance
pca_res <- prcomp(df_scaled)
# Variance explained by each PC
var_explained <- pca_res$sdev^2 / sum(pca_res$sdev^2)
Capture
# Cumulative variance Select principal components that
cum_var <- cumsum(var_explained)
# Print cumulative variance capture the most variance (e.g.,
print(cum_var)
# Select number of PCs to reach 95% variance
95% of the total variance).
num_pcs <- which(cum_var >= 0.95)[1]
cat("Number of PCs needed to explain at least 95% variance:", num_pcs,
"\n")
Applied Data Science – R programming 16
# Sample data
df <- mtcars[, c("mpg", "disp", "hp", "wt", "qsec")]
# Standardize data
df_scaled <- scale(df)
# PCA
pca_res <- prcomp(df_scaled) Number of
# Scree plot
plot(pca_res, type = "lines", main = "Scree Plot (Elbow
Method)")
Components
# Variance explained by each PC Deciding the number of principal
var_explained <- pca_res$sdev^2 /
sum(pca_res$sdev^2) components to keep based on
cum_var <- cumsum(var_explained)
cumulative variance.
# Display variance explained
print([Link](PC = 1:length(var_explained),
Variance_Explained = var_explained,
Cumulative = cum_var)) Applied Data Science – R programming 17
Transforming
the Data
Applied Data Science – R programming 18
# Sample data
df <- mtcars[, c("mpg", "disp", "hp", "wt", "qsec")]
# Standardize data
New Feature Space
df_scaled <- scale(df)
# PCA
pca_res <- prcomp(df_scaled) Transform the original data into the new
# Variance explained feature space defined by the selected
var_explained <- pca_res$sdev^2 / sum(pca_res$sdev^2)
cum_var <- cumsum(var_explained) principal components.
# Select number of PCs to keep (e.g., first 2)
num_pcs <- 2
# Project original data onto first 2 PCs
df_pca <- [Link](pca_res$x[, 1:num_pcs])
# Rename columns for clarity
colnames(df_pca) <- paste0("PC", 1:num_pcs)
# View transformed data
head(df_pca)
9/3/20XX Applied Data Science – R programming 19
# Sample data
df <- mtcars[, c("mpg", "disp", "hp", "wt", "qsec")]
# Standardize data
df_scaled <- scale(df)
# Perform PCA
pca_res <- prcomp(df_scaled)
# Select first two principal components
df_pca <- [Link](pca_res$x[, 1:2])
Reduced
colnames(df_pca) <- c("PC1", "PC2")
# Add a grouping variable for visualization (e.g., number of
Dimensionality
cylinders)
df_pca$cyl <- [Link](mtcars$cyl)
Achieve reduced dimensionality
# Plot the first two PCs
library(ggplot2) while retaining significant
ggplot(df_pca, aes(x = PC1, y = PC2, color = cyl)) +
geom_point(size = 3, alpha = 0.7) + information.
labs(title = "PCA: 2D Visualization of mtcars Dataset",
x = "Principal Component 1",
y = "Principal Component 2",
color = "Number of Cylinders") +
theme_minimal() Applied Data Science – R programming 20
Visualization of PCA
Applied Data Science – R programming 21
library(ggplot2)
library(ggfortify)
# Use mtcars dataset as example
data(mtcars)
# Perform PCA with scaling
pca_result <- prcomp(mtcars, scale. = TRUE)
Scatter Plot
# Extract first two PCs and add grouping variable (cylinders)
pca_df <- [Link](pca_result$x[, 1:2])
pca_df$cyl <- [Link](mtcars$cyl)
# Create scatter plot of PC1 vs PC2
ggplot(pca_df, aes(x = PC1, y = PC2, color = cyl)) +
geom_point(size = 3, alpha = 0.8) + Visualize the transformed data in
labs(
title = "Scatter Plot of First Two Principal Components", the principal component space
subtitle = "Visualizing mtcars dataset in PCA space",
x = "Principal Component 1",
y = "Principal Component 2", using scatter plots.
color = "Number of Cylinders"
)+
theme_minimal()
Applied Data Science – R programming 22
# Load necessary library
library(ggplot2)
# Use mtcars dataset
data(mtcars)
# Perform PCA with scaling
pca_res <- prcomp(mtcars, scale. = TRUE)
# Calculate variance explained
var_explained <- pca_res$sdev^2 / sum(pca_res$sdev^2)
Scree Plot
# Prepare data for scree plot
scree_df <- [Link](
PC = factor(1:length(var_explained)),
VarianceExplained = var_explained
)
# Scree plot Use scree plots to show the
ggplot(scree_df, aes(x = PC, y = VarianceExplained)) +
geom_bar(stat = "identity", fill = "steelblue") +
geom_line(aes(group = 1), color = "red", size = 1) +
variance captured by each
geom_point(color = "red", size = 2) +
labs( principal component.
title = "Scree Plot: Variance Explained by Principal Components",
x = "Principal Component",
y = "Proportion of Variance Explained"
)+
theme_minimal()
Applied Data Science – R programming 23
What is Clustering?
Applied Data Science – R programming 24
# Load library
library(ggplot2)
# Simulate customer data: spending in 2 categories
[Link](123)
customer_data <- [Link](
spending_electronics = c(rnorm(50, 500, 50), rnorm(50, 150, 30), rnorm(50, 300,
40)),
Definition
spending_clothing = c(rnorm(50, 200, 30), rnorm(50, 600, 50), rnorm(50, 300, 40))
)
# Perform K-means clustering (k=3)
[Link](123) Clustering is the task of dividing a
kmeans_res <- kmeans(customer_data, centers = 3)
# Add cluster assignments dataset into groups where data points
customer_data$cluster <- factor(kmeans_res$cluster)
# Plot clusters in the same group are more similar to
ggplot(customer_data, aes(x = spending_electronics, y = spending_clothing, color =
cluster)) +
geom_point(size = 3, alpha = 0.7) + each other than to those in other
labs(
title = "Customer Segmentation Using K-Means Clustering",
x = "Spending on Electronics", groups.
y = "Spending on Clothing",
color = "Cluster"
)+
theme_minimal()
Applied Data Science – R programming 25
# Simulate gene expression data (10 genes across 6
samples)
[Link](42)
gene_data <- matrix(rnorm(60, mean = 5, sd = 2), nrow =
10, ncol = 6)
rownames(gene_data) <- paste0("Gene", 1:10)
colnames(gene_data) <- paste0("Sample", 1:6)
# Compute distance matrix
dist_matrix <- dist(gene_data)
Purpose
# Perform hierarchical clustering The purpose is to find structure in
hc <- hclust(dist_matrix)
an unlabeled dataset.
# Plot dendrogram
plot(hc, main = "Hierarchical Clustering of Genes", xlab
= "", sub = "", hang = -1)
Applied Data Science – R programming 26
Types of
Clustering
Algorithms
Applied Data Science – R programming 27
# Load library
library(ggplot2)
# Simulate customer spending data
[Link](123)
customer_data <- [Link](
electronics = c(rnorm(50, 500, 50), rnorm(50, 150, 30), rnorm(50, 300,
40)),
k-means
clothing = c(rnorm(50, 200, 30), rnorm(50, 600, 50), rnorm(50, 300, 40))
)
# Apply k-means with k=3
Clustering
[Link](123)
kmeans_res <- kmeans(customer_data, centers = 3)
# Assign clusters
customer_data$cluster <- factor(kmeans_res$cluster)
# Visualize clusters k-means Clustering: An iterative
ggplot(customer_data, aes(x = electronics, y = clothing, color = cluster))
+
geom_point(size = 3, alpha = 0.7) +
algorithm that partitions the data
labs(
title = "K-means Clustering: Customer Segmentation", into k clusters.
x = "Electronics Spending",
y = "Clothing Spending",
color = "Cluster"
)+
theme_minimal()
Applied Data Science – R programming 28
# Simulate data
Hierarchical Clustering
[Link](42)
data <- matrix(rnorm(30), nrow = 10, ncol = 3)
rownames(data) <- paste("Sample", 1:10) Hierarchical Clustering: Builds a tree
# Compute distance matrix of clusters by either merging or
dist_mat <- dist(data)
splitting existing clusters.
# Perform hierarchical clustering (agglomerative)
hc <- hclust(dist_mat, method = "complete")
# Plot dendrogram
plot(hc, main = "Hierarchical Clustering Dendrogram",
xlab = "", sub = "", hang = -1)
Applied Data Science – R programming 29
Introduction
to k-means
Clustering
9/3/20XX Applied Data Science – R programming 30
library(ggplot2)
# Simulate customer purchase data
[Link](123)
customer_data <- [Link](
purchase_category1 = c(rnorm(50, 500, 50), rnorm(50, 150, 30),
rnorm(50, 300, 40)),
Definition
purchase_category2 = c(rnorm(50, 200, 30), rnorm(50, 600, 50),
rnorm(50, 300, 40))
)
k-means clustering partitions the data
# Perform k-means clustering with k = 3
[Link](123)
kmeans_result <- kmeans(customer_data, centers = 3) into k clusters, each represented by
# Add cluster labels to data
customer_data$cluster <- factor(kmeans_result$cluster)
the mean of the data points in the
# Visualize clusters cluster.
ggplot(customer_data, aes(x = purchase_category1, y =
purchase_category2, color = cluster)) +
geom_point(size = 3, alpha = 0.7) +
labs(
title = "K-means Clustering: Customer Segmentation",
x = "Purchase Category 1",
y = "Purchase Category 2",
color = "Cluster"
)+
theme_minimal()
Applied Data Science – R programming 31
# Load libraries
library(jpeg)
library(ggplot2)
library(grid)
# Read and process image (replace with your image path)
img <- readJPEG([Link]("img", "[Link]", package="jpeg"))
# Reshape image data: convert 3D array (height x width x channels) to 2D matrix
img_data <- [Link](
r = [Link](img[,,1]),
g = [Link](img[,,2]),
Application
b = [Link](img[,,3])
)
# Apply k-means clustering to colors (e.g., k=16)
[Link](123)
k <- 16
kmeans_res <- kmeans(img_data, centers = k)
Commonly used for customer
# Replace each pixel color by its cluster centroid
compressed_img <- matrix(nrow = nrow(img), ncol = ncol(img) * 3)
for (i in 1:k) { segmentation, image compression,
cluster_indices <- which(kmeans_res$cluster == i)
compressed_img[cluster_indices, ] <- matrix(rep(kmeans_res$centers[i, ],
length(cluster_indices)), ncol=3, byrow=TRUE) etc.
}
# Note: This is a conceptual snippet; full image reconstruction requires careful
reshaping.
Applied Data Science – R programming 32
How k-means
Clustering Works
Applied Data Science – R programming 33
# Simulate customer data
[Link](42)
customer_data <- [Link](
spending_electronics = c(rnorm(50, 500, 50), rnorm(50,
150, 30), rnorm(50, 300, 40)),
spending_clothing = c(rnorm(50, 200, 30), rnorm(50,
600, 50), rnorm(50, 300, 40))
)
# Run k-means with multiple random starts (nstart = 25)
[Link](42)
Initialization
kmeans_res <- kmeans(customer_data, centers = 3, Initialize k centroids randomly.
nstart = 25)
# Output cluster centers and total within-cluster sum of
squares
print(kmeans_res$centers)
cat("Total WCSS:", kmeans_res$[Link], "\n")
Applied Data Science – R programming 34
# Simulate simple data
[Link](101)
data <- [Link](x = c(1, 2, 4, 5, 8, 9),
y = c(1, 1, 4, 5, 8, 8))
# Initialize centroids (randomly pick 2 points)
centroids <- data[sample(1:nrow(data), 2), ]
# Function to assign clusters based on nearest centroid
Iterative
assign_clusters <- function(data, centroids) {
dist_mat <- [Link](dist(rbind(centroids, data)))[1:nrow(centroids),
(nrow(centroids)+1):(nrow(centroids)+nrow(data))]
clusters <- apply(dist_mat, 2, [Link])
return(clusters)
}
# Iterate assignment and update steps
for (i in 1:10) {
clusters <- assign_clusters(data, centroids)
Process
new_centroids <- aggregate(data, by = list(cluster = clusters), FUN = mean)[, -1]
# Check for convergence
Assign data points to the nearest
if (all(round(new_centroids, 4) == round(centroids, 4))) {
cat("Converged at iteration", i, "\n")
break
centroid and update centroids until
}
centroids <- new_centroids
convergence.
}
print(centroids)
print(clusters)
Applied Data Science – R programming 35
Choosing the Value
of k
9/3/20XX Applied Data Science – R programming 36
library(ggplot2)
# Simulate customer spending data
[Link](123)
customer_data <- [Link](
spending_electronics = c(rnorm(50, 500, 50), rnorm(50, 150, 30),
rnorm(50, 300, 40)),
spending_clothing = c(rnorm(50, 200, 30), rnorm(50, 600, 50),
rnorm(50, 300, 40))
)
# Compute total within-cluster sum of squares for k = 1 to 10
wss <- sapply(1:10, function(k) {
Elbow Method
kmeans(customer_data, centers = k, nstart = 25)$[Link]
})
# Create dataframe for plotting
elbow_df <- [Link](
k = 1:10,
wss = wss The Elbow Method: Plot the sum of
)
# Plot the Elbow Method graph
squared distances and look for an
ggplot(elbow_df, aes(x = k, y = wss)) +
geom_line(color = "steelblue", size = 1) + "elbow" point.
geom_point(color = "darkred", size = 3) +
labs(
title = "Elbow Method for Optimal Number of Clusters",
x = "Number of Clusters (k)",
y = "Total Within-Cluster Sum of Squares"
)+ Applied Data Science – R programming 37
theme_minimal()
library(cluster)
library(ggplot2)
# Simulate customer spending data
[Link](123)
customer_data <- [Link](
spending_electronics = c(rnorm(50, 500, 50), rnorm(50, 150, 30), rnorm(50, 300,
40)),
spending_clothing = c(rnorm(50, 200, 30), rnorm(50, 600, 50), rnorm(50, 300, 40))
)
# Compute average silhouette width for k = 2 to 10
sil_width <- sapply(2:10, function(k) {
km <- kmeans(customer_data, centers = k, nstart = 25)
ss <- silhouette(km$cluster, dist(customer_data))
Silhouette
})
mean(ss[, 3])
# Prepare data for plotting
sil_df <- [Link](
Score
k = 2:10,
)
silhouette = sil_width Silhouette Score: Measure how
# Plot silhouette scores
ggplot(sil_df, aes(x = k, y = silhouette)) +
similar a point is to its own cluster
geom_line(color = "darkgreen", size = 1) +
geom_point(color = "orange", size = 3) +
labs(
compared to other clusters.
title = "Silhouette Analysis for Optimal Number of Clusters",
x = "Number of Clusters (k)",
y = "Average Silhouette Score"
)+
theme_minimal()
Applied Data Science – R programming 38
Introduction to
Hierarchical
Clustering
Applied Data Science – R programming 39
# Sample gene expression data (10 genes x 6 samples)
[Link](42)
gene_data <- matrix(rnorm(60, mean = 5, sd = 2), nrow =
10, ncol = 6)
rownames(gene_data) <- paste0("Gene", 1:10)
colnames(gene_data) <- paste0("Sample", 1:6)
# Compute distance matrix (Euclidean)
dist_matrix <- dist(gene_data)
Definition
# Perform hierarchical clustering (complete linkage) Hierarchical clustering builds a tree
hc <- hclust(dist_matrix, method = "complete")
of clusters by either merging or
# Plot dendrogram
plot(hc, main = "Hierarchical Clustering Dendrogram", splitting existing clusters.
xlab = "", sub = "", hang = -1)
Applied Data Science – R programming 40
# Sample data: 10 points in 2D
[Link](101)
data <- matrix(rnorm(20), nrow = 10, ncol = 2)
Managing
rownames(data) <- paste("Doc", 1:10)
# Compute distance matrix
Database
dist_mat <- dist(data)
# Perform agglomerative hierarchical clustering
Connections
(complete linkage)
hc <- hclust(dist_mat, method = "complete") Agglomerative (bottom-up) and
# Plot dendrogram divisive (top-down) clustering.
plot(hc, main = "Agglomerative Hierarchical Clustering
Dendrogram",
xlab = "", sub = "", hang = -1)
Applied Data Science – R programming 41
How
Hierarchical
Clustering
Works
Applied Data Science – R programming 42
Agglomerative
# Simulate customer purchase data
Clustering
[Link](42)
customer_data <- [Link](
electronics = c(rnorm(20, 500, 50), rnorm(20, 150, 30), rnorm(20, 300, Agglomerative Clustering: Each data
40)),
clothing = c(rnorm(20, 200, 30), rnorm(20, 600, 50), rnorm(20, 300, 40))
) point starts in its own cluster, and
# Compute distance matrix
dist_matrix <- dist(customer_data)
pairs of clusters are merged
# Perform agglomerative hierarchical clustering (complete linkage) iteratively.
hc <- hclust(dist_matrix, method = "complete")
# Plot dendrogram
plot(hc, main = "Agglomerative Hierarchical Clustering - Customer Data",
xlab = "", sub = "", hang = -1)
Applied Data Science – R programming 43
library(cluster)
Divisive
# Simulate data
[Link](42)
data <- matrix(rnorm(30), nrow = 10, ncol = 3)
rownames(data) <- paste("Species", 1:10)
Clustering
# Perform divisive hierarchical clustering Divisive Clustering: All data points
diana_res <- diana(data)
# Plot dendrogram
start in one cluster, and splits are
plot(diana_res, main = "Divisive Hierarchical Clustering Dendrogram")
performed recursively.
Applied Data Science – R programming 44
Dendrogram
Visualization
Applied Data Science – R programming 45
# Simulate data
[Link](101)
Tree Structure
data <- matrix(rnorm(20), nrow = 10, ncol = 2)
rownames(data) <- paste("Sample", 1:10)
# Compute distance matrix
dist_mat <- dist(data)
# Perform agglomerative clustering A dendrogram is a tree-like
hc <- hclust(dist_mat)
diagram that records the
# Plot dendrogram
plot(hc, main = "Dendrogram of Hierarchical Clustering",
xlab = "", sub = "", hang = -1) sequences of merges or splits.
Applied Data Science – R programming 46
# Simulate customer data
[Link](123)
customer_data <- [Link](
electronics = c(rnorm(20, 500, 50), rnorm(20, 150, 30), rnorm(20, 300,
40)),
clothing = c(rnorm(20, 200, 30), rnorm(20, 600, 50), rnorm(20, 300, 40))
)
# Compute distance and hierarchical clustering
dist_mat <- dist(customer_data)
hc <- hclust(dist_mat) Cluster
# Plot dendrogram
plot(hc, main = "Customer Segmentation Dendrogram", hang = -1)
# Cut dendrogram at height to form 3 clusters
Interpretation
clusters <- cutree(hc, k = 3)
The height of the branches
# Add clusters to data
customer_data$cluster <- factor(clusters)
indicates the distance or
# Visualize clusters
library(ggplot2)
ggplot(customer_data, aes(x = electronics, y = clothing, color = cluster))
dissimilarity between clusters.
+
geom_point(size = 3, alpha = 0.7) +
labs(title = "Customer Segmentation via Hierarchical Clustering",
x = "Electronics Spending", y = "Clothing Spending") +
theme_minimal() Applied Data Science – R programming 47
Practical Applications
of Clustering
Applied Data Science – R programming 48
library(ggplot2)
# Simulate customer purchase data
[Link](123)
customer_data <- [Link](
high_end_spending = c(rnorm(50, 1000, 200), rnorm(50, 300, 100),
rnorm(50, 600, 150)),
budget_spending = c(rnorm(50, 200, 50), rnorm(50, 800, 200),
rnorm(50, 400, 120))
) Customer
Segmentation
# Apply k-means clustering with k=3
[Link](123)
kmeans_res <- kmeans(customer_data, centers = 3, nstart = 25)
# Assign cluster labels
customer_data$cluster <- factor(kmeans_res$cluster)
Grouping customers based on
# Visualize clusters
ggplot(customer_data, aes(x = high_end_spending, y =
budget_spending, color = cluster)) +
purchasing behavior.
geom_point(size = 3, alpha = 0.7) +
labs(title = "Customer Segmentation via K-means Clustering",
x = "High-End Product Spending",
y = "Budget Product Spending",
color = "Cluster") +
theme_minimal()
Applied Data Science – R programming 49
library(tm)
library(proxy)
library(ggplot2)
# Sample documents
docs <- c(
"Machine learning is a field of artificial intelligence.",
"Deep learning advances neural networks.",
"Climate change impacts global temperatures.",
"Global warming affects sea levels.",
"Neural networks are a subset of machine learning.",
Document
"Sea levels rise due to climate change."
)
# Create a text corpus
corpus <- VCorpus(VectorSource(docs))
# Preprocess text: convert to lower, remove punctuation, stopwords, stemming
corpus_clean <- tm_map(corpus, content_transformer(tolower))
corpus_clean <- tm_map(corpus_clean, removePunctuation)
corpus_clean <- tm_map(corpus_clean, removeWords, stopwords("english"))
Clustering
corpus_clean <- tm_map(corpus_clean, stemDocument)
# Create Document-Term Matrix (TF-IDF weighting)
Organizing documents into topics
dtm <- DocumentTermMatrix(corpus_clean, control = list(weighting = weightTfIdf))
# Convert DTM to matrix
based on content similarity.
dtm_mat <- [Link](dtm)
# Compute cosine distance matrix
dist_mat <- dist(dtm_mat, method = "cosine")
# Perform hierarchical clustering
hc <- hclust(dist_mat, method = "complete")
# Plot dendrogram Applied Data Science – R programming 50
plot(hc, main = "Document Clustering Dendrogram")
Evaluating Clustering
Results
9/3/20XX Applied Data Science – R programming 51
library(cluster)
# Simulate data
[Link](42)
Internal
data <- rbind(
matrix(rnorm(50, mean = 0), ncol = 2),
matrix(rnorm(50, mean = 5), ncol = 2)
)
# Perform k-means clustering with k=2
km <- kmeans(data, centers = 2, nstart = 25) Evaluation
# Compute silhouette scores
sil <- silhouette(km$cluster, dist(data))
Metrics such as cohesion and
# Average silhouette width
avg_sil_width <- mean(sil[, 3]) separation to evaluate the quality
cat("Average Silhouette Score:", avg_sil_width, "\n")
# Plot silhouette
of clusters.
plot(sil, main = "Silhouette Plot for k=2")
Applied Data Science – R programming 52
External
library(mclust) # Provides adjustedRandIndex()
# Simulate data with true labels
[Link](123)
Evaluation
true_labels <- rep(1:3, each = 50)
data <- rbind(
matrix(rnorm(100, mean = 0), ncol = 2),
matrix(rnorm(100, mean = 5), ncol = 2),
matrix(rnorm(100, mean = 10), ncol = 2)
)
Comparing the clustering results to
# Apply k-means clustering
kmeans_res <- kmeans(data, centers = 3, nstart = 25) ground truth labels using metrics
# Calculate ARI between predicted clusters and true labels
ari <- adjustedRandIndex(kmeans_res$cluster, true_labels) like Adjusted Rand Index.
cat("Adjusted Rand Index:", ari, "\n")
Applied Data Science – R programming 53
Thank you
• [Link]
• Ghayoumi@[Link]
9/3/20XX Applied Data Science – R programming 54