Instantly share code, notes, and snippets.

Embed
What would you like to do?
Self-organizing map (SOM) example in R
#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)
@csetzkorn

This comment has been minimized.

csetzkorn commented May 14, 2017

Getting:

Error in dist(sommap$codes) :
(list) object cannot be coerced to type 'double'

any ideas?

@mfokaMapuma

This comment has been minimized.

mfokaMapuma commented May 19, 2017

Getting

Error in dist(sommap$codes) :
(list) object cannot be coerced to type 'double'

Any one to assist. Am a newbie in R

@Xnsam

This comment has been minimized.

Xnsam commented May 30, 2017

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

@jokergoo

This comment has been minimized.

jokergoo commented May 31, 2017

following works

som.hc <- cutree(hclust(dist(sommap$codes[[1]])), groups)
@alleztous

This comment has been minimized.

alleztous commented Jun 9, 2017

i am a little confused about how to do clustering using the result of SOM...

@mustikarizki

This comment has been minimized.

mustikarizki commented Sep 29, 2017

How can you define the number of groups to be 3?

@mazroyal

This comment has been minimized.

mazroyal commented Nov 13, 2017

som.hc <- cutree(hclust(dist(sommap$codes[[1]])), groups)
thanks. this gives no error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment