2013-05-15 42 views
0

在R中使用sparcl包在R中执行稀疏分级集群时,我无法获得数据的集群标签。在帮助文档,它们具有以下代码:如何获取R中HierarchicalSparseCluster()中的集群标签

# Generate 2-class data 
set.seed(1) 
x <- matrix(rnorm(100*50),ncol=50) 
y <- c(rep(1,50),rep(2,50)) 
x[y==1,1:25] <- x[y==1,1:25]+2 

# Do tuning parameter selection for sparse hierarchical clustering 
perm.out <- HierarchicalSparseCluster.permute(x, wbounds=c(1.5,2:6), nperms = 5) 

# Perform sparse hierarchical clustering 
sparsehc <- HierarchicalSparseCluster(dists=perm.out$dists, 
wbound=perm.out$bestw, method="complete") 

现在,我怎么从对象sparsehc到簇标签是我的问题?

对于Kmeans,我们创建了一个简单的属性“cs”。例如。

## Choosing tuning parameters 
km.perm <- KMeansSparseCluster.permute(data_mat, K = 10, wbounds= seq(3,7, len = 
20),  nperms=5) 

## Performing kmean sparce clustring 
sparse_data_clus <- KMeansSparseCluster(data_mat, K = 10, wbounds= km.perm$bestw) 
clusterlabel <- sparse_data_clus[[1]]$Cs 

我怎样才能HierarchicalSparseCluster()得到类似的标签?

谢谢!

回答

1

分层聚类通常会返回一个树状图(即一个簇的层次结构,底层的单个元素和顶层的完整数据集),而不是严格的分区。

如果你想要一个严格的分区(例如由常规的k-means生成),你将不得不从这个分层结构中提取这样的分区。有很多方法可以这样做,最简单的方法是使用阈值级别。

由于我不使用R很多(太慢),我不能在这里给你详细的说明。看看?cutree

0

对此做出回应有点迟,但我遇到了同样的问题。下面是我工作:

set.seed(1) 
x <- matrix(rnorm(100*50),ncol=50) 
y <- c(rep(1,50),rep(2,50)) 
x[y==1,1:25] <- x[y==1,1:25]+2 
data_mat <- x 

请在矩阵的置换您对结果

hier.sparse <- HierarchicalSparseCluster(dists=hier.perm$dists, 
             wbound=hier.perm$bestw, 
             method='complete') 

运行hclust的价值$u从上一行创建

hier.perm <- HierarchicalSparseCluster.permute(data_mat, 
               wbounds= seq(3,7, len = 20), 
               nperms=5) 

运行HierarchicalSparse,然后使用cutree将其分开,如您所愿

cluster = hclust(dist(hier.sparse$u))  
cutree(cluster,3)