Easy SOM

Authors

[Editor] Hu Zheng;

[Contributors]

Establish the SOM model and conduct the visulization.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: data.table; jsonlite; kohonen

# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("kohonen", quietly = TRUE)) {
  install.packages("kohonen")
}

# Load packages
library(data.table)
library(jsonlite)
library(kohonen)

Data Preparation

# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/easy-som/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)

# convert data structure
target <- data[,1]
target <- factor(target, levels = unique(target))
data <- data[,-1]
data <- as.data.frame(data)
for (i in 1:ncol(data)) {
  data[,i] <- as.numeric(data[,i])
}
data <- as.matrix(data)
set.seed(7)
kohmap <- xyf(scale(data), target, grid = somgrid(xdim=6, ydim=4, topo="hexagonal"), rlen=100)

color_key <- c("#A50026","#D73027","#F46D43","#FDAE61","#FEE090","#FFFFBF","#E0F3F8",
               "#ABD9E9","#74ADD1","#4575B4","#313695")
colors <- function (n, alpha, rev = FALSE) {
  colorRampPalette(color_key)(n)
}

# View data
head(data[,1:5])
     alcohol malic acid  ash ash alkalinity magnesium
[1,]   12.86       1.35 2.32           18.0       122
[2,]   12.88       2.99 2.40           20.0       104
[3,]   12.81       2.31 2.40           24.0        98
[4,]   12.70       3.55 2.36           21.5       106
[5,]   12.51       1.24 2.25           17.5        85
[6,]   12.60       2.46 2.20           18.5        94

Visualization

# Easy SOM
p <- function () {
  par(mfrow = c(3,2))
  xyfpredictions <- classmat2classvec(getCodes(kohmap, 2))
  plot(kohmap, type="counts", col = as.integer(target),
       palette.name = colors,
       pchs = as.integer(target), 
       main = "Counts plot", shape = "straight", border = NA)
  
  som.hc <- cutree(hclust(object.distances(kohmap, "codes")), 3)
  add.cluster.boundaries(kohmap, som.hc)

  plot(kohmap, type="mapping",
       labels = as.integer(target), col = colors(3)[as.integer(target)],
       palette.name = colors,
       shape = "straight",
       main = "Mapping plot")

  ## add background colors to units according to their predicted class labels
  xyfpredictions <- classmat2classvec(getCodes(kohmap, 2))
  bgcols <- colors(3)
  plot(kohmap, type="mapping", col = as.integer(target),
       pchs = as.integer(target), bgcol = bgcols[as.integer(xyfpredictions)],
       main = "Another mapping plot", shape = "straight", border = NA)
  
  similarities <- plot(kohmap, type="quality", shape = "straight",
                       palette.name = colors)
  
  plot(kohmap, type="codes", shape = "straight", 
       main = c("Codes X", "Codes Y"), palette.name = colors)
}

p()
FigureΒ 1: Easy SOM