使用raster包进行randomForest分类的问题

4

我在使用randomForest和raster包时遇到了问题。首先,我创建了分类器:

library(raster)
library(randomForest)

# Set some user variables
fn = "image.pix"
outraster = "classified.pix"
training_band = 2
validation_band = 1
original_classes = c(125,126,136,137,151,152,159,170)
reclassd_classes = c(122,122,136,137,150,150,150,170)

# Get the training data
myraster = stack(fn)
training_class = subset(myraster, training_band)

# Reclass the training data classes as required
training_class = subs(training_class, data.frame(original_classes,reclassd_classes))

# Find pixels that have training data and prepare the data used to create the classifier
is_training = Which(training_class != 0, cells=TRUE)
training_predictors = extract(myraster, is_training)[,3:nlayers(myraster)]
training_response = as.factor(extract(training_class, is_training))
remove(is_training)

# Create and save the forest, use odd number of trees to avoid breaking ties at random
r_tree = randomForest(training_predictors, y=training_response, ntree = 201, keep.forest=TRUE) # Runs out of memory, does not allow more trees than this...
remove(training_predictors, training_response)

到目前为止,一切都很好。通过查看错误率、混淆矩阵等,我可以看出森林被正确创建。然而,当我尝试对一些数据进行分类时,我遇到了以下问题:predictions中返回全部NA值。

# Classify the whole image
predictor_data = subset(myraster, 3:nlayers(myraster))
layerNames(predictor_data) = layerNames(myraster)[3:nlayers(myraster)]
predictions = predict(predictor_data, r_tree, type='response', progress='text')

并给出了以下警告:

Warning messages:
1: In `[<-.factor`(`*tmp*`, , value = c(1, 1, 1, 1, 1, 1,  ... :
  invalid factor level, NAs generated
(keeps going like this)...

然而,直接调用predict.randomForest函数可行并返回预期的predictions(这对我来说不是一个好选择,因为图像很大,我无法在内存中存储整个矩阵):

# Classify the whole image and write it to file
predictor_data = subset(myraster, 3:nlayers(myraster))
layerNames(predictor_data) = layerNames(myraster)[3:nlayers(myraster)]
predictor_data = extract(predictor_data, extent(predictor_data))
predictions = predict(r_tree, newdata=predictor_data)

如何使它直接与“raster”版本配合使用? 我知道这是可能的,如 predict{raster} 的示例所示。


没有数据,这个问题不容易解决。当我的训练集和测试集没有相同的因子水平时,我会遇到类似的错误,这通常是由于某些错误的转换(数值变量被解释为因子,或者由于数据中的某些错误而将数字存储为字符)。我会尝试弄清楚输入和输出的确切内容。predict.raster 进行了相当多的操作,所以可能有一些东西不像预期那样工作。 - Joris Meys
正如您在代码中所看到的,训练数据由一个类(因子)和26个数值变量组成,即125、126、127、128、129...和0到32768(16位通道)。正如我所说,分类器对象已经正确创建,当我直接使用矩阵中的数据调用predict.randomForest时,而不是使用RasterLayer数据和Raster的predict时,它会输出正确的预测结果... 我认为问题可能与NA有关,但我无法确定具体位置。是否有一种测试方法可以用来排除这种情况? - Benjamin
1个回答

0

你可以尝试在writeRaster函数中嵌套predict.randomForest,并按照raster包中包含的pdf将矩阵分块写入栅格。在调用raster函数中的predict时,尝试使用参数'na.rm=TRUE'。你还可以为预测栅格中的NA值分配虚拟值,然后使用raster包中的函数将其重写为NA。

至于调用RFs时的内存问题,我曾经遇到过大量处理BRTs时的内存问题。它们在磁盘和内存中都非常庞大!(如果模型比数据更复杂,会怎样呢?)我没有在32位机器上可靠地运行它们(WinXp或Linux)。有时调整Windows对应用程序的内存分配有所帮助,转移到Linux也有所帮助,但是我从64位Windows或Linux机器中获得了最多的帮助,因为它们对应用程序可以占用的内存数量施加了更高(或无)限制。通过这种方式,您可以增加可以使用的树的数量。


谢谢您的建议,但最终我发现对图像的行组进行递归调用predict.randomForest比调用“Raster”版本的predict更快,所以目前问题已经解决。 - Benjamin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接