PAM聚类 – 在另一个数据集中使用结果

我已经成功地使用R语言中的cluster包的pam函数进行了基于中心点的分区聚类,现在,我想使用这些结果将新的观测值归属到之前定义的聚类/中心点中。

换句话说,给定pam函数找到的k个聚类/中心点,哪个与初始数据集中未包含的额外观测值更接近?

x<-matrix(c(1,1.2,0.9,2.3,2,1.8,            3.2,4,3.1,3.9,3,4.4),6,2)x     [,1] [,2][1,]  1.0  3.2[2,]  1.2  4.0[3,]  0.9  3.1[4,]  2.3  3.9[5,]  2.0  3.0[6,]  1.8  4.4pam(x,2)

观测值1、3和5为一组,观测值2、4和6为一组,观测值1和6是中心点:

Medoids:     ID        [1,]  1 1.0 3.2[2,]  6 1.8 4.4Clustering vector:[1] 1 2 1 2 1 2

现在,新的观测值y应该归属到哪个聚类/中心点?

y<-c(1.5,4.5)

哦,如果你有多个解决方案,计算时间在我的大数据集中是很重要的。


回答:

尝试以下方法以处理一般情况下的k个聚类:

k <- 2 # pam with k clustersres <- pam(x,k)y <- c(1.5,4.5) # new point# get the cluster centroid to which the new point is to be assigned to# break ties by taking the first medoid in case there are multiple ones# non-vectorized functionget.cluster1 <- function(res, y) which.min(sapply(1:k, function(i) sum((res$medoids[i,]-y)^2)))# vectorized function, much fasterget.cluster2 <- function(res, y) which.min(colSums((t(res$medoids)-y)^2))get.cluster1(res, y)#[1] 2get.cluster2(res, y)#[1] 2# comparing the two implementations (the vectorized function takes much les s time)library(microbenchmark)microbenchmark(get.cluster1(res, y), get.cluster2(res, y))#Unit: microseconds#                 expr    min     lq     mean median     uq     max neval cld# get.cluster1(res, y) 31.219 32.075 34.89718 32.930 33.358 135.995   100   b# get.cluster2(res, y) 17.107 17.962 19.12527 18.817 19.245  41.483   100  a 

扩展到任意距离函数:

# distance functioneuclidean.func <- function(x, y) sqrt(sum((x-y)^2))manhattan.func <- function(x, y) sum(abs(x-y))get.cluster3 <- function(res, y, dist.func=euclidean.func) which.min(sapply(1:k, function(i) dist.func(res$medoids[i,], y)))get.cluster3(res, y) # use Euclidean as default#[1] 2get.cluster3(res, y, manhattan.func) # use Manhattan distance#[1] 2

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注