Plot causes "Error: Incorrect Number of Dimensions"
Plot causes "Error: Incorrect Number of Dimensions"
我正在学习 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
getCodes()
生成一个列表,因此您必须将其视为一个列表。
调用 getCodes(som)
会生成一个包含 7 个名为 a-g 的项目的列表,因此您应该使用 $
或 [[]]
从列表中选择项目
例如
plot(som, type = "property", property = getCodes(som)[[1]], main=names(getCodes(som))[1], palette.name=terrain.colors)
或
plot(som, type = "property", property = getCodes(som)$a, main="a", palette.name=terrain.colors)
或
plot(som, type = "property", property = getCodes(som)[["a"]], main="a", palette.name=terrain.colors)
如果您必须在调用绘图之前设置变量,您可以这样做:
var <- 1
plot(som, type = "property", property = getCodes(som)[[var]], main=names(getCodes(som))[var], palette.name=terrain.colors)
关于 kmeans()
kmeans()
需要一个矩阵或一个可以强制转化为矩阵的对象,您有不能强制转化为数字的因子(分类数据),要么删除因子,要么找到另一种方法。
删除因素:
#cluster (error)
set.seed(33)
#for reproducability
fit_kmeans <- kmeans(as.matrix(data[1:4]), 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])
编辑:
或者,您可以使用 idx 直接从 getCodes()
指定代码,如下所示:
plot(som, type = "property", property = getCodes(som, idx = 1), main="a"), palette.name=terrain.colors)
我正在学习 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
getCodes()
生成一个列表,因此您必须将其视为一个列表。
调用 getCodes(som)
会生成一个包含 7 个名为 a-g 的项目的列表,因此您应该使用 $
或 [[]]
例如
plot(som, type = "property", property = getCodes(som)[[1]], main=names(getCodes(som))[1], palette.name=terrain.colors)
或
plot(som, type = "property", property = getCodes(som)$a, main="a", palette.name=terrain.colors)
或
plot(som, type = "property", property = getCodes(som)[["a"]], main="a", palette.name=terrain.colors)
如果您必须在调用绘图之前设置变量,您可以这样做:
var <- 1
plot(som, type = "property", property = getCodes(som)[[var]], main=names(getCodes(som))[var], palette.name=terrain.colors)
关于 kmeans()
kmeans()
需要一个矩阵或一个可以强制转化为矩阵的对象,您有不能强制转化为数字的因子(分类数据),要么删除因子,要么找到另一种方法。
删除因素:
#cluster (error)
set.seed(33)
#for reproducability
fit_kmeans <- kmeans(as.matrix(data[1:4]), 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])
编辑:
或者,您可以使用 idx 直接从 getCodes()
指定代码,如下所示:
plot(som, type = "property", property = getCodes(som, idx = 1), main="a"), palette.name=terrain.colors)