K-Means Clustering Tutorial for Data Scientists | Towards Data Science

IntroductionTutorialSummaryReferences
Introduction
Tutorial
Summary
References
Customer segmentation is widely used by both Data Scientists and Machine Learning Engineers to detect data that is the most similar to one another. These are groups that are closely related to that same group, but different enough to be a new group when compared to another group. These segments can be used to identify customer groups that can be targeted for a marketing campaign through an email perhaps, for example. The ultimate goal is to identify groups of people that are in large numbers that you would have not noticed yourself, manually. This Machine Learning algorithm makes it much easier for you to group customers or something similar by features. Those features can be easily targeted as well for a business. For example, people who get coffee at 8 am at a store, might be more likely to get coffee again at that same store at 3 pm — because they already had one so early. Those people are more likely to get two coffees over someone who would get their first coffee at 1 pm. This information can be highly beneficial and tunable for a campaign in a company.
Below, I will give a Python tutorial on using unsupervised learning by means of clustering automatically. You can apply this code and concepts it to your dataset as well to follow along. Variables that will be different for you include your data of course, k itself, and the specific features you are targeting.
This tutorial will include importing some important libraries, preprocessing your data, transforming it, creating a clustering model, utilizing principal component analysis (PCA) and elbow plots, as well as ultimately grouping data together for customer segmentation. Unsupervised clustering is a facet of Machine Learning algorithms that do not have labels already, like a 0 or 1, or red-blue-green, and however, are data points that share similar attributes that could be classified as the same or similar thing. The clusters are ultimately up to you to decide on — naming wise.
The following code includes reading in your data, scaling it, and transforming it as a preprocessing step for the eventual model. There are several key libraries to import, and some that you have already most likely used like pandas and matplotlib. Here is part one of this article tutorial:
# import librariesfrom sklearn.preprocessing import StandardScalerfrom sklearn.decomposition import PCAfrom matplotlib import pyplot as pltfrom sklearn.cluster import KMeansfrom sklearn import preprocessingimport pandas as pdimport numpy as np# read in your datadata = pd.read_csv(‘your_file_path.csv’)# scale datascaler = preprocessing.StandardScaler()scaled_df = scaler.fit_transform(data)data = pd.DataFrame(scaled_df)pca = PCA(n_components=2)# transform datadata = pca.fit_transform(data)scaled = scaler.fit_transform(data)data = pd.DataFrame(scaled)
This next part of the tutorial is determining the best amount of k for the k-means Machine Learning algorithm, which is determined by the elbow plot method. You will essentially want to look at where the graphed line breaks significantly — or where it looks like the elbow, and plot it. You will also assign that best k amount to the k-means algorithm parameter. In this case, we see that 3 is the best amount of k for our data determined by the elbow plot method. Sometimes this is less clear and you can use the silhouette coefficient method instead to find the peak point which, for that plot, would be the k value.
# create an elbow plot to determine k (where the elbow occurs/line bends)n_cluster = range(1, 7) kmeans = [KMeans(n_clusters=i).fit(data) for i in n_cluster] scores = [kmeans[i].score(data) for i in range(len(kmeans))] fig, ax = plt.subplots() ax.plot(n_cluster, scores) plt.show()
The next part of the tutorial is to use the k-means clustering algorithm for the clusters for the new data. You will also see the value counts of the respective clusters. I’m using 4 instead for this graph to show what 4 would look like. You can use 3 or whatever your specific data shows you in your elbow plot — or you can plot any amount of clusters if that’s what a business stakeholder is requesting. You can gain a sense of your general groupings. Finally, you will plot the clusters after colorizing them.
# predict clusters with your k# k = however many clusters you determined from your elbow plot or silhouette methoddata['cluster'] = kmeans[k].predict(data)data['principal_feature1'] = data[0]data['principal_feature2'] = data[1]# plot the clusters and their respective amounts of datadata['cluster'].value_counts()# plot the different clusters with the 2 main PCA features fig, ax = plt.subplots() colors = {0:'red', 1:'blue', 2:'green', 3:'pink'} ax.scatter(data['principal_feature1'], data['principal_feature2'], c=data["cluster"].apply(lambda x: colors[x]))plt.show()
As an example, the pink group would be people who have high feature 1, but low feature 2, while the green cluster would be people who have high feature 2, and medium level feature 1. Features could be something to describe a group of people like age or orders per customer, for example. Therefore, the pink group would be people who are older and order less frequently — meaning you would create a marketing campaign for older ages to encourage them to order more since they are the group that has the most opportunity to order more. In this case, you would save money from spending less on ads for younger people, and optimize to spend the extra money on older ages. You can apply this concept to pretty much any features that you are working with in your data, and of course, make sure it makes sense to use those features in your business use case (meaningful features that you can promote, increase, or decrease, for example).
As you can see, with the use of Machine Learning algorithms, you can create a Data Science model that can be saved and used to group data for customer segmentation. Product enhancements can be optimized from different features, for example, and you can target those specific groups in a way that makes sense for the data and business. There are several use cases for using customer segmentation. Data Science only makes it easier to segment, and these use cases can benefit from an algorithmic way of detecting similar groups:
customer segmentationgrouping for ad campaignscustomer behavior
I hope you found my article both interesting and useful. Feel free to comment down below on how you use customer segmentation, and what steps you take to perform it as a Data Scientist or Machine Learning Engineer. Thank you for reading!
~ Changed original article from anomaly detection to customer segmentation to highlight clustering better. ~
[1] Photo by Will Myers on Unsplash, (2018)
[2] Photo by Markus Winkler on Unsplash, (2020)
[3] M.Przybyla, elbow plot screenshot, (2020)
[4] M.Przybyla, clustering screenshot, (2020)