R中的3D阵列聚类 [英] clustering 3D array in R
问题描述
我正在尝试对阵列中的3D数据进行聚类。它实际上是来自3D图像的信息,因此此数组代表具有x,y,z值的单个图像。我想知道什么体素倾向于与什么聚类。数组看起来像这样。
I'm trying to cluster 3D data that I have in an array. It's actually information from a 3D image so this array represents a single image with x,y,z values. I would like to know what voxel tends to cluster with what. The array looks like this.
dim(x)
[1] 34 34 34 1
我该怎么办?我试过只用scatterplot3d进行绘图,但是没有用。
How can I go about this? I tried just plotting with scatterplot3d but it did not work.
推荐答案
因此,这是对群集的尝试。
So this is an attempt at clustering. You really should provide data if you want a better answer.
library(reshape2) # for melt(...)
library(rgl) # for plot3d(...)
set.seed(1) # to create reproducible sample
# 3D matrix, values clustered around -2 and +2
m <- c(rnorm(500,-2),rnorm(500,+2))
dim(m) <- c(10,10,10)
v <- melt(m, varnames=c("x","y","z")) # 4 columns: x, y, z, value
# interactive 3D plot, coloring based on value
plot3d(v$x,v$y,v$z, col=1+round(v$value-min(v$value)),size=5)
# identify clusters
v <- scale(v) # need to scale or clustering will fail
v <- data.frame(v) # need data frame for later
d <- dist(v) # distance matrix
km <- kmeans(d,centers=2) # kmeans clustering, 2 clusters
v$clust <- km$cluster # identify clusters
# plot the clusters
plot(z[1:4],col=v$clust) # scatterplot matrix
plot3d(v$x,v$y,v$z, col=v$clust,size=5) # 3D plot, colors based in cluster
主要思想是将3D矩阵重塑为长格式,其中包含x,y,z和实际矩阵值的列。因此,现在x,y和z包含位置信息(此处为索引值1:10)。您需要对其进行缩放,以使值
列和索引列的缩放比例相同,否则聚类会给您带来误导的结果。
The main idea is to reshape your 3D matrix into "long" format with columns for x, y, z, and the actual matrix values. So now x, y, and z contain the positional information (here, the index values 1:10). You need to scale this so the value
column and the index columns are on the same scale, otherwise clustering will give you misleading results.
这篇关于R中的3D阵列聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!