# 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)
Easy SOM
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
Data Preparation
# Load data
<- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/easy-som/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
data
# convert data structure
<- data[,1]
target <- factor(target, levels = unique(target))
target <- data[,-1]
data <- as.data.frame(data)
data for (i in 1:ncol(data)) {
<- as.numeric(data[,i])
data[,i]
}<- as.matrix(data)
data set.seed(7)
<- xyf(scale(data), target, grid = somgrid(xdim=6, ydim=4, topo="hexagonal"), rlen=100)
kohmap
<- c("#A50026","#D73027","#F46D43","#FDAE61","#FEE090","#FFFFBF","#E0F3F8",
color_key "#ABD9E9","#74ADD1","#4575B4","#313695")
<- function (n, alpha, rev = FALSE) {
colors 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
<- function () {
p par(mfrow = c(3,2))
<- classmat2classvec(getCodes(kohmap, 2))
xyfpredictions plot(kohmap, type="counts", col = as.integer(target),
palette.name = colors,
pchs = as.integer(target),
main = "Counts plot", shape = "straight", border = NA)
<- cutree(hclust(object.distances(kohmap, "codes")), 3)
som.hc 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
<- classmat2classvec(getCodes(kohmap, 2))
xyfpredictions <- colors(3)
bgcols 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)
<- plot(kohmap, type="quality", shape = "straight",
similarities palette.name = colors)
plot(kohmap, type="codes", shape = "straight",
main = c("Codes X", "Codes Y"), palette.name = colors)
}
p()
