Unsupervised Learning: Hands-on with K-means Clustering
Quick Recap: Unraveling K-means Clustering
Before moving on to the practical application, let's refresh our memory with a recap of K-means clustering. K-means clustering is an integral method in unsupervised learning. The main principle of K-means clustering is quite simple: it groups data points into distinct clusters based on their mutual distances to minimize the variance, also known as inertia, within each cluster.
We will now apply K-means clustering to a well-known dataset: the Iris dataset.
Diving into the Iris Dataset Again
The Iris dataset, as we've discussed in previous lessons, consists of measurements taken from 150 iris flowers across three distinct species. Imagine being a botanist searching for a systematic way to categorize new iris flowers based on these features. Doing so manually would be burdensome; hence, resorting to machine learning, specifically K-means clustering, becomes a logical choice!
Let's load this dataset using the sklearn library in Python and convert it into a pandas DataFrame:
Implementing K-means Clustering with sklearn
We're now going to implement K-means clustering on the Iris dataset. For this, we'll use the KMeans class from sklearn's cluster module. To keep our initial implementation straightforward, let's focus on just two dataset features: sepal length and sepal width.
In the above block:
n_clusters: stipulates the number of clusters to form.init: sets the method for initialization. The"k-means++"method selects initial cluster centers intelligently to hasten convergence.max_iter: limits the maximum number of iterations for a single run.n_init: specifies the number of times the algorithm runs with different centroid seeds.- We have left out parameters like
tolandalgorithm:tolis the tolerance with regard to inertia required to declare convergence. We did not include this to use the default tolerance.algorithmspecifies the algorithm to use. The classical EM-style algorithm is"full", the Elkan variant is more efficient but is not available for sparse data. To keep things simple, we chose not to specify this option.

