【问题标题】:Visualising evolution of SOM learning in 2-D在 2-D 中可视化 SOM 学习的演变
【发布时间】:2022-10-21 15:36:14
【问题描述】:

我对使用 R 进行数据可视化比较陌生。但是,我想使用 R 来提供自组织图 (SOM) 如何学习的可视化演示。

我想知道是否有人可以帮助如何在 R 中重现这种类型的示例,或者指导我使用可重现的代码?

即使是一些好的指针也会很有帮助。我在 SOM 文档或 R 可视化文档中找不到类似的内容。

将不胜感激。

【问题讨论】:

  • 您能否提供一些示例数据并说明您查看或尝试过哪种 R 可视化以及为什么它们不起作用?或者,如果您在 Google 上搜索过,您看到哪些指南方向正确,但并不完全正确(以及为什么它们不符合您的需求)。如果您是 R 的绝对初学者,请查看 ggplot2 的可视化作为开始的地方。
  • 嗨@Roger-123。感谢您的反馈。我遇到的问题是,尽管查看了各种 R 文本和库(例如 ggplot、igraph 等)。因此,可以肯定的是,如果您已经看到示例 - 对它们的指导会有所帮助。对于这些类型的图表,我发现的唯一来源是基于 SOM 的出版物,它们在其中模拟了 SOM 学习过程。然而,不幸的是,他们并不经常公布他们的方法。
  • 例如,我在以下出版物中找到了上面发布的示例:osti.gov/servlets/purl/1566795

标签: r visualization


【解决方案1】:
rm(list = ls())
while(!is.null(dev.list()))dev.off()

library(dplyr)
library(kohonen)
library(ggplot2)

nx <- 20; ny <- 20

som.input <- as.matrix(expand.grid(x = seq(-1, 1, length.out = nx), y = seq(-1, 1, length.out = ny))) %>% 
  scale

x.grid <- 10; y.grid <- 10

sgd <- somgrid(x.grid, y.grid,'hexagonal')

som.output <- list()

epoch <- 100

initial.matrix <- matrix(rnorm(x.grid * y.grid * ncol(som.input), 0, .1), nrow = x.grid * y.grid)
training.alpha <- seq(1, .01, length.out = epoch)

som.output[[1]] <- som(som.input, sgd, rlen = 1, init = initial.matrix, alpha = training.alpha[1], mode = 'online')

for(a in 2:epoch) {
  som.output[[a]] <- som(som.input, sgd, rlen = 1, init = som.output[[a-1]]$codes[[1]], alpha = training.alpha[a], mode = 'online')
}

no.picture <- 16

index <- as.integer(seq(1, epoch, length.out = no.picture))

som.codes <- lapply(index, function(input) {
  codes <- som.output[[input]]$codes[[1]] %>% 
    scale(attr(som.input, 'scaled:center'), attr(som.input, 'scaled:scale'))
  
  cbind(
    data.frame(codes * (input / epoch), index = input),
    expand.grid(column = 1:y.grid, rows = 1:x.grid)
  )
})

som.codes <- do.call(rbind, som.codes)

ggplot() +
  geom_point(aes(x, y), as.data.frame(som.input), color = 'red', size = 1.1) +
  geom_point(aes(x, y), som.codes, size = 1.9) +
  geom_path(aes(x, y, group = column), as.data.frame(som.codes)) +
  geom_path(aes(x, y, group = rows), as.data.frame(som.codes)) +
  facet_wrap(index ~ .) +
  theme_bw() +
  coord_equal() +
  xlab('') + ylab('')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2013-01-15
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多