While k-means clustering is fast and easy to use, it requires k to be the input at the beginning. Therefore, we can use the sum of squares to determine which k value is best for finding the optimum number of clusters for k-means. In the following recipe, we will discuss how to find the optimum number of clusters for the k-means clustering method.
In order to find the optimum number of clusters, you need to have the previous recipe completed by generating the customer
dataset.
Perform the following steps to find the optimum number of clusters for the k-means clustering:
- First, calculate the within sum of squares (
withinss
) of different numbers of clusters:
> nk = 2:10 > set.seed(22) > WSS = sapply(nk, function(k) { + kmeans(customer, centers=k)$tot.withinss + }) > WSS Output [1] 123.49224 88.07028 61.34890 48.76431 47.20813 ...