【发布时间】:2021-04-23 03:33:49
【问题描述】:
我正在学习 R 中的“kohonen”包,目的是制作自组织地图(SOM,也称为 Kohonen 网络 - 一种机器学习算法)。我在这里关注这个 R 语言教程:https://www.rpubs.com/loveb/som
我尝试创建自己的数据(这次使用“因子”和“数值”变量)并运行 SOM 算法(这次使用“supersom()”函数):
#load libraries and adjust colors
library(kohonen) #fitting SOMs
library(ggplot2) #plots
library(RColorBrewer) #colors, using predefined palettes
contrast <- c("#FA4925", "#22693E", "#D4D40F", "#2C4382", "#F0F0F0", "#3D3D3D") #my own, contrasting pairs
cols <- brewer.pal(10, "Paired")
#create and format data
a =rnorm(1000,10,10)
b = rnorm(1000,10,5)
c = rnorm(1000,5,5)
d = rnorm(1000,5,10)
e <- sample( LETTERS[1:4], 100 , replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
f <- sample( LETTERS[1:5], 100 , replace=TRUE, prob=c(0.2, 0.2, 0.2, 0.2, 0.2) )
g <- sample( LETTERS[1:2], 100 , replace=TRUE, prob=c(0.5, 0.5) )
data = data.frame(a,b,c,d,e,f,g)
data$e = as.factor(data$e)
data$f = as.factor(data$f)
data$g = as.factor(data$g)
cols <- 1:4
data[cols] <- scale(data[cols])
#som model
som <- supersom(data= as.list(data), grid = somgrid(10,10, "hexagonal"),
dist.fct = "euclidean", keep.data = TRUE)
从这里,我能够成功地制作一些基本情节:
#plots
#pretty gradient colors
colour1 <- tricolor(som$grid)
colour4 <- tricolor(som$grid, phi = c(pi/8, 6, -pi/6), offset = 0.1)
plot(som, type="changes")
plot(som, type="count")
plot(som, type="quality", shape = "straight")
plot(som, type="dist.neighbours", palette.name=grey.colors, shape = "straight")
但是,当我尝试为每个变量制作单独的图时,问题就出现了:
#error
var <- 1 #define the variable to plot
plot(som, type = "property", property = getCodes(som)[,var], main=colnames(getCodes(som))[var], palette.name=terrain.colors)
var <- 6 #define the variable to plot
plot(som, type = "property", property = getCodes(som)[,var], main=colnames(getCodes(som))[var], palette.name=terrain.colors)
这会产生错误:"Error: Incorrect Number of Dimensions"
尝试对 SOM 网络进行集群时会产生类似的错误 (NAs by coercion):
#cluster (error)
set.seed(33) #for reproducability
fit_kmeans <- kmeans(data, 3) #3 clusters are used, as indicated by the wss development.
cl_assignmentk <- fit_kmeans$cluster[data$unit.classif]
par(mfrow=c(1,1))
plot(som, type="mapping", bg = rgb(colour4), shape = "straight", border = "grey",col=contrast)
add.cluster.boundaries(som, fit_kmeans$cluster, lwd = 3, lty = 2, col=contrast[4])
谁能告诉我我做错了什么? 谢谢
来源:https://www.rdocumentation.org/packages/kohonen/versions/2.0.5/topics/supersom
【问题讨论】:
-
这是很多与问题无关的代码。请将代码减少到重现问题所需的最低限度:生成一个最小数据集,当绘制该数据集时,它会导致您看到的问题并发布只是那个(通过
dput),加上最小的导致问题的代码。 -
@konrad rudolph:谢谢你的回复。我想展示代码的工作示例以及错误 - 以提供更多上下文。我只会保留相关代码。谢谢
-
@KonradRudolph :我已经删除了一些不相关的代码,你能告诉我你的想法吗?谢谢!
标签: r machine-learning data-visualization cluster-analysis data-manipulation