title: “KMeans and KNN” author: “Leah - CDA - Lab9” date: “March 21st, 2023” — #============================================================================= EXPLORATORY DATA ANALYSIS
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
data(iris)
View(iris)
sapply(iris[, -5], var)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 0.6856935 0.1899794 3.1162779 0.5810063
ggplot(iris, aes(x = Petal.Length,
y = Petal.Width,
col = Species)) +
geom_point()
#============================================================================= SOME MORE ON CLUSTER
set.seed(123)
irisCluster <- kmeans(iris[, 1:4], center = 3,
nstart = 1)
irisCluster
## K-means clustering with 3 clusters of sizes 50, 62, 38
##
## Cluster means:
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.006000 3.428000 1.462000 0.246000
## 2 5.901613 2.748387 4.393548 1.433871
## 3 6.850000 3.073684 5.742105 2.071053
##
## 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
## [38] 1 1 1 1 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
## [75] 2 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 3 2 3 3 3 3 2 3 3 3 3
## [112] 3 3 2 2 3 3 3 3 2 3 2 3 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
## [149] 3 2
##
## Within cluster sum of squares by cluster:
## [1] 15.15100 39.82097 23.87947
## (between_SS / total_SS = 88.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
table(irisCluster$cluster, iris$Species)
##
## setosa versicolor virginica
## 1 50 0 0
## 2 0 48 14
## 3 0 2 36
library(cluster)
## Warning: package 'cluster' was built under R version 4.1.3
clusplot(iris, irisCluster$cluster,
color = T, shade = T,
labels = 0, lines = 0)
#============================================================================= CLUSTERING HANDOUT & SELF WORK
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
install.packages("ClusterR", , repos = "http://cran.us.r-project.org")
## package 'ClusterR' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\DELL\AppData\Local\Temp\Rtmpszr4WO\downloaded_packages
install.packages("cluster", , repos = "http://cran.us.r-project.org")
## Warning: package 'cluster' is in use and will not be installed
library(ClusterR)
## Warning: package 'ClusterR' was built under R version 4.1.3
library(cluster)
iris_1 <- iris[, -5]
set.seed(124)
kmeans.re <- kmeans(iris_1, centers = 3,
nstart = 20)
kmeans.re
## K-means clustering with 3 clusters of sizes 62, 50, 38
##
## Cluster means:
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.901613 2.748387 4.393548 1.433871
## 2 5.006000 3.428000 1.462000 0.246000
## 3 6.850000 3.073684 5.742105 2.071053
##
## Clustering vector:
## [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [75] 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 3 3 3 1 3 3 3 3
## [112] 3 3 1 1 3 3 3 3 1 3 1 3 1 3 3 1 1 3 3 3 3 3 1 3 3 3 3 1 3 3 3 1 3 3 3 1 3
## [149] 3 1
##
## Within cluster sum of squares by cluster:
## [1] 39.82097 15.15100 23.87947
## (between_SS / total_SS = 88.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
kmeans.re$cluster
## [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [38] 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [75] 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 3 3 3 1 3 3 3 3
## [112] 3 3 1 1 3 3 3 3 1 3 1 3 1 3 3 1 1 3 3 3 3 3 1 3 3 3 3 1 3 3 3 1 3 3 3 1 3
## [149] 3 1
cm <- table(iris$Species, kmeans.re$cluster)
cm
##
## 1 2 3
## setosa 0 50 0
## versicolor 48 0 2
## virginica 14 0 36
plot(iris_1[c("Sepal.Length", "Sepal.Width")])
plot(iris_1[c("Sepal.Length", "Sepal.Width")],
col = kmeans.re$cluster)
plot(iris_1[c("Sepal.Length", "Sepal.Width")],
col = kmeans.re$cluster,
main = "K-Means with 3 clusters")
kmeans.re$centers[, c("Sepal.Length", "Sepal.Width")]
## Sepal.Length Sepal.Width
## 1 5.901613 2.748387
## 2 5.006000 3.428000
## 3 6.850000 3.073684
points(kmeans.re$centers[, c("Sepal.Length", "Sepal.Width")],
col = 1:3, pch = 8, cex = 3)
y_kmeans <- kmeans.re$cluster
clusplot(iris_1[, c("Sepal.Length", "Sepal.Width")],
y_kmeans,
lines = 0, shade = T, color = T,
labels = 2, plotchar = F,
span = T, main = paste("Cluster iris"),
xlab = "Sepal.Length",
ylab = "Sepal.Width")
#============================================================================= KNN
set.seed(125)
rand_num <- sample(1:nrow(iris), nrow(iris)*0.9)
rand_num
## [1] 30 72 24 121 9 99 84 109 139 87 13 58 118 17 127 61 122 35
## [19] 42 134 19 40 93 36 44 59 112 66 70 131 16 105 142 57 91 108
## [37] 20 73 69 102 97 101 81 106 103 48 4 126 49 15 71 54 140 130
## [55] 148 89 75 111 67 133 38 90 23 47 39 7 86 137 52 43 41 132
## [73] 123 8 31 94 51 107 11 63 95 150 22 68 138 113 65 14 77 96
## [91] 2 80 6 147 60 117 110 141 146 145 21 136 62 88 56 28 34 10
## [109] 26 82 144 120 149 18 37 12 5 98 78 55 1 92 79 25 143 45
## [127] 74 129 85 115 32 33 124 100 119
norm_func <- function(x) {
return ((x - min(x))/(max(x) - min(x)))
}
iris_2 <- as.data.frame(lapply(iris[, -5], norm_func))
iris_2
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 0.22222222 0.62500000 0.06779661 0.04166667
## 2 0.16666667 0.41666667 0.06779661 0.04166667
## 3 0.11111111 0.50000000 0.05084746 0.04166667
## 4 0.08333333 0.45833333 0.08474576 0.04166667
## 5 0.19444444 0.66666667 0.06779661 0.04166667
## 6 0.30555556 0.79166667 0.11864407 0.12500000
## 7 0.08333333 0.58333333 0.06779661 0.08333333
## 8 0.19444444 0.58333333 0.08474576 0.04166667
## 9 0.02777778 0.37500000 0.06779661 0.04166667
## 10 0.16666667 0.45833333 0.08474576 0.00000000
## 11 0.30555556 0.70833333 0.08474576 0.04166667
## 12 0.13888889 0.58333333 0.10169492 0.04166667
## 13 0.13888889 0.41666667 0.06779661 0.00000000
## 14 0.00000000 0.41666667 0.01694915 0.00000000
## 15 0.41666667 0.83333333 0.03389831 0.04166667
## 16 0.38888889 1.00000000 0.08474576 0.12500000
## 17 0.30555556 0.79166667 0.05084746 0.12500000
## 18 0.22222222 0.62500000 0.06779661 0.08333333
## 19 0.38888889 0.75000000 0.11864407 0.08333333
## 20 0.22222222 0.75000000 0.08474576 0.08333333
## 21 0.30555556 0.58333333 0.11864407 0.04166667
## 22 0.22222222 0.70833333 0.08474576 0.12500000
## 23 0.08333333 0.66666667 0.00000000 0.04166667
## 24 0.22222222 0.54166667 0.11864407 0.16666667
## 25 0.13888889 0.58333333 0.15254237 0.04166667
## 26 0.19444444 0.41666667 0.10169492 0.04166667
## 27 0.19444444 0.58333333 0.10169492 0.12500000
## 28 0.25000000 0.62500000 0.08474576 0.04166667
## 29 0.25000000 0.58333333 0.06779661 0.04166667
## 30 0.11111111 0.50000000 0.10169492 0.04166667
## 31 0.13888889 0.45833333 0.10169492 0.04166667
## 32 0.30555556 0.58333333 0.08474576 0.12500000
## 33 0.25000000 0.87500000 0.08474576 0.00000000
## 34 0.33333333 0.91666667 0.06779661 0.04166667
## 35 0.16666667 0.45833333 0.08474576 0.04166667
## 36 0.19444444 0.50000000 0.03389831 0.04166667
## 37 0.33333333 0.62500000 0.05084746 0.04166667
## 38 0.16666667 0.66666667 0.06779661 0.00000000
## 39 0.02777778 0.41666667 0.05084746 0.04166667
## 40 0.22222222 0.58333333 0.08474576 0.04166667
## 41 0.19444444 0.62500000 0.05084746 0.08333333
## 42 0.05555556 0.12500000 0.05084746 0.08333333
## 43 0.02777778 0.50000000 0.05084746 0.04166667
## 44 0.19444444 0.62500000 0.10169492 0.20833333
## 45 0.22222222 0.75000000 0.15254237 0.12500000
## 46 0.13888889 0.41666667 0.06779661 0.08333333
## 47 0.22222222 0.75000000 0.10169492 0.04166667
## 48 0.08333333 0.50000000 0.06779661 0.04166667
## 49 0.27777778 0.70833333 0.08474576 0.04166667
## 50 0.19444444 0.54166667 0.06779661 0.04166667
## 51 0.75000000 0.50000000 0.62711864 0.54166667
## 52 0.58333333 0.50000000 0.59322034 0.58333333
## 53 0.72222222 0.45833333 0.66101695 0.58333333
## 54 0.33333333 0.12500000 0.50847458 0.50000000
## 55 0.61111111 0.33333333 0.61016949 0.58333333
## 56 0.38888889 0.33333333 0.59322034 0.50000000
## 57 0.55555556 0.54166667 0.62711864 0.62500000
## 58 0.16666667 0.16666667 0.38983051 0.37500000
## 59 0.63888889 0.37500000 0.61016949 0.50000000
## 60 0.25000000 0.29166667 0.49152542 0.54166667
## 61 0.19444444 0.00000000 0.42372881 0.37500000
## 62 0.44444444 0.41666667 0.54237288 0.58333333
## 63 0.47222222 0.08333333 0.50847458 0.37500000
## 64 0.50000000 0.37500000 0.62711864 0.54166667
## 65 0.36111111 0.37500000 0.44067797 0.50000000
## 66 0.66666667 0.45833333 0.57627119 0.54166667
## 67 0.36111111 0.41666667 0.59322034 0.58333333
## 68 0.41666667 0.29166667 0.52542373 0.37500000
## 69 0.52777778 0.08333333 0.59322034 0.58333333
## 70 0.36111111 0.20833333 0.49152542 0.41666667
## 71 0.44444444 0.50000000 0.64406780 0.70833333
## 72 0.50000000 0.33333333 0.50847458 0.50000000
## 73 0.55555556 0.20833333 0.66101695 0.58333333
## 74 0.50000000 0.33333333 0.62711864 0.45833333
## 75 0.58333333 0.37500000 0.55932203 0.50000000
## 76 0.63888889 0.41666667 0.57627119 0.54166667
## 77 0.69444444 0.33333333 0.64406780 0.54166667
## 78 0.66666667 0.41666667 0.67796610 0.66666667
## 79 0.47222222 0.37500000 0.59322034 0.58333333
## 80 0.38888889 0.25000000 0.42372881 0.37500000
## 81 0.33333333 0.16666667 0.47457627 0.41666667
## 82 0.33333333 0.16666667 0.45762712 0.37500000
## 83 0.41666667 0.29166667 0.49152542 0.45833333
## 84 0.47222222 0.29166667 0.69491525 0.62500000
## 85 0.30555556 0.41666667 0.59322034 0.58333333
## 86 0.47222222 0.58333333 0.59322034 0.62500000
## 87 0.66666667 0.45833333 0.62711864 0.58333333
## 88 0.55555556 0.12500000 0.57627119 0.50000000
## 89 0.36111111 0.41666667 0.52542373 0.50000000
## 90 0.33333333 0.20833333 0.50847458 0.50000000
## 91 0.33333333 0.25000000 0.57627119 0.45833333
## 92 0.50000000 0.41666667 0.61016949 0.54166667
## 93 0.41666667 0.25000000 0.50847458 0.45833333
## 94 0.19444444 0.12500000 0.38983051 0.37500000
## 95 0.36111111 0.29166667 0.54237288 0.50000000
## 96 0.38888889 0.41666667 0.54237288 0.45833333
## 97 0.38888889 0.37500000 0.54237288 0.50000000
## 98 0.52777778 0.37500000 0.55932203 0.50000000
## 99 0.22222222 0.20833333 0.33898305 0.41666667
## 100 0.38888889 0.33333333 0.52542373 0.50000000
## 101 0.55555556 0.54166667 0.84745763 1.00000000
## 102 0.41666667 0.29166667 0.69491525 0.75000000
## 103 0.77777778 0.41666667 0.83050847 0.83333333
## 104 0.55555556 0.37500000 0.77966102 0.70833333
## 105 0.61111111 0.41666667 0.81355932 0.87500000
## 106 0.91666667 0.41666667 0.94915254 0.83333333
## 107 0.16666667 0.20833333 0.59322034 0.66666667
## 108 0.83333333 0.37500000 0.89830508 0.70833333
## 109 0.66666667 0.20833333 0.81355932 0.70833333
## 110 0.80555556 0.66666667 0.86440678 1.00000000
## 111 0.61111111 0.50000000 0.69491525 0.79166667
## 112 0.58333333 0.29166667 0.72881356 0.75000000
## 113 0.69444444 0.41666667 0.76271186 0.83333333
## 114 0.38888889 0.20833333 0.67796610 0.79166667
## 115 0.41666667 0.33333333 0.69491525 0.95833333
## 116 0.58333333 0.50000000 0.72881356 0.91666667
## 117 0.61111111 0.41666667 0.76271186 0.70833333
## 118 0.94444444 0.75000000 0.96610169 0.87500000
## 119 0.94444444 0.25000000 1.00000000 0.91666667
## 120 0.47222222 0.08333333 0.67796610 0.58333333
## 121 0.72222222 0.50000000 0.79661017 0.91666667
## 122 0.36111111 0.33333333 0.66101695 0.79166667
## 123 0.94444444 0.33333333 0.96610169 0.79166667
## 124 0.55555556 0.29166667 0.66101695 0.70833333
## 125 0.66666667 0.54166667 0.79661017 0.83333333
## 126 0.80555556 0.50000000 0.84745763 0.70833333
## 127 0.52777778 0.33333333 0.64406780 0.70833333
## 128 0.50000000 0.41666667 0.66101695 0.70833333
## 129 0.58333333 0.33333333 0.77966102 0.83333333
## 130 0.80555556 0.41666667 0.81355932 0.62500000
## 131 0.86111111 0.33333333 0.86440678 0.75000000
## 132 1.00000000 0.75000000 0.91525424 0.79166667
## 133 0.58333333 0.33333333 0.77966102 0.87500000
## 134 0.55555556 0.33333333 0.69491525 0.58333333
## 135 0.50000000 0.25000000 0.77966102 0.54166667
## 136 0.94444444 0.41666667 0.86440678 0.91666667
## 137 0.55555556 0.58333333 0.77966102 0.95833333
## 138 0.58333333 0.45833333 0.76271186 0.70833333
## 139 0.47222222 0.41666667 0.64406780 0.70833333
## 140 0.72222222 0.45833333 0.74576271 0.83333333
## 141 0.66666667 0.45833333 0.77966102 0.95833333
## 142 0.72222222 0.45833333 0.69491525 0.91666667
## 143 0.41666667 0.29166667 0.69491525 0.75000000
## 144 0.69444444 0.50000000 0.83050847 0.91666667
## 145 0.66666667 0.54166667 0.79661017 1.00000000
## 146 0.66666667 0.41666667 0.71186441 0.91666667
## 147 0.55555556 0.20833333 0.67796610 0.75000000
## 148 0.61111111 0.41666667 0.71186441 0.79166667
## 149 0.52777778 0.58333333 0.74576271 0.91666667
## 150 0.44444444 0.41666667 0.69491525 0.70833333
summary(iris_2)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.00000
## 1st Qu.:0.2222 1st Qu.:0.3333 1st Qu.:0.1017 1st Qu.:0.08333
## Median :0.4167 Median :0.4167 Median :0.5678 Median :0.50000
## Mean :0.4287 Mean :0.4406 Mean :0.4675 Mean :0.45806
## 3rd Qu.:0.5833 3rd Qu.:0.5417 3rd Qu.:0.6949 3rd Qu.:0.70833
## Max. :1.0000 Max. :1.0000 Max. :1.0000 Max. :1.00000
iris_train <- iris_2[rand_num, ]
iris_test <- iris_2[-rand_num, ]
iris_target_category <- iris[rand_num, 5]
iris_test_category <- iris[-rand_num, 5]
library(class)
pr <- knn(iris_train, iris_test,
cl = iris_target_category, k = 13)
conf_mat_table <- table(pr, iris_test_category)
conf_mat_table
## iris_test_category
## pr setosa versicolor virginica
## setosa 5 0 0
## versicolor 0 4 1
## virginica 0 0 5
accuracy <- function(x) {
sum(diag(x) / (sum(rowSums(x)))) * 100
}
print(paste("Accuracy of KNN model",
"on Iris Dataset is: ",
accuracy(conf_mat_table),
"%"))
## [1] "Accuracy of KNN model on Iris Dataset is: 93.3333333333333 %"