本文介绍了如何使用R中的k均值聚类技术获取与数据相对应的聚类编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过k均值聚类方法对数据进行聚类,如何在R中使用k均值聚类技术获取与数据相对应的聚类数?为了获得每个记录属于哪个集群.

I clustered data by k-means clustering method, how can i get cluster number correspond to data using k-means clustering techniques in R? In order to get each record belongs to which cluster.

示例12 32 13 => 1. 12,13 2. 32

推荐答案

听起来您正在尝试访问kmeans()返回的群集矢量.从群集的帮助页面:

It sounds like you are trying to access the cluster vector that is returned by kmeans(). From the help page for cluster:

A vector of integers (from 1:k) indicating the cluster to which each
point is allocated.

使用帮助页面上的示例:

Using the example on the help page:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))

#Access the cluster vector
cl$cluster

> cl$cluster
  [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [45] 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [89] 1 1 1 1 1 1 1 1 1 1 1 1

要在评论中解决问题

您可以通过以下操作将群集号映射"到原始数据:

You can "map" the cluster number to the original data by doing something like this:

out <- cbind(x, clusterNum = cl$cluster)
head(out)

               x          y clusterNum
[1,] -0.42480483 -0.2168085          2
[2,] -0.06272004  0.3641157          2
[3,]  0.08207316  0.2215622          2
[4,] -0.19539844  0.1306106          2
[5,] -0.26429056 -0.3249288          2
[6,]  0.09096253 -0.2158603          2

cbind是用于列绑定的函数,还有一个rbind用于行的函数.分别参见?cbind?rbind来查看其帮助页面.

cbind is the function for column bind, there is also an rbind function for rows. See their help pages for more details ?cbind and ?rbind respectively.

这篇关于如何使用R中的k均值聚类技术获取与数据相对应的聚类编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 01:09