rself-organizing-maps

add clusters and nodes from SOMbrero package to training data


I am playing a bit with the SOMbrero package. I would like to attach the cluster numbers created like so (taken from here):

my.sc <- superClass(iris.som, k=3)

and X and Y coordinates of the SOM nodes to the training dataset.

In some code, where I use the kohonen package, I create clusters like this:

range01 <- function(x){(x-min(x))/(max(x)-min(x))}

ind <- sapply(SubsetData, is.numeric)
SubsetData[ind] <- lapply(SubsetData[ind], range01)

TrainingMatrix <- as.matrix(SubsetData)

GridDefinition <- somgrid(xdim = 4, ydim = 4, topo = "rectangular", toroidal = FALSE)

SomModel <- som(
    data = TrainingMatrix,
    grid = GridDefinition,
    rlen = 10000,
    alpha = c(0.05, 0.01),
    keep.data = TRUE
)

nb <- table(SomModel$unit.classif)
groups = 5
tree.hc = cutree(hclust(d=dist(SomModel$codes[[1]]),method="ward.D2",members=nb),groups)

plot(SomModel, type="codes", bgcol=rainbow(groups)[tree.hc])

add.cluster.boundaries(SomModel, tree.hc)
result <- OrginalData
result$Cluster <- tree.hc[SomModel$unit.classif]
result$X <- SomModel$grid$pts[SomModel$unit.classif,"x"]
result$Y <- SomModel$grid$pts[SomModel$unit.classif,"y"]

write.table(result, file = "FinalData.csv", sep = ",", col.names = NA, quote = FALSE)

PS:

Some example code using the iris dataset can be found here.

PPS:

I played a bit with the code iris code quoted above and think I have managed to extract the clusters, node ids and prototypes (see code below). What is missing are the coordinates X and Y. I think they are in here:

iris.som$parameters$the.grid$coord

Code:

library(SOMbrero)

set.seed(100)
setwd("D:\\RProjects\Clustering")

#iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(10,10), maxit=100000, scaling="unitvar", radius.type="gaussian")
iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(3,3), maxit=100000, scaling="unitvar", radius.type="gaussian")

# perform a hierarchical clustering
## with 3 super clusters
iris.sc <- superClass(iris.som, k=3)
summary(iris.sc)

# compute the projection quality indicators
quality(iris.som)

iris1 <- iris
iris1$Cluster = iris.sc$cluster[iris.sc$som$clustering]
iris1$Node = iris.sc$som$clustering
iris1$Pt1Sepal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,1]
iris1$Pt2Sepal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,2]
iris1$Pt3Petal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,3]
iris1$Pt4Petal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,4]

write.table(iris1, file = "Iris.csv", sep = ",", col.names = NA, quote = FALSE)

Solution

  • I think I have figured it out using the iris example (please correct/improve code! - I am not fluent in R):

    library(SOMbrero)
    
    set.seed(100)
    setwd("D:\\RProjects\\SomBreroClustering")
    
    iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(5,5), maxit=10000, scaling="unitvar", radius.type="letremy")
    
    # perform a hierarchical clustering
    # with 3 super clusters
    iris.sc <- superClass(iris.som, k=3)
    summary(iris.sc)
    
    # compute the projection quality indicators
    quality(iris.som)
    
    iris1 <- iris
    iris1$Cluster = iris.sc$cluster[iris.sc$som$clustering]
    iris1$Node = iris.sc$som$clustering
    iris1$Pt1Sepal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,1]
    iris1$Pt2Sepal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,2]
    iris1$Pt3Petal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,3]
    iris1$Pt4Petal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,4]
    iris1$X = iris.som$parameters$the.grid$coord[iris.sc$som$clustering,1]
    iris1$Y = iris.som$parameters$the.grid$coord[iris.sc$som$clustering,2]
    
    write.table(iris1, file = "Iris.csv", sep = ",", col.names = NA, quote = FALSE)