简体   繁体   English

oneHotEncode R 中的光栅层

[英]oneHotEncode a rasterlayer in R

I have a categorical raster layer that I need to one-hot encode, as I'm using neural networks to run a species distribution model (for a class) and it only works on continuous predictors.我有一个分类栅格层,我需要对其进行一次热编码,因为我正在使用神经网络来运行物种分布 model (对于一个类),它只适用于连续预测器。 I've already run the model with the one-hot encoded data itself, but in order to predict onto a map, the raster itself needs to be one-Hot encoded in the same way.我已经用 one-hot 编码数据本身运行了 model,但是为了预测到 map,光栅本身需要以相同的方式进行 one-Hot 编码。

In the RStoolbox package, oneHotEncode() should do what I need it to do, but I can't get it to workRStoolbox package 中, oneHotEncode()应该做我需要做的事情,但我无法让它工作

nn.raster<-oneHotEncode(newraster$NLCD_2016_Land_Cover_oreg, classes=values, background = 0, foreground = 1, na.rm = FALSE)

Error message:错误信息:

Error in.calcTest(x[1:5], fun, na.rm, forcefun, forceapply): cannot use this function.错误 in.calcTest(x[1:5], fun, na.rm, forcefun, forceapply):不能使用这个 function。 Perhaps add '...' or 'na.rm' to the function arguments?也许在 function arguments 中添加“...”或“na.rm”?

Has anybody used this function and can help me troubleshoot?有人用过这个 function 可以帮我排除故障吗? I think the problem is coming from the class's argument.我认为问题来自班级的争论。 My categories are numerical (from the national land cover raster), which is why they show up as "values" in the raster info.我的类别是数字的(来自国家土地覆盖栅格),这就是为什么它们在栅格信息中显示为“值”。 Do I need to do something to reclassify them?我需要做些什么来重新分类它们吗? I think I'm naming them wrong but I'm not sure how.我想我把它们命名错了,但我不知道怎么命名。

It is difficult to help ass you are not providing a minimal, self-contained, reproducible, example .如果您没有提供最小的、独立的、可重现的示例,很难帮助您。 But, after giving a quick look at RStoolbox::oneHotEncode , I am under the impression that it does what raster::layerize also does.但是,在快速浏览一下RStoolbox::oneHotEncode ,我的印象是它做了raster::layerize也可以做的事情。

library(raster)
r <- raster(nrow=20, ncol=20)
values(r) <- c(rep(NA, 50), rep(1:5, 70))
r
#class      : RasterLayer 
#dimensions : 20, 20, 400  (nrow, ncol, ncell)
#resolution : 18, 9  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#source     : memory
#names      : layer 
#values     : 1, 5  (min, max)

b <- layerize(r)
b
#class      : RasterBrick 
#dimensions : 20, 20, 400, 5  (nrow, ncol, ncell, nlayers)
#resolution : 18, 9  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#source     : memory
#names      : X1, X2, X3, X4, X5 
#min values :  0,  0,  0,  0,  0 
#max values :  1,  1,  1,  1,  1 

Which is equivalent to terra::separate相当于terra::separate

library(terra)    
r <- rast(nrow=5, ncol=5)
values(r) <- rep(c(1:4, NA), each=5)
b <- separate(r)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM