March 2019
Beginner to intermediate
464 pages
10h 57m
English
Quite often, we do not know what the best number of clusters to use is when building k-means clustering models. As discussed in an earlier section of this chapter, we can use the silhouette coefficient to determine what the best number of clusters is to split the data. In R, you can use the silhouette function in the cluster library to calculate the silhouette score and measure the quality of clusters. Take a look at the following code:
# Selecting the best number of clusterlibrary(cluster)for(n_cluster in 4:8){ cluster <- kmeans(normalizedDF[c("TotalSales", "OrderCount", "AvgOrderValue")], n_cluster) silhouetteScore <- mean( silhouette( cluster$cluster, dist(normalizedDF[c("TotalSales", "OrderCount", ...Read now
Unlock full access