0% found this document useful (0 votes)
6 views17 pages

K-means and K-medoids Clustering in R

The document details the calculation of new centroids for three clusters based on given points, resulting in centroids of (4, 4.6) for cluster 1, (4.143, 9.571) for cluster 2, and (10, 11.333) for cluster 3. It also includes R code for performing k-means clustering on a dataset of points and the iris dataset, as well as k-medoids clustering using the pam function. The R code includes data preparation, clustering execution, and visualization using ggplot2.

Uploaded by

goviraj098765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

K-means and K-medoids Clustering in R

The document details the calculation of new centroids for three clusters based on given points, resulting in centroids of (4, 4.6) for cluster 1, (4.143, 9.571) for cluster 2, and (10, 11.333) for cluster 3. It also includes R code for performing k-means clustering on a dataset of points and the iris dataset, as well as k-medoids clustering using the pam function. The R code includes data preparation, clustering execution, and visualization using ggplot2.

Uploaded by

goviraj098765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Calculate the new centroid for each cluster for the third iteration.

In cluster 1, 5 points i.e. A2 (2,6), A5 (6,4), A6 (1,2), A10 (7,5), and A12 (4,6).

To calculate the new centroid for cluster 1, we will find the mean of the x and y
coordinates of each point in the cluster. Hence, the new centroid for cluster 1 is (4, 4.6).

In cluster 2, we have 7 points i.e. A1 (2,10), A4 (6,9), A7 (5,10) , A8 (4,9), A13 (3,10), A14
(3,8), and A15 (6,11). Hence, the new centroid for cluster 2 is (4.143, 9.571)

In cluster 3, we have 3 points i.e. A3 (11,11), A9 (10,12), and A11 (9,11). Hence, the new
centroid for cluster 3 is (10, 11.333).

1. Write R program for kmeans clustering

A1 (2,10) A2 (2,6) A3 (11,11) A4 (6,9) A5 (6,4) A6 (1,2) A7 (5,10) A8 (4,9) A9 (10,12) A10
(7,5) A11 (9,11) A12 (4,6) A13 (3,10) A14 (3,8) A15 (6,11)

# Load necessary library

library(ggplot2)

# Create the dataset

points <- [Link](

Point = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12",
"A13", "A14", "A15"),
X = c(2, 2, 11, 6, 6, 1, 5, 4, 10, 7, 9, 4, 3, 3, 6),

Y = c(10, 6, 11, 9, 4, 2, 10, 9, 12, 5, 11, 6, 10, 8, 11)

# View the dataset

print("Dataset:")

print(points)

# Remove the 'Point' column to perform clustering on the numeric columns

coordinates <- points[, c("X", "Y")]

# Set the number of clusters (k). Let's assume k = 3 for this example.

[Link](123) # Set seed for reproducibility

k <- 3

# Perform k-means clustering

kmeans_result <- kmeans(coordinates, centers = k)

kmeans_result

# Add cluster results to the dataset

points$Cluster <- [Link](kmeans_result$cluster)

# Print the clustering result

print("Clustering result with 3 clusters:")

print(points)

# Visualize the clusters

ggplot(points, aes(x = X, y = Y, color = Cluster, label = Point)) +

geom_point(size = 4) +

geom_text(vjust = -1, hjust = 0.5) +

labs(title = "K-means Clustering", x = "X Coordinate", y = "Y Coordinate") +

theme_minimal()

> # Load necessary library


> library(ggplot2)

> # Create the dataset


