【发布时间】:2021-06-03 16:19:49
【问题描述】:
我正在使用以下代码执行 SOM(自组织图,也称为 Kohonen 网络)机器学习算法来可视化一些数据。然后,我在可视化上使用了一个聚类算法(我选择了 8 个聚类):
#load library
library(tidyverse)
library(kohonen)
library(GGally)
library(purrr)
library(tidyr)
library(dplyr)
library(mlr)
#load data
data(flea)
fleaTib <- as_tibble(flea)
#define SOM grid
somGrid <- somgrid(xdim = 5, ydim = 5, topo = "hexagonal",
neighbourhood.fct = "bubble", toroidal = FALSE)
#format data
fleaScaled <- fleaTib %>%
select(-species) %>%
scale()
#perform som
fleaSom <- som(fleaScaled, grid = somGrid, rlen = 5000,
alpha = c(0.05, 0.01))
par(mfrow = c(2, 3))
plotTypes <- c("codes", "changes", "counts", "quality",
"dist.neighbours", "mapping")
walk(plotTypes, ~plot(fleaSom, type = ., shape = "straight"))
getCodes(fleaSom) %>%
as_tibble() %>%
iwalk(~plot(fleaSom, type = "property", property = .,
main = .y, shape = "straight"))
# listing flea species on SOM
par(mfrow = c(1, 2))
nodeCols <- c("cyan3", "yellow", "purple", "red", "blue", "green", "white", "pink")
plot(fleaSom, type = "mapping", pch = 21,
bg = nodeCols[as.numeric(fleaTib$species)],
shape = "straight", bgcol = "lightgrey")
# CLUSTER AND ADD TO SOM MAP ---- (8 clusters)
clusters <- cutree(hclust(dist(fleaSom$codes[[1]],
method = "manhattan")), 8)
somClusters <- map_dbl(clusters, ~{
if(. == 1) 3
else if(. == 2) 2
else 1
}
)
plot(fleaSom, type = "mapping", pch = 21,
bg = nodeCols[as.numeric(fleaTib$species)],
shape = "straight",
bgcol = nodeCols[as.integer(somClusters)])
add.cluster.boundaries(fleaSom, somClusters)
但在上图中,只显示了 3 种颜色而不是 8 种。
谁能告诉我我做错了什么?
【问题讨论】:
标签: r machine-learning colors data-visualization cluster-analysis