【问题标题】:R Self Organized Maps - Predict new data with fitted unsupervised SOMR 自组织地图 - 使用拟合的无监督 SOM 预测新数据
【发布时间】:2018-07-24 13:37:57
【问题描述】:

我有兴趣从无监督拟合 SOM 模型预测新数据集,但我不确定自己是否走在正确的道路上。感谢您的指导

目标: 使用先前在训练集上拟合的集群组对新数据集进行分类。

我不确定的:

一个。测试集中分配的集群组是否与训练集类似。即测试集中的第 1 组必须与训练集中的第 1 组具有相同的特征。

b. Iris 无监督拟合似乎不太合适。

library('kohonen')
set.seed(1)

idx_n <- sample(nrow(iris),120)

train <- iris[idx_n,]
row.names(train) <- NULL

test <- iris[-idx_n,]
row.names(test) <- NULL

#preprocess
train.sc <- scale(train[,-5])

#train model
som_grid <- somgrid(xdim = 5
                    ,ydim=5
                    ,topo="hexagonal"
                    ,toroidal = F)  
som.iris<- som(train.sc
               ,grid=som_grid
               ,rlen=200
               ,alpha=c(0.05,0.01)
               ,keep.data = TRUE )

set_cluster <- 3

## use hierarchical clustering to cluster the codebook vectors
som.iris.hc <- cutree(hclust(dist(som.iris$codes[[1]])), set_cluster)



# --------- Predict new dataset ----------
#scale test set acording to fitted model data
test.sc  <- scale(test[,-5],
                  center = attr(som.iris$data[[1]], "scaled:center"),
                  scale  = attr(som.iris$data[[1]], "scaled:scale"))

test.pred <- predict(som.iris, 
                     newdata = test.sc)


set_cluster <- 3

## use hierarchical clustering to cluster the codebook vectors
som.iris.hc_test <- cutree(hclust(dist(test.pred$predictions[[1]])), set_cluster)

#attach cluster groups. Am I doing this right?
train_final <- cbind(train,cluster=som.iris.hc[som.iris$unit.classif])
test_final <- cbind(test,cluster=som.iris.hc_test)

#explore each clusters
by(train_final, train_final$cluster, summary)
by(test_final, test_final$cluster, summary)

#results - Not very Spectacular
table(train_final$Species,train_final$cluster)

【问题讨论】:

    标签: r cluster-analysis prediction unsupervised-learning som


    【解决方案1】:

    我目前的解决方法是首先训练为无监督 SOM 模型,一旦我确定了集群的数量,我将标记集群并重新训练为监督 SOM 模型。然后我可以更有针对性地预测新数据集。想听听你的想法。

    library('kohonen')
    set.seed(1)
    
    idx_n <- sample(nrow(iris),120)
    
    train <- iris[idx_n,]
    row.names(train) <- NULL
    
    test <- iris[-idx_n,]
    row.names(test) <- NULL
    
    #preprocess
    train.sc <- scale(train[,-5])
    
    #train model as unsupervised
    som_grid <- somgrid(xdim = 5
                        ,ydim=5
                        ,topo="hexagonal"
                        ,toroidal = F)  
    som.iris<- som(train.sc
                   ,grid=som_grid
                   ,rlen=200
                   ,alpha=c(0.05,0.01)
                   ,keep.data = TRUE )
    
    set_cluster <- 3
    
    ## use hierarchical clustering to cluster the codebook vectors
    som.iris.hc <- cutree(hclust(dist(som.iris$codes[[1]])), set_cluster)
    train_cluster <- as.factor(as.vector(som.iris.hc[som.iris$unit.classif]))
    
    #assign new clusters into training set
    train.l.sc <- list(x=train.sc,y=train_cluster)
    
    #retrain model as supervised learning
    mygrid = somgrid(5, 5, "hexagonal")
    som.iris.l <- supersom(train.l.sc, grid = mygrid, maxNA.fraction = .5)
    
    # --------- Predict new dataset ----------
    #scale test set acording to fitted model data
    test.l.sc <- list(x = as.matrix(scale(test[,-5]
                                          ,center = attr(som.iris.l$data[[1]], "scaled:center"),
                                          scale  = attr(som.iris.l$data[[1]], "scaled:scale")
                                          )))
    
    test.pred <- predict(som.iris.l, 
                         newdata = test.l.sc)
    
    #attach cluster groups
    train_final <- cbind(train,cluster=train_cluster)
    test_final <- cbind(test,cluster=test.pred$predictions$y)
    
    #explore each clusters
    by(train_final, train_final$cluster, summary)
    by(test_final, test_final$cluster, summary)
    

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2018-03-07
      • 2019-07-04
      • 2017-11-03
      • 2018-07-11
      相关资源
      最近更新 更多