> points <- [Link](
+ Point = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10",
"A11", "A12", "A13", "A14", .... [TRUNCATED]

> # View the dataset


> print("Dataset:")
[1] "Dataset:"

> print(points)
Point X Y
1 A1 2 10
2 A2 2 6
3 A3 11 11
4 A4 6 9
5 A5 6 4
6 A6 1 2
7 A7 5 10
8 A8 4 9
9 A9 10 12
10 A10 7 5
11 A11 9 11
12 A12 4 6
13 A13 3 10
14 A14 3 8
15 A15 6 11

> # Remove the 'Point' column to perform clustering on the numeric columns
> coordinates <- points[, c("X", "Y")]

> # Set the number of clusters (k). Let's assume k = 3 for this example.
> [Link](123) # Set seed for reproducibility

> k <- 3

> # Perform k-means clustering


> kmeans_result <- kmeans(coordinates, centers = k)

> kmeans_result
K-means clustering with 3 clusters of sizes 7, 3, 5

Cluster means:
X Y
1 4.142857 9.571429
2 10.000000 11.333333
3 4.000000 4.600000

Clustering vector:
[1] 1 3 2 1 3 3 1 1 2 3 2 3 1 1 1
Within cluster sum of squares by cluster:
[1] 20.571429 2.666667 37.200000
(between_SS / total_SS = 76.0 %)

Available components:

[1] "cluster" "centers" "totss" "withinss"


"[Link]"
[6] "betweenss" "size" "iter" "ifault"

> # Add cluster results to the dataset


> points$Cluster <- [Link](kmeans_result$cluster)

> # Print the clustering result


> print("Clustering result with 3 clusters:")
[1] "Clustering result with 3 clusters:"

> print(points)
Point X Y Cluster
1 A1 2 10 1
2 A2 2 6 3
3 A3 11 11 2
4 A4 6 9 1
5 A5 6 4 3
6 A6 1 2 3
7 A7 5 10 1
8 A8 4 9 1
9 A9 10 12 2
10 A10 7 5 3
11 A11 9 11 2
12 A12 4 6 3
13 A13 3 10 1
14 A14 3 8 1
15 A15 6 11 1

> # Visualize the clusters


> ggplot(points, aes(x = X, y = Y, color = Cluster, label = Point)) +
+ geom_point(size = 4) +
+ geom_text(vjust = -1, .... [TRUNCATED]
Write R program for kmeans clustering using dataset

# Load necessary library

library(datasets)

# Load the iris dataset

data("iris")

# View the first few rows of the dataset

head(iris)

# Select only numeric columns for clustering (exclude the Species column)
iris_data <- iris[, 1:4]

# Set seed for reproducibility

[Link](42)

# Perform K-means clustering with 3 clusters (since we know there are 3 species in the iris
dataset)

kmeans_result <- kmeans(iris_data, centers = 3)

# View the results

print(kmeans_result)

# Add the cluster assignment to the original data

iris$Cluster <- [Link](kmeans_result$cluster)

# Compare the clustering results with the actual species

table(iris$Cluster, iris$Species)

# Visualize the clusters using [Link] and [Link]

library(ggplot2)

# Plot [Link] vs [Link] with clusters

ggplot(iris, aes(x = [Link], y = [Link], color = Cluster, shape = Species)) +

geom_point(size = 3) +

labs(title = "K-means Clustering of Iris Dataset (Sepal Dimensions)",

x = "Sepal Length", y = "Sepal Width") +

theme_minimal()

> # Load necessary library


> library(datasets)

> # Load the iris dataset


> data("iris")

> # View the first few rows of the dataset


> head(iris)
[Link] [Link] [Link] [Link] Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa

> # Select only numeric columns for clustering (exclude the Species column)
> iris_data <- iris[, 1:4]

> # Set seed for reproducibility


> [Link](42)

> # Perform K-means clustering with 3 clusters (since we know there are 3
species in the iris dataset)
> kmeans_result <- kmeans(iris_data, centers = .... [TRUNCATED]

> # View the results


> print(kmeans_result)
K-means clustering with 3 clusters of sizes 62, 38, 50

Cluster means:
[Link] [Link] [Link] [Link]
1 5.901613 2.748387 4.393548 1.433871
2 6.850000 3.073684 5.742105 2.071053
3 5.006000 3.428000 1.462000 0.246000

Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3
[42] 3 3 3 3 3 3 3 3 3 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 1 1 1 1
[83] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2 2 2 1 1 2 2
2 2 1 2 1 2
[124] 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 1

Within cluster sum of squares by cluster:


[1] 39.82097 23.87947 15.15100
(between_SS / total_SS = 88.4 %)

Available components:

[1] "cluster" "centers" "totss" "withinss"


"[Link]"
[6] "betweenss" "size" "iter" "ifault"

> # Add the cluster assignment to the original data


> iris$Cluster <- [Link](kmeans_result$cluster)

> # Compare the clustering results with the actual species


> table(iris$Cluster, iris$Species)

setosa versicolor virginica


1 0 48 14
2 0 2 36
3 50 0 0
> # Visualize the clusters using [Link] and [Link]
> library(ggplot2)

> # Plot [Link] vs [Link] with clusters


> ggplot(iris, aes(x = [Link], y = [Link], color = Cluster,
shape = Species)) +
+ geom .... [TRUNCATED]
Write R program for kmediod clustering

A1 (2,10) A2 (2,6) A3 (11,11) A4 (6,9) A5 (6,4) A6 (1,2) A7 (5,10) A8 (4,9) A9 (10,12) A10
(7,5) A11 (9,11) A12 (4,6) A13 (3,10) A14 (3,8) A15 (6,11)

# Load necessary library

library(cluster)

# Create the dataset

points <- [Link](

Point = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12",
"A13", "A14", "A15"),

X = c(2, 2, 11, 6, 6, 1, 5, 4, 10, 7, 9, 4, 3, 3, 6),

Y = c(10, 6, 11, 9, 4, 2, 10, 9, 12, 5, 11, 6, 10, 8, 11)

# View the dataset

print("Dataset:")

print(points)

# Remove the 'Point' column to perform clustering on the numeric columns

coordinates <- points[, c("X", "Y")]

# Set the number of clusters (k). Let's assume k = 3 for this example.

k <- 3

# Perform K-medoids clustering using the pam function from the cluster package

kmedoids_result <- pam(coordinates, k)

# Add the cluster results to the dataset

points$Cluster <- [Link](kmedoids_result$clustering)

# Print the clustering result

print("Clustering result with 3 clusters:")

print(points)

# Visualize the clusters


library(ggplot2)

ggplot(points, aes(x = X, y = Y, color = Cluster, label = Point)) +

geom_point(size = 4) +

geom_text(vjust = -1, hjust = 0.5) +

labs(title = "K-medoids Clustering", x = "X Coordinate", y = "Y Coordinate") +

theme_minimal()

> # Load necessary library


> library(cluster)

> # Create the dataset


> points <- [Link](
+ Point = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11",
"A12", "A13", "A14", .... [TRUNCATED]

> # View the dataset


> print("Dataset:")
[1] "Dataset:"

> print(points)
Point X Y
1 A1 2 10
2 A2 2 6
3 A3 11 11
4 A4 6 9
5 A5 6 4
6 A6 1 2
7 A7 5 10
8 A8 4 9
9 A9 10 12
10 A10 7 5
11 A11 9 11
12 A12 4 6
13 A13 3 10
14 A14 3 8
15 A15 6 11

> # Remove the 'Point' column to perform clustering on the numeric columns
> coordinates <- points[, c("X", "Y")]

> # Set the number of clusters (k). Let's assume k = 3 for this example.
> k <- 3

> # Perform K-medoids clustering using the pam function from the cluster package
> kmedoids_result <- pam(coordinates, k)
> # Add the cluster results to the dataset
> points$Cluster <- [Link](kmedoids_result$clustering)

> # Print the clustering result


> print("Clustering result with 3 clusters:")
[1] "Clustering result with 3 clusters:"

> print(points)
Point X Y Cluster
1 A1 2 10 1
2 A2 2 6 2
3 A3 11 11 3
4 A4 6 9 1
5 A5 6 4 2
6 A6 1 2 2
7 A7 5 10 1
8 A8 4 9 1
9 A9 10 12 3
10 A10 7 5 2
11 A11 9 11 3
12 A12 4 6 2
13 A13 3 10 1
14 A14 3 8 1
15 A15 6 11 1

> # Visualize the clusters


> library(ggplot2)

> ggplot(points, aes(x = X, y = Y, color = Cluster, label = Point)) +


+ geom_point(size = 4) +
+ geom_text(vjust = -1, hjust = 0.5) +
+ labs(tit .... [TRUNCATED]

>
Write R program for kmediod clustering using dataset

# Load necessary libraries

library(cluster) # For K-medoids clustering

library(datasets) # For the iris dataset

library(ggplot2) # For visualization

# Load the iris dataset

data("iris")
# View the first few rows of the dataset

head(iris)

# Select only numeric columns for clustering (exclude the Species column)

iris_data <- iris[, 1:4]

# Set seed for reproducibility

[Link](42)

# Perform K-medoids clustering with 3 clusters (since we know there are 3 species in the iris
dataset)

kmedoids_result <- pam(iris_data, k = 3)

# Partitioning Around Medoids)

# View the results

print(kmedoids_result)

# Add the cluster assignment to the original data

iris$Cluster <- [Link](kmedoids_result$cluster)

# Compare the clustering results with the actual species

table(iris$Cluster, iris$Species)

# Visualize the clusters using [Link] and [Link]

ggplot(iris, aes(x = [Link], y = [Link], color = Cluster, shape = Species)) +

geom_point(size = 3) +

labs(title = "K-medoids Clustering of Iris Dataset (Sepal Dimensions)",

x = "Sepal Length", y = "Sepal Width") +

theme_minimal()

> # Load necessary library


> library(datasets)

> # Load the iris dataset


> data("iris")

> # View the first few rows of the dataset


> head(iris)
[Link] [Link] [Link] [Link] Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa

> # Select only numeric columns for clustering (exclude the Species column)
> iris_data <- iris[, 1:4]

> # Set seed for reproducibility


> [Link](42)

> # Perform K-means clustering with 3 clusters (since we know there are 3
species in the iris dataset)
> kmeans_result <- kmeans(iris_data, centers = .... [TRUNCATED]

> # View the results


> print(kmeans_result)
K-means clustering with 3 clusters of sizes 62, 38, 50

Cluster means:
[Link] [Link] [Link] [Link]
1 5.901613 2.748387 4.393548 1.433871
2 6.850000 3.073684 5.742105 2.071053
3 5.006000 3.428000 1.462000 0.246000

Clustering vector:
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3
[42] 3 3 3 3 3 3 3 3 3 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 1 1 1 1
[83] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2 2 2 1 1 2 2
2 2 1 2 1 2
[124] 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 1

Within cluster sum of squares by cluster:


[1] 39.82097 23.87947 15.15100
(between_SS / total_SS = 88.4 %)

Available components:

[1] "cluster" "centers" "totss" "withinss"


"[Link]"
[6] "betweenss" "size" "iter" "ifault"

> # Add the cluster assignment to the original data


> iris$Cluster <- [Link](kmeans_result$cluster)

> # Compare the clustering results with the actual species


> table(iris$Cluster, iris$Species)

setosa versicolor virginica


1 0 48 14
2 0 2 36
3 50 0 0

> # Visualize the clusters using [Link] and [Link]


> library(ggplot2)

> # Plot [Link] vs [Link] with clusters


> ggplot(iris, aes(x = [Link], y = [Link], color = Cluster,
shape = Species)) +
+ geom .... [TRUNCATED]
> source('~/.active-rstudio-document', echo=TRUE)

> # Load necessary libraries


> library(cluster) # For K-medoids clustering

> library(datasets) # For the iris dataset

> library(ggplot2) # For visualization

> # Load the iris dataset


> data("iris")

> # View the first few rows of the dataset


> head(iris)
[Link] [Link] [Link] [Link] Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa

> # Select only numeric columns for clustering (exclude the Species column)
> iris_data <- iris[, 1:4]

> # Set seed for reproducibility


> [Link](42)

> # Perform K-medoids clustering with 3 clusters (since we know there are 3
species in the iris dataset)
> kmedoids_result <- pam(iris_data, k = 3)

> # View the results


> print(kmedoids_result)
Medoids:
ID [Link] [Link] [Link] [Link]
[1,] 8 5.0 3.4 1.5 0.2
[2,] 79 6.0 2.9 4.5 1.5
[3,] 113 6.8 3.0 5.5 2.1
Clustering vector:
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1
[42] 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 3 2 2 2 2
[83] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3
3 3 2 3 2 3
[124] 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3 3 2
Objective function:
build swap
0.6709391 0.6542077

Available components:
[1] "medoids" "[Link]" "clustering" "objective" "isolation"
"clusinfo"
[7] "silinfo" "diss" "call" "data"

> # Add the cluster assignment to the original data


> iris$Cluster <- [Link](kmedoids_result$cluster)

> # Compare the clustering results with the actual species


> table(iris$Cluster, iris$Species)

setosa versicolor virginica


1 50 0 0
2 0 48 14
3 0 2 36

> # Visualize the clusters using [Link] and [Link]


> ggplot(iris, aes(x = [Link], y = [Link], color = Cluster,
shape = Species)) .... [TRUNCATED]

You might also like