[GIS] I don’t find the mistake in the RandomForest-classification

errorrandom forestraster

I am trying to make a RF-classification of aerial-imagery to specify tree-species.
I extracted 3 bands and made a .csv file with columns:

band1  band2  band3  x  y  class

The band columns include the pixel-value of the point which I marked in ArcGIS and copied to the attribute-table.
x/y include the coordinates of the points.
class includes the class, such as forest, juniper…

The RF reads the .csv and classifies the bands it sees in the rasterstack.
The classifier gives back an image with the classes.

So I mark my code and run it:

A classifier-window shows up, and freezes.
5 Seconds later the console shows an error:

 Error in eval(expr, envir, enclos) : object 'band1' not found

The problem is unclear. The code is right, but the compiler reacts as if there was a typo when I run the code. But as far as I can see, there is no typo.
band1, like band2 and band3 are in the pointdata_sw_aug1.csv. It is the 1st column and has no space before or after it (" band1"). Therefore I uploaded the images.
Since my rasters are in tif-format, I recently changed the code because at first is was written by Aaron for img-files:

rlist=list.files(getwd(), pattern="img$", full.names=TRUE) 

to

rlist=list.files(getwd(), pattern="tif$", full.names=TRUE)

The RandomForest-Package then opens a little "predict" window where it says that 31 steps are left.
It takes 5 seconds till it stops with an error-message:

Error in eval(expr, envir, enclos) : object 'band1' not found

It doesn't go on.


This should work, but gives me an error:

require(sp)
require(rgdal)
require(raster)
require(randomForest)

# Set the working directory

setwd("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R")

# Create list of rasters

rlist=list.files(getwd(), pattern="tif$", full.names=TRUE) 

# Create raster-stack

rasters = stack(rlist)

# Read Raster Training-Table *.csv

intable = read.csv("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R/pointdata_sw_aug1.csv")

# Assign Training-points from Columns

myrf = randomForest(factor(class) ~ band1 + band2 + band3 , data = intable, ntree = 2500)

# Run RandomForest classifier

predict(rasters, myrf, filename="outFileName.img", type="response",
    index=1, na.rm=TRUE, progress="window", overwrite=TRUE) 

my .csv:
enter image description here

enter image description here

As far as I can see there are no mistakes.
Whats wrong here?

Error-message is this (copied from console):

require(sp)
require(rgdal)
require(raster)
require(randomForest)


 >setwd("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/test_run_R")


> rlist=list.files(getwd(), pattern="tif$", full.names=TRUE) 
> 

> rasters = stack(rlist)
> 
> intable = read.csv("D:/BA-Workspace/DOP_10/orthophotos_abcd/R/pointdata_sw_aug1.csv")
> 
> myrf = randomForest(factor(class) ~ band1 + band2 + band3 , data = intable, ntree =     2500)
> 
> predict(rasters, myrf, filename="outFileName.img", type="response",
+         index=1, na.rm=TRUE, progress="window", overwrite=TRUE)
**Error in eval(expr, envir, enclos) : object 'band1' not found**

Best Answer

I think your problem is related to the layer names of your raster stack ('rasters'). Make sure these are the same as in your .csv. You can get the layer names with names(rasters) and set them with names(rasters) <- c("band1", "band2", "band3")

Hope this helps TimSalabim

Related Question