集群中最具代表性的实例

发布于 2024-08-18 10:10:31 字数 965 浏览 6 评论 0原文

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

我现在想要的是每个集群的代表性实例。我试图找到距集群质心具有最小欧氏距离的实例(并对每个集群重复该过程)

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

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))
}

After performing a cluster analysis to my dataset (a dataframe named data.matrix), I added a new column, named cluster, at the end (col 27) containing the cluster name that each instance belongs to.

What I want now, is a representative instance from each cluster. I tried to find the instance having the smallest euclidean distance from the cluster's centroid (and repeat the procedure for each one of my clusters)

This is what I did. Can you think of other -perhaps more elegant- ways? (assume numeric columns with no nulls).

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))
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筑梦 2024-08-25 10:10:31

起初我现在不知道你的距离公式是否正确。我认为应该有 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)
}))

At first I don't now if you distance formula is alright. I think there should be sqrt(sum((x-cent)^2)) or sum(abs(x-cent)). I assumed first.
Second thought is that just printing solution is not good idea. So I first compute, then print.
Third - I recommend using plyr but I give both (with and without plyr) solutions.

# 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)
}))
玩物 2024-08-25 10:10:31

您对“k 均值聚类”感兴趣吗?如果是这样,则每次迭代时计算质心的方式如下:

  1. 选择 ak 值(一个整数,
    指定簇的数量
    除您的数据集);

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

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

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

  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)

Is the technique you're interested in 'k-means clustering'? If so, here's how the centroids are calculated at each iteration:

  1. choose a k value (an integer that
    specifies the number of clusters to
    divide your data set);

  2. random select k rows from your data
    set, those are the centroids for the
    1st iteration;

  3. calculate the distance that each
    data point is from each centroid;

  4. each data point has a 'closest
    centroid', that determines its
    'group';

  5. calculate the mean for each
    group--those are the new centroids;

  6. back to step 3 (stopping criterion
    is usually based on comparison with
    the respective centroid values in
    successive loops, i.e., if they
    values change not more than 0.01%,
    then quit).

Those steps in code:

# 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文