Clustering Data
By: Anshul Tiwari
Tags: Clustering, K Means Clustering, Python, K Means

To put it simply, clustering data is dividing up data points into groups of data in such a manner that we segregate groups with similar traits and assign them to clusters.

Why would we do this? Well, let’s try to understand this with an example. Imagine that you are the director of a rental car company and you want to start catering your company’s fleet to customers. How would you do this? You can survey each customer individually, which would be extremely time consuming and resource heavy. How about studying the rental details of each transaction/customer and creating a strategy for each and every one of them? Absolutely not.

But… what if you’re able to cluster all of your customers into groups of three, let’s say for example business travelers, leisure travelers, and students. Now, you can tailor your company’s fleet offerings to these groups. That’s clustering. We’re grouping our business travelers into one segment, our vacationers into another segment, and so on.

Now that we know what clustering is, and why it can be helpful, let’s talk about one specific type of clustering algorithm: K-Means.

K-Means is one of the simplest and most popular unsupervised machine learning algorithms. Unsupervised machine learning will make inferences from a given set of data using only input variables, also known as independent variables (we won’t be predicting anything with K-Means).

K-Means starts with a group of randomly selected centroids, which serve as the beginning point of each cluster, and then iterates through the clusters to optimize the position of each centroid. K-Means will stop optimizing centroids when one of two things happen: the centroids have stabilized — meaning there is no change in their values because clustering was successful, or the number of iterations we have specified have been achieved.

K-Means is fast, and can run on thousands, or even millions of points of data, but there are drawbacks. Firstly, it doesn’t work very well when some of the data points are are distributed more densely than others. Secondly, it’s up to the data scientist to choose the number of clusters (k), which requires an informed, qualitative decision. Let’s go through a quick process on importing the appropriate libraries for k-means, and an example:

Step 1 (Importing Libraries):

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans

Step 2 (Generating some data to work with):

X = -2 * np.random.rand(100,2) X1 = 1 + 2 * np.random.rand(50, 2) X[50:100, :] = X1 plt.scatter(X[:, 0], X[:, 1])

In the code box above, we’re generating 100 random data points, divided into two clusters of 50 points each. Matplotlib is then plotting the data for us, below:

Step 3 (K-Means, and cluster centers):

km = KMeans(n_clusters = 2) km.fit(X)

Notice we’re choosing a k value of 2 above, as we clearly have two clusters of data.

km.cluster_centers_

Step 4 (Plotting the centroids):

plt.scatter(X[:, 0], X[:, 1]) plt.scatter(-1.14772566, -1.0781176, marker = '*', color = 'red', s = 500) plt.scatter(1.85158417, 2.02497047, marker = '*', color = 'green', s = 500) plt.show();

The code above looks complicated, but all it’s doing is plotting the original scatter points from our randomly generated data, and then plotting the centroids in the shape of a star. The coordinates for the centroids come from the km.cluster_centers_ code we ran in Step 3.

Step 5 (Testing):

km.labels_

Here’s the output of the code above:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

What we’re seeing above is the text version of our scatter plot: half of our data points are in cluster 0, and the other half are in cluster 1.

K-Means is by far the most popular unsupervised machine learning algorithm. It’s not complicated to understand, or even explain, and gives you results quickly. There are other, far more sophisticated unsupervised algorithms out there, but K-Means is perfect for beginners. Try it out!