Skip to content

Instantly share code, notes, and snippets.

@dgrapov

dgrapov/SOM example.R

Last active Nov 29, 2019
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.

Copy link

@csetzkorn 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.

Copy link

@mfokaMapuma 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.

Copy link

@Xnsam 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.

Copy link

@jokergoo jokergoo commented May 31, 2017

following works

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

This comment has been minimized.

Copy link

@alleztous 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.

Copy link

@mustikarizki mustikarizki commented Sep 29, 2017

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

@mazroyal

This comment has been minimized.

Copy link

@mazroyal mazroyal commented Nov 13, 2017

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

@liphyra

This comment has been minimized.

Copy link

@liphyra liphyra commented Jul 18, 2019

following works

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

I follow yours suggested and it works

@dgrapov

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.