Cluster Analysis With Iris Data Set
By: Ahmed Yahya Khaled
Tags: Machine Learning, Data Science, Artificial Intelligence, K Means Clustering, Clustering

This article is about hands-on Cluster Analysis (an Unsupervised Machine Learning) in R with the popular ‘Iris’ data set.

Let’s brush up some concepts from Wikipedia

Machine learning is the study of computer algorithms that improve automatically through experience. It is seen as a subset of Artificial Intelligence. Machine learning algorithms build a mathematical model based on sample data, in order to make predictions or decisions without being explicitly programmed to do so. Supervised Learning is the machine learning task of learning a function that maps an input to an output based on example input-output pairs. It infers a function from labeled training data consisting of a set of training examples. Unsupervised Learning is a type of machine learning that looks for previously undetected patterns in a data set with no pre-existing labels and with a minimum of human supervision. Cluster Analysis or Clustering is the task of grouping a set of objects in such a way that objects in the same group (called a cluster) are more similar (in some sense) to each other than to those in other groups (clusters).

About Iris Data set

Iris Flower (google image)

Iris flower data set was introduced by the British statistician and biologist Ronald Fisher in his 1936 paper The use of multiple measurements in taxonomic problems. This is perhaps the best known database to be found in the pattern recognition literature. Iris data set gives the measurements in centimetres of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

Image from techgig.com

So, let’s start now ! You may like to download the Iris Data set & the R-script from my github repository.

Hope you have R & RStudio installed for a hands-on experience with me :)

Objective

The Objective is to segment the iris data(without labels) into clusters — 1, 2 & 3 by k-means clustering & compare these clusters with the actual species clusters — setosa, versicolor, and virginica.

Install and Load R Packages

‘tidyverse’, ‘cluster’ and ‘reshape2’ — these three R packages are required here. Install if not done earlier. We need to load the packages with the library function.

install.packages(“tidyverse”) # for data work & visualization

install.packages(“cluster”) # for cluster modeling install.packages("reshape2") # for melting data

# note : not required if already installed library(tidyverse)

library(cluster)

library(reshape2)

Import the Iris Data set

We can import from disc after setting the working directory where the csv file is.

setwd(“E:/my_folder/work_folder”) mydata <- read.csv(“iris.csv”)

Or, get it from the in-build R datasets

mydata <- iris

Explore the Data set

With below functions we can check the data set before exploring

glimpse(mydata)

head(mydata)

View(mydata)

Let’s visualize the data now with ggplot2

Sepal-Length vs. Sepal-Width

ggplot(mydata)+

geom_point(aes(x = Sepal.Length, y = Sepal.Width), stroke = 2)+

facet_wrap(~ Species)+

labs(x = ‘Sepal Length’, y = ‘Sepal Width’)+

theme_bw()

Petal-Length vs. Petal-Width

ggplot(mydata)+

geom_point(aes(x = Petal.Length, y = Petal.Width), stroke = 2)+

facet_wrap(~ Species)+

labs(x = ‘Petal Length’, y = ‘Petal Width’)+

theme_bw()

Sepal-Length vs. Petal-Length

ggplot(mydata)+

geom_point(aes(x = Sepal.Length, y = Petal.Length), stroke = 2)+

facet_wrap(~ Species)+

labs(x = ‘Sepal Length’, y = ‘Petal Length’)+

theme_bw()

Sepal-Width vs. Pedal-Width

ggplot(mydata)+

geom_point(aes(x = Sepal.Width, y = Petal.Width), stroke = 2)+

facet_wrap(~ Species)+

labs(x = ‘Sepal Width’, y = ‘Pedal Width’)+

theme_bw()

Box plots