Skip to content

Instantly share code, notes, and snippets.

@ibombonato
Created May 17, 2016 14:20
Show Gist options
  • Save ibombonato/dd6021d5a929d240644c5940e1bfb468 to your computer and use it in GitHub Desktop.
Save ibombonato/dd6021d5a929d240644c5940e1bfb468 to your computer and use it in GitHub Desktop.
Run Kmeans in parallel to calculate WSS
library(doParallel)
library(foreach)
my_data <- mtcars
# Within Sum of Squares
wss <- (nrow(my_data)-1)*sum(apply(my_data,2,var))
# Number of clusters
try_clusters <- seq(from = 2, to = 8, by = 1)
# Calculate the number of cores
no_cores <- detectCores() - 1
# Initiate cluster
cl <- makeCluster(no_cores)
registerDoParallel(cl)
# Calculating WSS for each cluster size in parallel
wss_results <- foreach::foreach (i = try_clusters) %dopar% {
sum(kmeans(my_data, centers=i, iter.max = 1000)$withinss)
}
# Stop Clusters
stopCluster(cl)
# Plot WSS results
plot(try_clusters, unlist(wss_results), type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment