Last active
March 11, 2023 11:21
-
-
Save dgrapov/f67d0696c4fb02731f55da3e1b9e8c4d to your computer and use it in GitHub Desktop.
Self-organizing map (SOM) example in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#SOM example using wines data set | |
library(kohonen) | |
data(wines) | |
set.seed(7) | |
#create SOM grid | |
sommap <- som(scale(wines), grid = somgrid(2, 2, "hexagonal")) | |
## use hierarchical clustering to cluster the codebook vectors | |
groups<-3 | |
som.hc <- cutree(hclust(dist(sommap$codes)), groups) | |
#plot | |
plot(sommap, type="codes", bgcol=rainbow(groups)[som.hc]) | |
#cluster boundaries | |
add.cluster.boundaries(sommap, som.hc) |
I tried this
som.hc <- cutree(hclust(dist(as.numeric(unlist(sommap$codes)))), groups)
for the error:
Error in dist(sommap$codes) :
(list) object cannot be coerced to type 'double'
and then no more error
but not sure if this is the correct way. I am a newbie too
following works
som.hc <- cutree(hclust(dist(sommap$codes[[1]])), groups)
i am a little confused about how to do clustering using the result of SOM...
How can you define the number of groups to be 3?
som.hc <- cutree(hclust(dist(sommap$codes[[1]])), groups)
thanks. this gives no error.
following works
som.hc <- cutree(hclust(dist(sommap$codes[[1]])), groups)
I follow yours suggested and it works
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting
Error in dist(sommap$codes) :
(list) object cannot be coerced to type 'double'
Any one to assist. Am a newbie in R