K-Means in Real Life: Clustering Workout Sessions

Ready to learn Data Science? Browse Data Science Training and Certification courses developed by industry thought leaders and Experfy in Harvard Innovation Lab.

K-means clustering is a very popular unsupervised learning algorithm. In this article I want to provide a bit of background about it, and show how we could use it in an anecdotal real-life situation.


K-Means Clustering is an algorithm that, given a dataset, will identify which data points belong to each one of the clusters. It takes your data and learns how it can be grouped.

Through a series of iterations, the algorithm creates groups of data points — referred to as clusters — that have similar variance and that minimize a specific cost function: the within-cluster sum of squares.

 

Within-Cluster Sum of Squares: where C_i is a cluster and “mu_i” is the mean of the data points in cluster C_i

By using the within-cluster sum of squares as cost function, data points in the same cluster will be similar to each other, whereas data points in different clusters will have a lower level of similarity.

K-Means Clustering is part of a group of learning algorithms called unsupervised learning. In this type of learning model, there is no explicit identification of a label/class/category for each data point.

Each data point in your dataset is a vector of attributes, i.e., features without a specific label that could assign it to a specific cluster or class. The algorithm will then learn on its own how to group data points that have similar features and cluster them together.

One important detail about K-Means Clustering is that, even though it identifies which data point should part of which cluster, you’ll have to specify the parameter K, representing the total number of clusters that you want to use to “distribute” your data.


Your preferred workout is jogging and, since you’re extremely data-inclined, you make sure to keep track of your performance. So you end up compiling a dataset similar to this

It consists of the date you went jogging, the total distance run (Km), duration (Min) and the number of days since your last workout. Each row in your dataset contains the attributes or features of each workout session.

Ultimately, the question you want answer is: “How can I identify similar workout sessions?”

By identifying workout sessions that are similar to each other you can have a better understanding of your overall performance and get new ideas on how to improve.

A clustering algorithm like K-Means Clustering can help you group the data into distinct groups, guaranteeing that the data points in each group are similar to each other.

A good practice in Data Science & Analytics is to first have good understanding of your dataset before doing any analysis.

Taking an exploratory view of your dataset, you start by plotting a pair-plot, in order to get a better idea about the correlation between different features.

The plots on the diagonal correspond to the distribution of each feature, while the others are scatterplots of each pair of features.

One interesting plot is distance vs duration, which shows a linear correlation.

Whereas, when we compare distance and duration with the number of days since the last workout session, the is no evident correlation between them.


Now that you have a good idea of how your dataset looks like, it’s time to use K-Means and see how the individual sessions are grouped. You can start off by splitting your dataset into two distinct groups.

In this plot, like in the pair plot, we see the relationship between distance and workout duration, but now each data point has the colour of its cluster.

Taking a quick look at this scatter plot we can see that, for instance, all workouts under 25 minutes are in one cluster.

How do we know which K value to use?

Depending on the dataset and the problem you’re solving, you may have an idea of how you want to cluster your data. Maybe you want to see the data clustered in 3 groups, 5 groups, 10 groups…

If that’s not the case, how do we figure out which K value to use?

Like any parameter in an algorithm, it requires some experimentation, i.e., tuning.

In order to experiment with different values of K, you also need to have a deciding factor or metric, which you’re going to be using to make a decision on which value of K provides the best results.

The plot above is the result of running K-Means Clustering on the workout dataset with values of K between 2 and 43, which corresponds to the size of the dataset. Each value of K is against the respective distortion value.

Distortion, on the y-axis, corresponds to our cost function: the sum of squared difference between each data point and the centroid, i.e., the cluster centre.

As K increases the corresponding distortion value will tend to zero, because you end up having just one data point per cluster. With only one data point in per cluster, the centroid is the data point itself, so the distortion will be equal to zero.

For this dataset, the the elbow of the curve is around K= 5. For values of K greater than 5, the distortion value starts decaying steadily.

Going back to the original dataset and re-running K-Means with the new found K, you can cluster the data into 5 different groups!

At this point, the job of K-Means Clustering is done and where your data analysis work begins 

The next step requires human intervention, in order to interpret the results and obtain additional insight.

Just to recap, these are the advantages of using K-Means:

  • Easy to implement
  • Relatively fast and efficient
  • Only has one parameter to tune and you can easily see the direct impact of adjusting the value of parameter K
  • Top articles, research, podcasts, webinars and more delivered to you monthly.

  • © 2021, Experfy Inc. All rights reserved.
    Leave a Comment
    Next Post

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Big Data, Cloud & DevOps
    Cognitive Load Of Being On Call: 6 Tips To Address It

    If you’ve ever been on call, you’ve probably experienced the pain of being woken up at 4 a.m., unactionable alerts, alerts going to the wrong team, and other unfortunate events. But, there’s an aspect of being on call that is less talked about, but even more ubiquitous – the cognitive load. “Cognitive load” has perhaps

    5 MINUTES READ Continue Reading »
    Big Data, Cloud & DevOps
    How To Refine 360 Customer View With Next Generation Data Matching

    Knowing your customer in the digital age Want to know more about your customers? About their demographics, personal choices, and preferable buying journey? Who do you think is the best source for such insights? You’re right. The customer. But, in a fast-paced world, it is almost impossible to extract all relevant information about a customer

    4 MINUTES READ Continue Reading »
    Big Data, Cloud & DevOps
    3 Ways Businesses Can Use Cloud Computing To The Fullest

    Cloud computing is the anytime, anywhere delivery of IT services like compute, storage, networking, and application software over the internet to end-users. The underlying physical resources, as well as processes, are masked to the end-user, who accesses only the files and apps they want. Companies (usually) pay for only the cloud computing services they use,

    7 MINUTES READ Continue Reading »