You are on page 1of 1

Aim: CLUSTERING MODEL a. Clustering algorithms for unsupervised classification. b.

Plot
the cluster data using R visualizations.
Code:
# Load necessary libraries

library(ggplot2)

library(cluster)

# Generate synthetic data for clustering

set.seed(123)

data <- matrix(rnorm(2000), ncol = 2)

colnames(data) <- c("X1", "X2")

# Perform K-Means clustering

kmeans_result <- kmeans(data, centers = 3, nstart = 25) # Number of clusters = 3

# Extract cluster assignments and centroids

cluster_assignments <- kmeans_result$cluster

centroids <- kmeans_result$centers

# Plot the cluster data

cluster_data <- data.frame(data, Cluster = as.factor(cluster_assignments))

ggplot(cluster_data, aes(x = X1, y = X2, color = Cluster)) +

geom_point(size = 3) +

geom_point(data = as.data.frame(centroids), aes(x = X1, y = X2), color = "black", size = 5, shape = 3) +

scale_color_manual(values = c("blue", "green", "red")) +

theme_minimal() +

labs(title = "K-Means Clustering", color = "Cluster")

You might also like