集群中最具代表性的实例
对我的数据集(名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
起初我现在不知道你的距离公式是否正确。我认为应该有
sqrt(sum((x-cent)^2))
或sum(abs(x-cent))
。我首先假设。第二个想法是,仅仅打印解决方案并不是一个好主意。所以我先计算,然后打印。
第三-我建议使用plyr,但我给出了两种(有和没有plyr)解决方案。
At first I don't now if you distance formula is alright. I think there should be
sqrt(sum((x-cent)^2))
orsum(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.
您对“k 均值聚类”感兴趣吗?如果是这样,则每次迭代时计算质心的方式如下:
选择 ak 值(一个整数,
指定簇的数量
除您的数据集);
从数据中随机选择 k 行
集,这些是质心
第一次迭代;
计算每个点的距离
数据点距每个质心;
每个数据点都有一个“最接近的”
质心',决定了它的
'group';
计算每个的平均值
组——这些是新的质心;
返回步骤 3(停止标准
通常基于与
各自的质心值
连续的循环,即,如果它们
值变化不超过0.01%,
然后退出)。
代码中的这些步骤:
Is the technique you're interested in 'k-means clustering'? If so, here's how the centroids are calculated at each iteration:
choose a k value (an integer that
specifies the number of clusters to
divide your data set);
random select k rows from your data
set, those are the centroids for the
1st iteration;
calculate the distance that each
data point is from each centroid;
each data point has a 'closest
centroid', that determines its
'group';
calculate the mean for each
group--those are the new centroids;
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: