K-Means Clustering

Abstract

-Means is an *unsupervised* learning algorithm, meaning that you don’t have the label. The purpose of the algorithm is to partition the data into ( can be set to any number) different groups so you can find some unknown structure and learn what they belong to. First, you start off with clusters randomly placed. Then you assign each data point to the cluster it’s closest to. You then take the average of each cluster and move each centroid (center of each cluster) to their averages. This process continues until convergence, successfully grouping the data and finding structure.

Once Upon A Time

Let’s say you are an evil overlord and you want to use your mind controlling device to manipulate all of America. Your device plays a loud sound and when people hear it, they become controlled by you. The further away someone is from your device, the weaker the signal. You have 5 copies of it. The most optimal way for you to control everyone is to have all 5 devices spread out across the country. If all the devices are near each other, then only a small fraction of people will be controlled by a strong signal and a majority of people around the country will be controlled by a weak signal.

So, you randomly place all 5 devices near the middle of the country (initialization step). Each device activates and everyone (data points) in the country is controlled by the device closest to them (assigning to cluster). This is not optimal because people who live on the east and west coast are controlled by a weak signal which could possibly jeopardize your evil plan. So, for each device, we find the location of where it’s average signal strength is at (mean of cluster). We then relocate each device to that location (placing centroid). We then activate the devices again, having everyone controlled by the device closest to them.

We repeat these steps until convergence. Once we reach convergence, we have the devices spread out across the people of the country, efficiently controlling all people with solid signals.

Upon further investigation, we see that the devices have also grouped *specific* type of people. Each device has grouped people who all have properties in common, without us looking for it. We see that the people in device green, blue, and orange are mostly affiliated with the democratic party and the people controlled by device purple and red are mostly affiliated with the republican party. We have found a way to group similar people together without knowing what to look for.

This is precisely what clustering is set out to do. Find structure, determining the intrinsic grouping in a set of unlabeled data.

K-Means Clustering

Most of the algorithms I will discuss are *supervised* learning algorithms, meaning that they are presented with a label during the training phase. You take the data, with the label, and train your model to recognize patterns in the data that will lead to their respective labels. That way, when you have new data, your model will be able to classify/predict what label/class the new data belongs to. K-Means clustering, however, is an *unsupervised* algorithm, meaning there is *no* label in the training data. The purpose is to find hidden patterns or groupings in the data.

For example, if you had 500 dogs and knew all of their features (size, bark level, color, etc.) but had no idea what their breeds were (no label), you could simply run a clustering algorithm that would group together all the dogs with similar features. At the end of the clustering, all the dobermans, pit bulls, and corgis would be clustered into 3 different groups, even if you didn’t know that’s what they were called.

-means clustering aims to partition observations into clusters in which each observation belongs to the cluster it’s closest to. The goal after convergence is to have the data partitioned into groups that will grant us underlying information that we didn’t already know.

Given training examples where and target cardinality , our goal is to assign each example to one of the *k* clusters .

In order to assign each example to the cluster that its closest to, we must calculate the Euclidean distance. Given two examples and , the euclidean distance between two points is given as:

So, first, we initialize *K *clusters randomly. We then calculate the euclidean distance between each example and each cluster:

We then assign each example to the cluster it is closest to.

Once we have all examples assigned to the closest cluster, we then calculate the mean of each cluster:

where is the -th cluster

We then place each centroid (center of each cluster) at their corresponding means, reassign all examples to the cluster it is now closest to, recalculate the means, and continue repeating until convergence. Our goal is to minimize our *cost function* :

as this represents the distance between each example and the mean of the cluster they belong to. Once we have this as low as possible between all examples then we have converged and found the clusters that group pieces of data together.

*Remember, all we're trying to do is find groupings within the data. If you still don't fully understand the math and/or concept, skip down to the example section to really understand the purpose.*

Algorithm

Given a set of unlabeled data and parameter , the -Means Clustering algorithm works like this:

- Randomly initialize clusters on your data.
- Repeat until convergence:
- For each cluster for all : *Calculate the euclidean distance between each example and cluster using .
- Assign each example to the cluster it is closest to.
- Calculate the mean of each cluster with , where is the -th cluster.
- Then, place the center of each cluster (centroid) at its respective mean.

Example

You have unlabeled data with two features. The two features are *stance on progressive taxes* and *stance on government controlled healthcare* (on a scale of 1-6), i.e. means the 8th person feels pretty neutral about progressive tax and strongly about government controlled healthcare.

The unlabeled data presents nothing useful to you so you run -Means to hopefully learn some structure from your data. The training data from 6 different people is presented below:

Running the -means Clustering algorithm, we want to assign data to a set of clusters. We don’t know how any of them are related (labels) but once we find a structure between them after grouping, we can then do more digging to understand our data.

Goal is to assign each example to one of the -Clusters .

So first, we initialize the clusters at randoms spots and assign each example to the closest cluster. We set , so our two clusters are:

For each example , we calculate the *euclidean distance *to each cluster and assign each example to the nearest cluster. So, for the first person who fully believes in a flat tax and that healthcare should be privatized , we get:

Therefore, person 1 will be assigned to since it’s closest to that. After doing the calculations for the rest of the examples, we see that will be assigned to and to .

Once each example is assigned to a cluster, you can then calculate the mean for each cluster with:

Then place the center of each cluster at their respective means, i.e. will be at and will be placed at .

This process repeats until convergence. Convergence could mean that the clusters no longer move, being placed at the most optimal spot, or it has reached a max number of iterations set by the user.

At convergence, our clusters have found structure within our data. After observing the data and the clusters, we see that the people that belong to are mostly republican and the people that belong to are mostly democrat. We found this out without knowing what to look for.

Now, you might be thinking, *Couldn’t you have just seen the plotting of the data and know to group them together?*. The answer to that is yes. But, when we go to more complicated data with higher dimensions, *seeing* the data won’t work and -Means still will. This simple example was just for illustration purposes.

Summary/Discussion

-Means is an *unsupervised* learning algorithm, meaning it has no labels provided and one must learn to find structure by clustering the data. -Means is easy to implement and works well when you have data centered around means. Unfortunately, there is no theoretical foundation, you need to know parameter , and similar to NN, it could also suffer from the curse of dimensionality.

In -Means, all you simply do is randomly place centroids in your data. You then assign each example to the centroid it is closest to, to form clusters. The next step is to calculate the mean of each cluster and move the corresponding centroid to its mean. These steps repeat until convergence. Other observations:

- To set to optimally cluster data, you can first initialize to a small number. Then assign each point to its nearest cluster. If the points in each cluster fit a Gaussian distribution, keep the cluster center. Otherwise, replace it with two cluster centers. Repeat these steps until no more cluster center (centroids) are created.
- Evaluating your model is quite different than supervised algorithms as you can’t simply count the number of errors. Instead, you can do internal evaluations such as seeing if the data has high intra-cluster (within/inside one cluster) similarity and low inter-cluster (between 2 or more clusters) similarity. Or external evaluations, such as using
*mutual information*or*entropy*. - You can cluster non-circular shapes by using methods such as spectral clustering, DBSCAN, or BIRCH.