绘图导致“错误:尺寸错误". [英] Plot causes "Error: Incorrect Number of Dimensions"

查看:77
本文介绍了绘图导致“错误:尺寸错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关"kohonen"的信息,R中的软件包,用于制作自组织映射(SOM,也称为Kohonen Networks-一种机器学习算法).我在这里关注该R语言教程: https://www.rpubs.com/loveb/som

I am learning about the "kohonen" package in R for the purpose of making Self Organizing Maps (SOM, also called Kohonen Networks - a type of Machine Learning algorithm). I am following this R language tutorial over here: https://www.rpubs.com/loveb/som

我试图创建自己的数据(这次同时使用"factor"和"numeric"变量)并运行SOM算法(这次使用"supersom()"函数):

I tried to create my own data (this time with both "factor" and "numeric" variables) and run the SOM algorithm (this time using the "supersom()" function instead):

#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)

从这里,我能够成功地绘制一些基本情节:

From here, I was able to successfully make some of the basic plots:

    #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")

但是,当我尝试为每个变量绘制单独的图时,就会出现问题:

However, the problem arises when I try to make individual plots for each variable:

#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)

这会产生错误:错误:尺寸错误"

尝试对SOM网络进行群集时,会产生类似的错误( NAs by coercion ):

A similar error (NAs by coercion) is produced when attempting to cluster the SOM Network:

#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])

有人可以告诉我我做错了什么吗?谢谢

Can someone please tell me what I am doing wrong? Thanks

来源: https://www.rdocumentation.org/packages/kohonen/versions/2.0.5/topics/supersom

推荐答案

getCodes()产生一个列表,因此您必须将其视为一个列表.

getCodes() produces a list and as such you have to treat it like one.

调用 getCodes(som)会生成一个包含7个名为ag的项目的列表,因此您应该使用 $ [[]从列表中选择项目.]

Calling getCodes(som) produces a list containing 7 items named a-g as such you should be selecting items from the list either using $ or [[]]

例如

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)

如果必须在调用绘图之前设置变量,则可以执行以下操作:

if you must set the variable prior to calling the plot you can do so like:

var <- 1    
plot(som, type = "property", property = getCodes(som)[[var]], main=names(getCodes(som))[var], palette.name=terrain.colors)

关于 kmeans()

kmeans()需要一个矩阵或一个可以强制转换为矩阵的对象,您拥有一些不能强制转换为数值的因子(分类数据),或者删除因子或找到另一种方法

kmeans() needs a matrix or an object that can be coerced into a matrix, you have factors (categorical data) which cannot be coerced into numeric, either drop the factors, or find another method.

删除因素:

#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()直接指定代码,如下所示:

edit: Alternatively you can specify the code directly from getCodes() by using idx like so:

plot(som, type = "property", property = getCodes(som, idx = 1), main="a"), palette.name=terrain.colors)

这篇关于绘图导致“错误:尺寸错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