I downloaded this wholesale customer dataset from UCI Machine Learning Repository. The data set refers to clients of a wholesale distributor. It includes the annual spending in monetary units on diverse product categories.
My goal today is to use various clustering techniques to segment customers. Clustering is an unsupervised learning algorithm that tries to cluster data based on their similarity. Thus, there is no outcome to be predicted, and the algorithm just tries to find patterns in the data.
This is the head and structure of the original data
K-Means Clustering
Prepare the data for analysis. Remove the missing value and remove “Channel” and “Region” columns because they are not useful for clustering.
Standardize the variables.
Determine the number of clusters.
The correct choice of k is often ambiguous, but from the above plot, I am going to try my cluster analysis with 6 clusters .
Fit the model and print out the cluster means.
Plotting the results.
Interpretation of the results: With my analysis, more than 70% of information about the multivariate data is captured by this plot of component 1 and 2.
Outlier detection with K-Means
First, the data are partitioned into k groups by assigning them to the closest cluster centers, as follows:
Then calculate the distance between each object and its cluster center, then pick those with largest distances as outliers and print out outliers’ IDs.
These are the outliers. Let me make it more meaningful.
Much better!
Hierarchical Clustering
First draw a sample of 40 records from the customer data, so that the clustering plot will not be over crowded. Same as before, variables “Region” and “Channel” are removed from the data. After that, I apply hierarchical clustering to the data.
There are a wide range of hierarchical clustering methods, I heard Ward’s method is a good appraoch, so try it out.
Let me try to interpret: At the bottom, I start with 40 data points, each assigned to separate clusters, two closest clusters are then merged till I have just one cluster at the top. The height in the dendrogram at which two clusters are merged represents the distance between two clusters in the data space. The decision of the number of clusters that can best depict different groups can be chosen by observing the dendrogram.
The End
I reviewed K Means clustering and Hierarchical Clustering. As we have seen, from using clusters we can understand the portfolio in a better way. We can then build targeted strategy using the profiles of each cluster.
The source code can be found here. I am happy to hear any feedback and questions.