The dataset for this exercise can be found here
>getwd()
>setwd("C:/Users/joyis/Downloads")
>zoo_animals<-read.csv("zoo.csv")
>str(zoo_animals)
'data.frame': 101 obs. of 18 variables:
$ animal_name: chr "aardvark" "antelope" "bass" "bear" ...
$ hair : int 1 1 0 1 1 1 1 0 0 1 ...
$ feathers : int 0 0 0 0 0 0 0 0 0 0 ...
$ eggs : int 0 0 1 0 0 0 0 1 1 0 ...
$ milk : int 1 1 0 1 1 1 1 0 0 1 ...
$ airborne : int 0 0 0 0 0 0 0 0 0 0 ...
$ aquatic : int 0 0 1 0 0 0 0 1 1 0 ...
$ predator : int 1 0 1 1 1 0 0 0 1 0 ...
$ toothed : int 1 1 1 1 1 1 1 1 1 1 ...
$ backbone : int 1 1 1 1 1 1 1 1 1 1 ...
$ breathes : int 1 1 0 1 1 1 1 0 0 1 ...
$ venomous : int 0 0 0 0 0 0 0 0 0 0 ...
$ fins : int 0 0 1 0 0 0 0 1 1 0 ...
$ legs : int 4 4 0 4 4 4 4 0 0 4 ...
$ tail : int 0 1 1 0 1 1 1 1 1 0 ...
$ domestic : int 0 0 0 0 0 0 1 1 0 1 ...
$ catsize : int 1 1 0 1 1 1 1 0 0 0 ...
$ class_type : int 1 1 4 1 1 1 1 4 4 1 ...
>head(zoo_animals)
animal_name hair feathers eggs milk airborne aquatic predator toothed backbone
1 aardvark 1 0 0 1 0 0 1 1 1
2 antelope 1 0 0 1 0 0 0 1 1
3 bass 0 0 1 0 0 1 1 1 1
4 bear 1 0 0 1 0 0 1 1 1
5 boar 1 0 0 1 0 0 1 1 1
6 buffalo 1 0 0 1 0 0 0 1 1
breathes venomous fins legs tail domestic catsize class_type
1 1 0 0 4 0 0 1 1
2 1 0 0 4 1 0 1 1
3 0 0 1 0 1 0 0 4
4 1 0 0 4 0 0 1 1
5 1 0 0 4 1 0 1 1
6 1 0 0 4 1 0 1 1
>tail(zoo_animals)
animal_name hair feathers eggs milk airborne aquatic predator toothed backbone
96 vulture 0 1 1 0 1 0 1 0 1
97 wallaby 1 0 0 1 0 0 0 1 1
98 wasp 1 0 1 0 1 0 0 0 0
99 wolf 1 0 0 1 0 0 1 1 1
100 worm 0 0 1 0 0 0 0 0 0
101 wren 0 1 1 0 1 0 0 0 1
breathes venomous fins legs tail domestic catsize class_type
96 1 0 0 2 1 0 1 2
97 1 0 0 2 1 0 1 1
98 1 1 0 6 0 0 0 6
99 1 0 0 4 1 0 1 1
100 1 0 0 0 0 0 0 7
101 1 0 0 2 1 0 0 2
>set.seed(50)# To ensure reproducible results
>km<-kmeans(zoo_animals[,2:18],7, nstart=25)# There are 7 categories of zoo animals
>table(km$cluster, zoo_animals$animal_name)
aardvark antelope bass bear boar buffalo calf carp catfish cavy cheetah chicken
1 0 0 1 0 0 0 0 1 1 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 1
5 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0
7 1 1 0 1 1 1 1 0 0 1 1 0
chub clam crab crayfish crow deer dogfish dolphin dove duck elephant flamingo
1 1 0 0 0 0 0 1 0 0 0 0 0
2 0 0 1 1 0 0 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 1 0 0 0 1 1 0 1
5 0 0 0 0 0 0 0 1 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 1 0 0 0 0 1 0
flea frog fruitbat giraffe girl gnat goat gorilla gull haddock hamster hare hawk
1 0 0 0 0 0 0 0 0 0 1 0 0 0
2 1 0 0 0 0 1 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 1 0 0 0 1
5 0 0 1 0 1 0 0 1 0 0 0 0 0
6 0 2 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 1 0 0 1 0 0 0 1 1 0
herring honeybee housefly kiwi ladybird lark leopard lion lobster lynx mink mole
1 1 0 0 0 0 0 0 0 0 0 0 0
2 0 1 1 0 1 0 0 0 1 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 1 0 1 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 1 1 0 1 1 1
mongoose moth newt octopus opossum oryx ostrich parakeet penguin pheasant pike
1 0 0 0 0 0 0 0 0 0 0 1
2 0 1 0 1 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 1 1 1 1 0
5 0 0 0 0 0 0 0 0 0 0 0
6 0 0 1 0 0 0 0 0 0 0 0
7 1 0 0 0 1 1 0 0 0 0 0
piranha pitviper platypus polecat pony porpoise puma pussycat raccoon reindeer
1 1 1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0
7 0 0 1 1 1 0 1 1 1 1
rhea scorpion seahorse seal sealion seasnake seawasp skimmer skua slowworm slug
1 0 0 1 0 0 1 0 0 0 1 0
2 0 1 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 1 0 0 0 1
4 1 0 0 0 0 0 0 1 1 0 0
5 0 0 0 1 1 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0
sole sparrow squirrel starfish stingray swan termite toad tortoise tuatara tuna
1 1 0 0 0 1 0 0 0 0 0 1
2 0 0 0 1 0 0 1 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0
4 0 1 0 0 0 1 0 0 0 0 0
5 0 0 1 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 1 1 1 0
7 0 0 0 0 0 0 0 0 0 0 0
vampire vole vulture wallaby wasp wolf worm wren
1 0 0 0 0 0 0 0 0
2 0 0 0 0 1 0 0 0
3 0 0 0 0 0 0 1 0
4 0 0 1 0 0 0 0 1
5 1 0 0 1 0 0 0 0
6 0 0 0 0 0 0 0 0
7 0 1 0 0 0 1 0 0
>table(km$cluster, zoo_animals$class_type)
1 2 3 4 5 6 7
1 0 0 3 13 0 0 0
2 0 0 0 0 0 8 6
3 0 0 0 0 0 0 4
4 0 20 0 0 0 0 0
5 10 0 0 0 0 0 0
6 0 0 2 0 4 0 0
7 31 0 0 0 0 0 0
>points(km$centers[,2], km$centers[,3], col=1:3, pch = 8, cex=2)
>pairs(zoo_animals[,-1],col= zoo_animals[,18], pch=19)
>zoopca <- prcomp(zoo_animals[, -1])
>summary(zoopca)
Importance of components:
PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8
Standard deviation 2.312 2.0582 0.81499 0.57537 0.42812 0.38925 0.33443 0.3100
Proportion of Variance 0.467 0.3702 0.05805 0.02893 0.01602 0.01324 0.00978 0.0084
Cumulative Proportion 0.467 0.8373 0.89530 0.92423 0.94025 0.95350 0.96327 0.9717
PC9 PC10 PC11 PC12 PC13 PC14 PC15
Standard deviation 0.2831 0.25965 0.23988 0.19983 0.17648 0.16746 0.11306
Proportion of Variance 0.0070 0.00589 0.00503 0.00349 0.00272 0.00245 0.00112
Cumulative Proportion 0.9787 0.98457 0.98960 0.99309 0.99581 0.99826 0.99938
PC16 PC17
Standard deviation 0.06379 0.05535
Proportion of Variance 0.00036 0.00027
Cumulative Proportion 0.99973 1.00000
>library(cluster)
>clusplot(zoo_animals, km$cluster, shade=TRUE,
main= '2D representation of Cluster', labels=2, lines=0)

>zoo2<- dist(zoo_animals, method = "euclidean")
>hfit=hclust(zoo2, method="ward.D")
>plot(hfit)
>plot(hfit)
>grps<-cutree(hfit, k = 8)
>grps
>rect.hclust(hfit,k=8, border = "purple")
