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

PAM Clustering - Use the results in another data-set

我已经成功地 运行 使用 pam 函数(R 中的集群包)围绕 Medoids 进行分区,现在,我想使用结果来归因对先前定义的 clusters/medoids 的新观察。

问题的另一种表述方式是,给定 k clusters/medoids 已被 pam 函数找到,哪个更接近初始数据集中没有的额外观察?

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.4
pam(x,2)

观测值 1、3 和 5,以及观测值 2、4 和 6 聚集在一起,观测值 1 和 6 是中心点:

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

现在,cluster/medoid y 应该 attributed/associated 和?

y<-c(1.5,4.5)

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

对一般的 k 个集群试试这个:

k <- 2 # pam with k clusters
res <- 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 function
get.cluster1 <- function(res, y) which.min(sapply(1:k, function(i) sum((res$medoids[i,]-y)^2)))

# vectorized function, much faster
get.cluster2 <- function(res, y) which.min(colSums((t(res$medoids)-y)^2))

get.cluster1(res, y)
#[1] 2
get.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 function
euclidean.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] 2
get.cluster3(res, y, manhattan.func) # use Manhattan distance
#[1] 2