[go: up one dir, main page]

0% found this document useful (0 votes)
2 views9 pages

Practical 7 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Mayur Sunil Bhagwat 248604

Practical 7

Q1)K- Means Clustering (Iris Dataset)


Step 1: Import iris Data
# Read data
data("iris")
names(iris)

Step 2: Create Subset where we are going to apply K-means Clustering


new_data<-subset(iris,select = c(-Species))
new_data

Step 3: Apply k-means using kmeans function


#kmeans(): Performs K-means clustering.

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

cl<-kmeans(new_data,3)
cl

Step 4: Create wss where we plot values


wss <- sapply(1:15, function(k){kmeans(data, k )$tot.withinss})
wss
#To visualize how WSS changes with different values of kkk and find the "elbow" (the
optimal number of clusters)
plot(1:15, wss,
type="b", pch = 19, frame = FALSE,
xlab="Number of clusters K",
ylab="Total within-clusters sum of squares")

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

Step 5: Using Library (cluster)


library(cluster)
clusplot(new_data, cl$cluster, color=TRUE, shade=TRUE, labels=2, lines=0)

Step 6: "agglomartive clustering"


cl$cluster

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

#To display the centroids of the clusters.


cl$centers

Q2) Hierarchical Clustering: Agglomerative clustering.


Step 1: Read Data
data("iris")
names(iris)

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

Step 2: To performs hierarchical clustering


clusters <- hclust(dist(iris[, 3:4]))
Step 3: To visualize the hierarchical clustering
plot(clusters)
clusterCut <- cutree(clusters, 3)
table(clusterCut, iris$Species)
install.packages("ggplot2")
library(ggplot2)

Step 4: To create a scatter plot of Petal.Length vs Petal.Width


ggplot(iris, aes(Petal.Length, Petal.Width, color = iris$Species)) +
geom_point(alpha = 0.4, size = 3.5) + geom_point(col = clusterCut) +
scale_color_manual(values = c('black', 'red', 'green'))

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

clusters <- hclust(dist(iris[, 3:4]), method = 'average')


clusterCut1 <- cutree(clusters, 3)
table(clusterCut1, iris$Species)

plot(clusters)
ggplot(iris, aes(Petal.Length, Petal.Width, color = iris$Species)) +
geom_point(alpha = 0.4, size = 3.5) + geom_point(col = clusterCut1) +
scale_color_manual(values = c('black', 'red', 'green'))

Q3)
data(iris)
# Structure
str(iris)
# Installing Packages
install.packages("ClusterR")
install.packages("cluster")

# Loading package
library(ClusterR)

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

library(cluster)

# Removing initial label of


# Species from original dataset
iris_1 <- iris[, -5]

# Fitting K-Means clustering Model


# to training dataset
set.seed(240) # Setting seed
kmeans.re <- kmeans(iris_1, centers = 3, nstart = 20)
kmeans.re

# Cluster identification for


# each observation
kmeans.re$cluster

# Confusion Matrix
cm <- table(iris$Species, kmeans.re$cluster)
cm

# Model Evaluation and visualization


plot(iris_1[c("Sepal.Length", "Sepal.Width")])
plot(iris_1[c("Sepal.Length", "Sepal.Width")],
col = kmeans.re$cluster,
main = "Clusters: Sepal Length vs Sepal Width",
xlab = "Sepal Length",
ylab = "Sepal Width",
pch = 19)
plot(iris_1[c("Sepal.Length", "Sepal.Width")],
col = kmeans.re$cluster,

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

main = "K-means with 3 clusters")

## Plotiing cluster centers


kmeans.re$centers
kmeans.re$centers[, c("Sepal.Length", "Sepal.Width")]

# cex is font size, pch is symbol


points(kmeans.re$centers[, c("Sepal.Length", "Sepal.Width")],
col = 1:3, pch = 8, cex = 3)

## Visualizing clusters
y_kmeans <- kmeans.re$cluster
clusplot(iris_1[, c("Sepal.Length", "Sepal.Width")],
y_kmeans,
lines = 0,
shade = TRUE,
color = TRUE,
labels = 2,
plotchar = FALSE,
span = TRUE,
main = paste("Cluster iris"),
xlab = "Sepal.Length",
ylab = "Sepal.Width")

Mulund College Of Commerce(Autonomous) Data Science


Mayur Sunil Bhagwat 248604

Mulund College Of Commerce(Autonomous) Data Science

You might also like