【问题标题】:PAM Clustering - Use the results in another data-setPAM 聚类 - 在另一个数据集中使用结果
【发布时间】:2017-05-08 18:08:49
【问题描述】:

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

解决问题的另一种方法是,考虑到 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.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

现在,y 应该归属/关联到哪个集群/中心点?

y<-c(1.5,4.5)

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

【问题讨论】:

  • 您可以计算从中位数到 y 的距离以及哪个距离更小。 Y 将属于该集群。
  • 您不需要用于which.min 和距离计算的库。 只需自己编写一行代码!

标签: r machine-learning cluster-analysis data-mining pam


【解决方案1】:

一般对 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

【讨论】:

  • 请注意,此代码仅是欧几里得距离 - 但您不会将 pam 与欧几里得距离一起使用。
  • @Anony-Mousse 我们可以使用任何距离函数来代替欧几里得。
猜你喜欢
  • 2020-05-09
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多