Image by Christina Brinza on Unsplash.

Classifying Iris Species with K-Means Clustering

Implementing the K-Means Clustering Algorithm on the Iris Dataset

If you’re anything like me, you might have spent the past couple of months diving into the realm of neural networks only to submerge from the depths of deep learning and realize you’ve neglected classical machine learning by casting it aside.

Although a lot of cutting-edge artificial intelligence research puts the limelight on deep learning, classical machine learning algorithms like support vector machines and logistic regression also have a lot to offer.

In this article, I’ll walk through one of the most popular algorithms: K-Means Clustering and step through how you can implement it on one of machine learning’s “hello world!” datasets: the iconic Iris dataset.

I’ve always been a strong believer in putting the spotlight on the data. Data is the backbone to artificial intelligence and in keeping up with appearances, I’ll quickly give a short introduction to the beloved Iris dataset.

Only interested in the code? That’s okay, I’m not too offended. I’ll save you the hassle of reading through the entire article because to be honest, I’m not the biggest fan of wading through large amounts of information when I only need a specific piece of it too. Here’s the link to the Kaggle Notebook.

The Iris flower dataset is a multiclassification problem first introduced by British statistician and biologist Ronald Fisher which lies in classifying three variations of an Iris — Iris Setosa, Iris Virginica, and Iris Versicolor. The features for this dataset are the length and width of the petals and sepals and the target class of the species. (If I’ve already convinced you to give this dataset a go, you can find it here, otherwise, hang tight and give me a couple more paragraphs and you might find yourself scrolling back up.)

With that dataset knowledge, let’s begin data exploration to see what golden nuggets of information we can uncover about this dataset.

This should reveal to you a table of the first 5 values of the dataset. Take a look. You likely won’t find anything too interesting about it. Only some columns and rows of numbers that don’t have much meaning just yet.

Pretty self-explanatory! Df.shape will fill us in on how many rows and columns we’re dealing with. In this case, we have 150 rows and 5 columns.

Then, we’ll use .describe() to draw out useful information. Again, this is quite simple so I’ll drop the code for the next bit and move on to explaining the K-Means Clustering algorithm.

What is the K-Means Clustering Algorithm?

K-Means clustering is an unsupervised classical machine learning algorithm that classifies targets by clusters of aggregated datapoints resulting from certain similarities within the features. As mentioned, K-Means is an unsupervised algorithm hence the model will be trying to understand the data and draw out valuable or informative features and reveal patterns within the dataset.

How K-Means Clustering works: The K in K-Means clustering is actually a variable that stands for the number of centroids — points at the center of clusters. Beginning with initialization, K-Means calculates the distances between the data points and the number of centroids (k), and then groups each data point under the centroid that it is closest to.

Then, the centroid is recalculated, the formula for the new centroids are as follows:

sum of points grouped to centroid / # of points in group

This process continues until all data points are grouped into their respective clusters and there are no changes in the classes of any of the data points.

With the intuition behind how the K-Means Clustering Algorithm now under our belt, let’s build the model.

We can then gauge the performance of the model by comparing the predicted and actual target values.

Closing Notes

This is a pretty standard dataset. Not too interesting or too many patterns or insights to extract because it only has 4 features, none of which are too unique from the others. It was nice to be able to reacquaint myself with K-Means clustering though! I’m looking forward to implementing more classical machine learning algorithms on some other datasets.

Check out some of my other notebooks!

Lunar Rocky Landscape Segmentation with U-Net

Designing Convolutional Neural Networks with Fashion MNIST

Galaxy Multi-Image Classification with LeNet-5

Writer of technology-centric articles.