# K Means

## **Introduction**

K-means clustering is one of the simplest and popular unsupervised machine learning algorithms.

A cluster refers to a collection of data points aggregated together because of certain similarities.

&#x20;You’ll define a target number _k_, which refers to the number of centroids you need in the dataset. A centroid is the imaginary or real location representing the center of the cluster.

Every data point is allocated to each of the clusters through reducing the in-cluster sum of squares.

&#x20;The _‘means’_ in the K-means refers to averaging of the data; that is, finding the centroid.

**There is an algorithm that tries to minimize the distance of the points in a cluster with their centroid – the k-means clustering technique.**

&#x20;**The main objective of the K-Means algorithm is to minimize the sum of distances between the points and their respective cluster centroid.**

## **Steps**

Let’s now take an example to understand how K-Means actually works:

[![k-means clustering](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-21-43.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-21-43.png)\
We have these 8 points and we want to apply k-means to create clusters for these points. Here’s how we can do it.

#### Step 1: Choose the number of clusters _k_

The first step in k-means is to pick the number of clusters, k.

#### Step 2: Select k random points from the data as centroids

Next, we randomly select the centroid for each cluster. Let’s say we want to have 2 clusters, so k is equal to 2 here. We then randomly select the centroid:

[![random cluster centroids](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-23-55.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-23-55.png)



#### Step 3: Assign all the points to the closest cluster centroid

Once we have initialized the centroids, we assign each point to the closest cluster centroid:

[![Clusters](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-24-35.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-24-35.png)

Here you can see that the points which are closer to the red point are assigned to the red cluster whereas the points which are closer to the green point are assigned to the green cluster.

#### Step 4: Recompute the centroids of newly formed clusters

Now, once we have assigned all of the points to either cluster, the next step is to compute the centroids of newly formed clusters:

[![new cluster centroids](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-26-59.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-26-59.png)

Here, the red and green crosses are the new centroids.

#### Step 5: Repeat steps 3 and 4

We then repeat steps 3 and 4:

[![clustering](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-28-14.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-09-12-28-14.png)

_The step of computing the centroid and assigning all the points to the cluster based on their distance from the centroid is a single iteration_. But wait – when should we stop this process? It can’t run till eternity, right?

Here, the red and green circles represent the centroid for these clusters.



K-Means is really just the EM (Expectation Maximization) algorithm applied to a particular naive bayes model.



## There are essentially three stopping criteria that can be adopted to stop the K-means algorithm:

1. Centroids of newly formed clusters do not change
2. Points remain in the same cluster
3. Maximum number of iterations are reached

## Applications of Clustering in Real-World Scenarios

Clustering is a widely used technique in the industry. It is actually being used in almost every domain, ranging from banking to recommendation engines, document clustering to image segmentation.

#### Customer Segmentation

We covered this earlier – one of the most common applications of clustering is customer segmentation. And it isn’t just limited to banking. This strategy is across functions, including telecom, e-commerce, sports, advertising, sales, etc.

#### Document Clustering

This is another common application of clustering. Let’s say you have multiple documents and you need to cluster similar documents together. Clustering helps us group these documents such that similar documents are in the same clusters.

#### [![document clustering](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-02-44.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-02-44.png) Image Segmentation

We can also use clustering to perform image segmentation. Here, we try to club similar pixels in the image together. We can apply clustering to create clusters having similar pixels in the same group.

[![image segmentation using clustering](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-03-22.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-03-22.png)

You can refer to [this article](https://www.analyticsvidhya.com/blog/2019/04/introduction-image-segmentation-techniques-python/?utm_source=blog\&utm_medium=comprehensive-guide-k-means-clustering) to see how we can make use of clustering for image segmentation tasks.

#### Recommendation Engines

Clustering can also be used in [recommendation engines](https://www.analyticsvidhya.com/blog/2018/06/comprehensive-guide-recommendation-engine-python/?utm_source=blog\&utm_medium=comprehensive-guide-k-means-clustering). Let’s say you want to recommend songs to your friends. You can look at the songs liked by that person and then use clustering to find similar songs and finally recommend the most similar songs.

[![recommendation clustering](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-05-03.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-05-03.png)

There are many more applications which I’m sure you have already thought of. You can share these applications in the comments section below. Next, let’s look at how we can evaluate our clusters.

## Understanding the Different Evaluation Metrics for Clustering

### Inertia

Inertia actually calculates the sum of distances of all the points within a cluster from the centroid of that cluster.

![](https://54486267-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ5sRvnbi6ybKs7Ho3A%2F-MJUtbttVgrDY9ZxdFuS%2F-MJUuXqIiwOvyZH3YgvN%2Fimage.png?alt=media\&token=66833ecd-e251-4a30-8363-500be3e9dd8a)

&#x20;We calculate this for all the clusters and the final inertial value is the sum of all these distances. This distance within the clusters is known as **intracluster distance**. So, inertia gives us the sum of intracluster distances

&#x20;The distance between them should be as low as possible.

### Dunn Index

Along with the distance between the centroid and points, **the Dunn index also takes into account the distance between two clusters**. This distance between the centroids of two different clusters is known as **inter-cluster distance**. Let’s look at the formula of the Dunn index:

![](https://54486267-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ5sRvnbi6ybKs7Ho3A%2F-MJUtbttVgrDY9ZxdFuS%2F-MJUufve-u6pDBhvvIm6%2Fimage.png?alt=media\&token=a4c9f7a2-739f-4fff-8719-e4af71042775)

[![Dunn index](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-37-22.png)](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-37-22.png)

Dunn index is the ratio of the minimum of inter-cluster distances and maximum of intracluster distances.

We want to maximize the Dunn index. The more the value of the Dunn index, the better will be the clusters. Let’s understand the intuition behind Dunn index:

\
In order to maximize the value of the Dunn index, the numerator should be maximum. Here, we are taking the minimum of the inter-cluster distances. So, the distance between even the closest clusters should be more which will eventually make sure that the clusters are far away from each other.

![minimum of inter cluster distance](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-39-19.png)

![maximum of intra cluster distance](https://cdn.analyticsvidhya.com/wp-content/uploads/2019/08/Screenshot-from-2019-08-08-15-39-34.png)

Also, the denominator should be minimum to maximize the Dunn index. Here, we are taking the maximum of intracluster distances. Again, the intuition is the same here. The maximum distance between the cluster centroids and the points should be minimum which will eventually make sure that the clusters are compact.

## How to Choose the Right Number of Clusters in K-Means Clustering?

&#x20;The maximum possible number of clusters will be equal to the number of observations in the dataset.

One thing we can do is plot a graph, also known as an **elbow curve**, where the x-axis will represent the number of clusters and the y-axis will be an evaluation metric.

![](https://54486267-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ5sRvnbi6ybKs7Ho3A%2F-MJUtbttVgrDY9ZxdFuS%2F-MJUwU_VRqq49Qux5GAo%2Fimage.png?alt=media\&token=988ba574-f20f-4779-a026-c2dbceb418bf)

The cluster value where this decrease in inertia value becomes constant can be chosen as the right cluster value for our data.

