【发布时间】:2021-03-26 14:45:08
【问题描述】:
我正在做一些 kmeans 聚类分析。示例:
library(tidyverse)
library(foreach)
my_diamonds <- diamonds %>% select_if(is.numeric) %>% scale %>% as.data.frame
try_centers <- seq(from = 3, to = 12, by = 1)
wss_list <- foreach(k = try_centers) %do% { # forget parallel processing with this size of data not enough ram
print(k) # progress bar
hw = kmeans(my_diamonds, centers = k, iter.max = 20, nstart = 3, algorithm = 'Hartigan-Wong')
}
wss <- lapply(wss_list, function(i) i$tot.withinss) %>% unlist()
plot(try_centers, wss)
但我想与其他两种 kmeans 算法进行比较。试过了:
wss_list <- foreach(k = try_centers) %do% { # forget parallel processing with this size of data not enough ram
print(k) # progress bar
hw = kmeans(my_diamonds, centers = k, iter.max = 20, nstart = 3, algorithm = 'Hartigan-Wong')
lloyd = kmeans(my_diamonds, centers = k, iter.max = 20, nstart = 3, algorithm = 'Lloyd')
mac = kmeans(my_diamonds, centers = k, iter.max = 20, nstart = 3, algorithm = 'MacQueen')
}
wss <- lapply(wss_list, function(i) i$tot.withinss) %>% unlist()
plot(try_centers, wss)
这确实返回了一个情节,但我在看哪一个?!马皇后?劳埃德?
我如何构造它以在每次迭代中使用三种算法运行 kmeans,然后在 3 种算法中的每一种上绘制一个图表?
【问题讨论】:
-
您需要返回一个
list(hw, lloyd, mac)
标签: r