【问题标题】:Most representative instance of a cluster最具代表性的集群实例
【发布时间】:2011-01-06 11:04:16
【问题描述】:

对我的数据集(名为 data.matrix 的数据框)执行聚类分析后,我在末尾添加了一个名为 cluster 的新列(第 27 列)包含每个实例所属的集群名称。

我现在想要的是来自每个集群的代表性实例。我试图找到与集群质心欧几里得距离最小的实例(并为我的每个集群重复该过程)

这就是我所做的。你能想到其他——也许更优雅——的方式吗? (假设没有空值的数字列)。

clusters <- levels(data.matrix$cluster)
cluster_col = c(27)

for (j in 1:length(clusters)) {
    # get the subset for cluster j
    data = data.matrix[data.matrix$cluster == clusters[j],]

    # remove the cluster column
    data <- data[,-cluster_col]

    # calculate the centroid
    cent <- mean(data)

    # copy data to data.matrix_cl, attaching a distance column at the end
    data.matrix_cl <- cbind(data, dist = apply(data, 1, function(x) {sqrt(sum((x - cent)^2))}))

    # get instances with min distance
    candidates <- data.matrix_cl[data.matrix_cl$dist == min(data.matrix_cl$dist),]

    # print their rownames
    print(paste("Candidates for cluster ",j))
    print(rownames(candidates))
}

【问题讨论】:

  • 看一下 R 中的 TraMineR 包。它提供了两个感兴趣的命令。 disscenter 允许使用给定的距离矩阵找到中心点的索引(到类中心距离最小的观察值)。 dissrep 提供了几种基于距离矩阵查找代表性对象(一个或多个)的方法。

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


【解决方案1】:

如果你的距离公式没问题,我现在不知道。我认为应该有sqrt(sum((x-cent)^2))sum(abs(x-cent))。我先假设。 第二个想法是仅仅打印解决方案不是一个好主意。所以我先计算,然后打印。 第三 - 我建议使用 plyr,但我同时提供(有和没有 plyr)解决方案。

# Simulated data:
n <- 100
data.matrix <- cbind(
  data.frame(matrix(runif(26*n), n, 26)),
  cluster=sample(letters[1:6], n, replace=TRUE)
)
cluster_col <- which(names(data.matrix)=="cluster")

# With plyr:
require(plyr)
candidates <- dlply(data.matrix, "cluster", function(data) {
  dists <- colSums(laply(data[, -cluster_col], function(x) (x-mean(x))^2))
  rownames(data)[dists==min(dists)]
})

l_ply(names(candidates), function(c_name, c_list=candidates[[c_name]]) {
    print(paste("Candidates for cluster ",c_name))
    print(c_list)
})

# without plyr
candidates <- tapply(
  1:nrow(data.matrix),
  data.matrix$cluster,
  function(id, data=data.matrix[id, ]) {
    dists <- rowSums(sapply(data[, -cluster_col], function(x) (x-mean(x))^2))
    rownames(data)[dists==min(dists)]
  }
)

invisible(lapply(names(candidates), function(c_name, c_list=candidates[[c_name]]) {
    print(paste("Candidates for cluster ",c_name))
    print(c_list)
}))

【讨论】:

  • 距离公式你说得对,谢谢。我会纠正它。也谢谢你的回答。
  • 我真的很喜欢这里的示例能够轻松运行。答案的结构使学习更容易。而有/无 plyr 确实显示了 plyr 如何让编写简洁的代码更容易。
【解决方案2】:

您对“k-means clustering”技术感兴趣吗?如果是这样,那么在每次迭代中计算质心的方式如下:

  1. 选择一个 k 值(一个整数 指定集群的数量 划分你的数据集);

  2. 从数据中随机选择 k 行 集,这些是质心 第一次迭代;

  3. 计算每个人的距离 数据点来自每个质心;

  4. 每个数据点都有一个“最近 质心',这决定了它的 '组';

  5. 计算每个的平均值 组——这些是新的质心;

  6. 返回步骤 3(停止标准 通常是基于比较 中的相应质心值 连续循环,即,如果它们 值变化不超过 0.01%, 然后退出)。

代码中的那些步骤:

# toy data set
mx = matrix(runif60, 10, 99), nrow=12, ncol=5, byrow=F)
cndx = sample(nrow(mx), 2)
# the two centroids at iteration 1
cn1 = mx[cndx[1],]
cn2 = mx[cndx[2],]
# to calculate Pearson similarity
fnx1 = function(a){sqrt((cn1[1] - a[1])^2 + (cn1[2] - a[2])^2)}
fnx2 = function(a){sqrt((cn2[1] - a[1])^2 + (cn2[2] - a[2])^2)}
# calculate distance matrix
dx1 = apply(mx, 1, fnx1)
dx2 = apply(mx, 1, fnx2)
dx = matrix(c(dx1, dx2), nrow=2, ncol=12)
# index for extracting the new groups from the data set
ndx = apply(dx, 1, which.min)
group1 = mx[ndx==1,]
group2 = mx[ndx==2,]
# calculate the new centroids for the next iteration
new_cnt1 = apply(group1, 2, mean)
new_cnt2 = apply(group2, 2, mean)

【讨论】:

  • 不,我不是在问如何执行 k-means 聚类。我的集群分析已经完成,并且在每个实例中都分配了它所属的集群。现在已经定义了集群,我要问的是哪个实例(来自每个集群)更接近它的集群质心(并且可以被认为是集群中实例的理想代表)
  • 这就是我给你的——特别是我的代码的最后两行。这些行中的每一行都为您提供了“集群中实例的理想代表”。我认为“理想代表”没有其他含义,除了如果您继续进行另一次迭代会计算出的集群质心的值。
  • 不,我想要的是真实实例的 id,而不是平均值。但最接近最终质心的实例。
  • 你真的不明白如何从我上面的代码中得到它? (i) 从上面的 dx1 开始(每个数据点和单个质心的距离); (ii) ndx = order(dx); (iii) mx[ndx,](这为您提供了按距该质心的距离从最近到最远的距离排列的数据点;(iv)获取最近的数据点:mx[ndx,][1,]
  • 我认为玩具数据的第一行应该是:mx = matrix(runif(60, 10, 99), nrow=12, ncol=5, byrow=F)
猜你喜欢
  • 2016-02-17
  • 2017-01-14
  • 2020-06-04
  • 2015-11-21
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-08
相关资源
最近更新 更多