clustering 3D array in R

2019-08-27 05:02发布

问题:

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

How can I go about this? I tried just plotting with scatterplot3d but it did not work.

回答1:

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

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.