-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClustering.R
More file actions
27 lines (19 loc) · 866 Bytes
/
Clustering.R
File metadata and controls
27 lines (19 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Install and load the ggplot2 package
install.packages("ggplot2")
library(ggplot2)
# Load the iris dataset
data(iris)
# Display the first 6 rows of the iris dataset
head(iris)
# Create a scatter plot using ggplot2 to visualize Petal.Length vs. Petal.Width with color indicating Species
ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) + geom_point()
# Perform k-means clustering on the Petal.Length and Petal.Width columns with 3 clusters
irisCluster <- kmeans(iris[, 3:4], 3, nstart = 20)
# Print information about the clustering results
print(irisCluster)
# Create a confusion matrix to compare the clustering results with actual species labels
conf_matrix <- table(irisCluster$cluster, iris$Species)
# Calculate accuracy based on the confusion matrix
accuracy = sum((diag(conf_matrix)))/sum(conf_matrix)
# Print the accuracy
print(accuracy)