简体   繁体   English

如何将新数据映射/绘制到训练有素的SOM映射上?

[英]How to map/plot new data onto a trained SOM map?

After training a SOM, how can you plot new data onto the SOM and visualise how it maps onto the SOM? 训练了SOM之后,如何将新数据绘制到SOM上并可视化它如何映射到SOM? Ideally, I would like for it to be plotted with the corresponding classification colour and node location. 理想情况下,我希望将其与相应的分类颜色和节点位置一起绘制。 identify() has the capability of pinpointing data based on selections on the SOM map but it is very limited and can only do one at a time. identify()可以基于SOM映射上的选择来精确定位数据,但是它非常有限,一次只能执行一次。 I would like to map a whole (new) dataset and visualise it. 我想映射整个(新)数据集并对其进行可视化。 I am able to get the node location from using map() and the group association, but how can I manually plot the new points onto the SOM? 我可以通过使用map()和组关联来获取节点位置,但是如何将新点手动绘制到SOM上呢? Couldn't find anything pertinent on the internet or the kohonen R documentation. 在互联网或kohonen R文档中找不到任何相关内容。 Appreciate any help. 感谢任何帮助。

library(kohonen)
data(wines)
wines.train<-wines[1:150,]
wines.test<-wines[151:nrow(wines),]
wines.sc <- scale(wines.train)

set.seed(7)
wines.som<-som(wines.sc, grid = somgrid(5, 4, "hexagonal"),rlen=150,alpha=c(0.05,0.01))
wines.hc<-cutree(hclust(dist(wines.som$codes[[1]])),6) 
plot(wines.som,type="mapping",bgcol=rainbow(6)[wines.hc])
add.cluster.boundaries(wines.som,wines.hc)

can be used to manually inspect specific nodes on SOM 可用于手动检查SOM上的特定节点

identify(wines.som$grid$pts,labels=as.vector(wines.hc),plot=T,pos=T) 

map new data onto trained SOM 将新数据映射到训练有素的SOM

wines.map<-map(wines.som,scale(wines.test))
wines.test.grp<-sapply(wines.map$unit.classif,function(x) wines.hc[[x]])

In my opinion, one thing to note is that you should not scale your test data using value inside of it. 我认为要注意的一件事是,您不应使用其内部的值来扩展测试数据。 You should scale your test data using scaling parameter of your train data. 您应该使用火车数据的缩放参数来缩放测试数据。 Because the model was trained using information from the train data. 因为模型是使用火车数据中的信息进行训练的。 It has not seen the test data. 尚未看到测试数据。

So your scaled test data would be like this: 因此,按比例缩放的测试数据将如下所示:

wines.test.scale <- scale(wines.test, center = attr(wines.sc, 'scaled:center'), scale = attr(wines.sc, 'scaled:scale'))

Now you can assign a new member to your model. 现在,您可以为模型分配一个新成员。 This is a distance measurement of each data to every model's node. 这是每个数据到每个模型节点的距离测量。 Because you split your data to train and test, there can be two new members added to your model, ie the train distance and the test distance. 由于您将数据拆分以进行训练和测试,因此可以在模型中添加两​​个新成员,即火车距离和测试距离。 I give them names train.map and test.map, since this process can be regarded as a mapping process of input data to the model's map. 我将它们命名为train.map和test.map,因为此过程可以看作是输入数据到模型映射的映射过程。

wines.som$train.map <- apply(
  wines.sc, 1, function(input1) {
    apply(
      wines.som$codes[[1]], 1, function(input2) dist(rbind(input1, input2))
    )
  }
)

wines.som$test.map <- apply(
  wines.test.scale, 1, function(input1) {
    apply(
      wines.som$codes[[1]], 1, function(input2) dist(rbind(input1, input2))
    )
  }
)

I think one must put the variable into the model because once the kernel attached the library, it overrode the base plot function with that of the package until one detach the package. 我认为必须将变量放入模型中,因为一旦内核附加了库,它就会用软件包的功能覆盖基本绘图功能,直到一个人拆离软件包。 The new plot function must recognize that the variable being processed has a proper class. 新的绘图功能必须识别出正在处理的变量具有适当的类。

Now you can plot the map of your individual input data to the model's network. 现在,您可以将各个输入数据的地图绘制到模型的网络。 You can put two stages here: the train data mapping and the test data mapping. 您可以在此处放置两个阶段:训练数据映射和测试数据映射。

par(mfrow = c(5,5))
for (a in 1:ncol(wines.som$train.map)) {
  plot(
    wines.som, type = 'property', property = wines.som$train.map[,a],
    main = paste('train',a) 
  )
}

par(mfrow = c(5,5))
for (a in 1:ncol(wines.som$test.map)) {
  plot(
    wines.som, type = 'property', property = wines.som$test.map[,a],
    main = paste('test',a) 
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM