如何将使用 k-medoids 算法(例如 PAM)的聚类解决方案应用于另一个数据集?

How to apply the clustering solution using k-medoids algorithm (for example PAM) to another dataset?

我正在寻找一种方法,将来自 k-medoids 算法(我正在使用 PAM)的聚类解决方案从一个样本应用到另一个样本。 我认为这可以用于 k-means 算法:对于 data1,从聚类结果中获取质心;然后在 data2 中,对于每个观察,计算到每个质心的距离,然后将每个观察分配给它最近的质心。通过这样做,我们将聚类解决方案从 data1 应用到 data2。 但是,k-medoids 算法(例如 PAM)使用 medoids 作为聚类中心而不是均值。在这种情况下,我不清楚如何将聚类解决方案从一个样本应用到另一个样本。 谁能帮忙回答一下这个问题? 非常感谢!

聚类仍然按到中心的距离分配,除了 k-medoids,中心实际上是数据集中的一个数据点。请参阅下面 R 中的代码:

library(ClusterR)
library(ggplot2)
set.seed(100)
# we use the iris data set, split into 2
a = sample(nrow(iris),90)
data_b = iris[-a,1:4]
data_a = iris[a,1:4]

#perform k medoids
cm = Cluster_Medoids(data_a,clusters=3)

你可以看到中心点是数据点:

cm$medoids
    Sepal.Length Sepal.Width Petal.Length Petal.Width
95           5.6         2.7          4.2         1.3
12           4.8         3.4          1.6         0.2
111          6.5         3.2          5.1         2.0

我们继续预测:

pm = predict_Medoids(data_b,MEDOIDS=cm$medoids)

我们可以从第一个数据集计算中心点之间的距离,并将第二个数据集分配给聚类:

M = as.matrix(dist(rbind(cm$medoids,data_b)))
labs = sapply(4:nrow(M),function(i)which.min(M[i,1:3]))

我们检查你可以看到,手动计算的集群与 clusterR 中实现的一致:

table(pm$clusters==labs)

TRUE 
  60 

我们可以想象一下:

PCA = prcomp(rbind(data_a,data_b))$x
plotdf = data.frame(PCA[,1:2],
label=c(cm$clusters,pm$clusters),
dataset=rep(c("train","pred"),c(nrow(data_a),nrow(data_b)))
)

ggplot(plotdf,aes(x=PC1,y=PC2,col=factor(label),shape=dataset)) + 
geom_point() + scale_color_brewer(palette="Paired") + theme_bw()