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:

from sklearn.datasets import load_iris
import pandas as pd

iris = load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df.head()
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.

from sklearn.cluster import KMeans

# Assigning the features for our model
X = iris_df.iloc[:, [0, 1]].values

# Defining the KMeans clustering model
kmeans = KMeans(n_clusters=3, init='k-means++', max_iter=300, n_init=10, random_state=0)
y_kmeans = kmeans.fit_predict(X)

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 tol and algorithm:
    • tol is the tolerance with regard to inertia required to declare convergence. We did not include this to use the default tolerance.
    • algorithm specifies 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.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal