SOM地图 - R - Kohonen包 - 聚类

问题描述 投票:0回答:2

我在R中尝试使用SOM映射和聚类。我使用本教程:https://www.r-bloggers.com/self-organising-maps-for-customer-segmentation-using-r/ ... SOM映射工作正常,但是当我尝试聚类此错误时:

> mydata <- som_model$codes 
> wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) 
Error in apply(mydata, 2, var) : dim(X) must have a positive length.

我的代码:

 require(kohonen)
    data = matrix( 
    c(6, 6, 80, 280, 404, 0, 158, 158197, 158197233,
      6, 13, 85, 280, 402, 0, 160, 160197, 160197233,
      6, 13, 81, 283, 400, 0, 160, 160197, 160197233),    
     nrow=3,             
     ncol=9,              
     byrow = TRUE)    
    data_train <- data[, c(1,2,4,5,7,8,9)]
    data_train_matrix <- as.matrix(scale(data_train))
    som_grid <- somgrid(xdim = 2, ydim=1, topo="hexagonal")

    som_model <- som(data_train_matrix, 
      grid=som_grid, 
    rlen=500, 
     alpha=c(0.05,0.01), 
     keep.data = TRUE )



      #training proces
        plot(som_model, type="changes")
        #nodes 
        plot(som_model, type="count", main="Node Counts")
        #heatmap
        plot(som_model, type = "property", property = getCodes(som_model)[,4], main="Heat map - status")

mydata <- som_model$codes 
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) 
for (i in 2:15) {
  wss[i] <- sum(kmeans(mydata, centers=i)$withinss)
}
plot(wss)

    ## use hierarchical clustering to cluster the codebook vectors
    som_cluster <- cutree(hclust(dist(som_model$codes)), 6)
    # plot these results:
    plot(som_model, type="mapping", bgcol = pretty_palette[som_cluster], main = "Clusters") 
    add.cluster.boundaries(som_model, som_cluster)

它像教程一样写,所以教程如何工作,这不是吗?我是R的新人,所以我不明白这个错误。我明白矩阵可能存在问题,但问题怎么样?

r cluster-analysis som
2个回答
2
投票

您的代码有几个问题。首先,您从一个非常小的数据样本开始,在2 x 1网格上执行SOM,在som_model$codes中只输出2行,然后执行最多15个簇的kmeans。我将使用库mlbench中的Sonar数据集提供工作代码。我必须补充一点,我从未在实际数据分析中使用过kohonen库或SOM

library(mlbench)
library(kohonen)
data(Sonar) #somewhat bigger data example

data_train <- Sonar[, 1:60] #use first 60 columns

data_train_matrix <- as.matrix(scale(data_train)) #scale data

som_grid <- somgrid(xdim = 5, ydim = 5, topo = "hexagonal") #initialize a bigger grid

som_model <- som(data_train_matrix, 
                 grid = som_grid, 
                 rlen = 500, 
                 alpha = c(0.05,0.01), 
                 keep.data = TRUE )

plot(som_model, type = "changes")

enter image description here

mydata <- som_model$codes[[1]] #extract the matrix containing codebook vectors

wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) 

for (i in 2:24) { #i must be less than 5*5 the grid size defined at the begining
  wss[i] <- sum(kmeans(mydata, centers = i)$withinss)
}

plot(wss, type = "l")

enter image description here

让我们使用8个集群来削减三个:

som_cluster <- cutree(hclust(dist(mydata)), k = 8)

plot(som_model, type="mapping", bgcol = som_cluster, main = "Clusters") 
add.cluster.boundaries(som_model, som_cluster)

enter image description here


0
投票

你得到的错误是 - 我相信 - 因为som_model$codes是一个列表所以你不能在它上面使用apply,所以用mydata替换你的mydata <- som_model$codes[[1]]的定义,就像在最后添加[[1]]一样

那它应该工作

© www.soinside.com 2019 - 2024. All rights reserved.