Random forest for forest cover classification

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Random forest for forest cover classification

Enoch Gyamfi Ampadu
Dear List,

Please I am now learning R to apply to my analysis. I am doing a forest
cover classification using Random forest. So far I have stacked the image,
done atmospheric correction, and clipped out my study area. I have created
some training sites of the cover classes in ArcMap and imported into R as
well as the clipped image of my study sites. I have been doing try and
error with some online codes. I will be glad if you could provide me with
some codes to apply.

Thank you.

Best regards,

Enoch

--
*Enoch Gyamfi - Ampadu*

*Geography & Environmental Sciences*

*College of Agriculture, Engineering & Science*

*University of KwaZulu-Natal, Westville Campus*

*Private Bag X54001*
*Durban, South Africa **– 4000**.*
*Phone: +27 835 828255*

*email: [hidden email] <[hidden email]>*


*skype: enoch.ampadu*
*The highest evidence of nobility is self-control*.

*A simple act of kindness creates an endless ripple*.

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Random forest for forest cover classification

Juan Pablo Carranza
Dear Enoch, I strongly recomend to try an segmentation approach instead a
classification. However, here's some code you can use:

library(rgdal)
library(raster)
library(caret)
library(kerndwd)
library(sp)
library(RColorBrewer)
library(dplyr)
library(leaflet)
library(sf)
img <- brick("******.tiff")
names(img) <- paste0("B", c(1:*))
plot(img)
library(sf)
sample <- st_read("******gpkg")
sample  = as(sample, 'Spatial')
sample$class = as.factor(sample$class) ; responseCol <- "class"
pixels = data.frame(matrix(vector(), nrow = 0, ncol = length(names(img)) +
1))

# A function to overlap de sample polygons on the image.
for (i in 1:length(unique(sample[[responseCol]]))){
  category <- unique(sample[[responseCol]])[i]
  categorymap <- sample[sample[[responseCol]] == category,]
  dataSet <- extract(img, categorymap)
  dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
  if(is(sample, "SpatialPointsDataFrame")){
    dataSet <- cbind(dataSet, class = as.numeric(category))
    pixeles <- rbind(pixeles, dataSet)
  }
  if(is(sample, "SpatialPolygonsDataFrame")){
    dataSet <- lapply(dataSet, function(x){cbind(x, class =
as.numeric(rep(category, nrow(x))))})
    df <- do.call("rbind", dataSet)
    pixels <- rbind(pixeles, df)
  }
}

# Define a number of pixels to use in the training of the model. More
pixels, more accuracy but more computational time.
n <- 1000

# Take a subsample in order to reduce computational times.
spixeles <- pixeles[sample(1:nrow(pixeles), n ), ]

# Train model.
modFit_rf <- train(as.factor(class) ~ ., method = "nnet", data = spixeles)
modFit_rf

# Predict.
beginCluster()
preds_rf <- clusterR(img, raster::predict, args = list(model = modFit_rf))
endCluster()

# Mapping results
maxpixels =  1e+06
m <- leaflet() %>%
  addProviderTiles('Esri.WorldImagery') %>%
  addRasterImage(preds_rf , opacity = 0.6) %>%
  addScaleBar(position = "topright") %>%
  addMiniMap( position = "bottomleft")
m

# Export raster
writeRaster(preds_rf, "estimacion.tif")



*Juan Pablo Carranza*
Mgter. en Administración Pública
Lic. en Economía




El mar., 13 ago. 2019 a las 5:50, Enoch Gyamfi Ampadu (<[hidden email]>)
escribió:

> Dear List,
>
> Please I am now learning R to apply to my analysis. I am doing a forest
> cover classification using Random forest. So far I have stacked the image,
> done atmospheric correction, and clipped out my study area. I have created
> some training sites of the cover classes in ArcMap and imported into R as
> well as the clipped image of my study sites. I have been doing try and
> error with some online codes. I will be glad if you could provide me with
> some codes to apply.
>
> Thank you.
>
> Best regards,
>
> Enoch
>
> --
> *Enoch Gyamfi - Ampadu*
>
> *Geography & Environmental Sciences*
>
> *College of Agriculture, Engineering & Science*
>
> *University of KwaZulu-Natal, Westville Campus*
>
> *Private Bag X54001*
> *Durban, South Africa **– 4000**.*
> *Phone: +27 835 828255*
>
> *email: [hidden email] <[hidden email]>*
>
>
> *skype: enoch.ampadu*
> *The highest evidence of nobility is self-control*.
>
> *A simple act of kindness creates an endless ripple*.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Random forest for forest cover classification

Carlos Alberto Arnillas-2
Hello Enoch
I think segmentation is a good idea, it will group pixels by similar
characteristics, creating polygons (don't forget to validate the
generated polygons!).
then you can do classification on those polygons using polygons properties
(including shape indices, but also the values obtained from the colours of
the image) using random forest.

Carlos Alberto

On Tue, Aug 13, 2019 at 8:41 AM Juan Pablo Carranza <[hidden email]>
wrote:

> Dear Enoch, I strongly recomend to try an segmentation approach instead a
> classification. However, here's some code you can use:
>
> library(rgdal)
> library(raster)
> library(caret)
> library(kerndwd)
> library(sp)
> library(RColorBrewer)
> library(dplyr)
> library(leaflet)
> library(sf)
> img <- brick("******.tiff")
> names(img) <- paste0("B", c(1:*))
> plot(img)
> library(sf)
> sample <- st_read("******gpkg")
> sample  = as(sample, 'Spatial')
> sample$class = as.factor(sample$class) ; responseCol <- "class"
> pixels = data.frame(matrix(vector(), nrow = 0, ncol = length(names(img)) +
> 1))
>
> # A function to overlap de sample polygons on the image.
> for (i in 1:length(unique(sample[[responseCol]]))){
>   category <- unique(sample[[responseCol]])[i]
>   categorymap <- sample[sample[[responseCol]] == category,]
>   dataSet <- extract(img, categorymap)
>   dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
>   if(is(sample, "SpatialPointsDataFrame")){
>     dataSet <- cbind(dataSet, class = as.numeric(category))
>     pixeles <- rbind(pixeles, dataSet)
>   }
>   if(is(sample, "SpatialPolygonsDataFrame")){
>     dataSet <- lapply(dataSet, function(x){cbind(x, class =
> as.numeric(rep(category, nrow(x))))})
>     df <- do.call("rbind", dataSet)
>     pixels <- rbind(pixeles, df)
>   }
> }
>
> # Define a number of pixels to use in the training of the model. More
> pixels, more accuracy but more computational time.
> n <- 1000
>
> # Take a subsample in order to reduce computational times.
> spixeles <- pixeles[sample(1:nrow(pixeles), n ), ]
>
> # Train model.
> modFit_rf <- train(as.factor(class) ~ ., method = "nnet", data = spixeles)
> modFit_rf
>
> # Predict.
> beginCluster()
> preds_rf <- clusterR(img, raster::predict, args = list(model = modFit_rf))
> endCluster()
>
> # Mapping results
> maxpixels =  1e+06
> m <- leaflet() %>%
>   addProviderTiles('Esri.WorldImagery') %>%
>   addRasterImage(preds_rf , opacity = 0.6) %>%
>   addScaleBar(position = "topright") %>%
>   addMiniMap( position = "bottomleft")
> m
>
> # Export raster
> writeRaster(preds_rf, "estimacion.tif")
>
>
>
> *Juan Pablo Carranza*
> Mgter. en Administración Pública
> Lic. en Economía
>
>
>
>
> El mar., 13 ago. 2019 a las 5:50, Enoch Gyamfi Ampadu (<[hidden email]
> >)
> escribió:
>
> > Dear List,
> >
> > Please I am now learning R to apply to my analysis. I am doing a forest
> > cover classification using Random forest. So far I have stacked the
> image,
> > done atmospheric correction, and clipped out my study area. I have
> created
> > some training sites of the cover classes in ArcMap and imported into R as
> > well as the clipped image of my study sites. I have been doing try and
> > error with some online codes. I will be glad if you could provide me with
> > some codes to apply.
> >
> > Thank you.
> >
> > Best regards,
> >
> > Enoch
> >
> > --
> > *Enoch Gyamfi - Ampadu*
> >
> > *Geography & Environmental Sciences*
> >
> > *College of Agriculture, Engineering & Science*
> >
> > *University of KwaZulu-Natal, Westville Campus*
> >
> > *Private Bag X54001*
> > *Durban, South Africa **– 4000**.*
> > *Phone: +27 835 828255*
> >
> > *email: [hidden email] <[hidden email]>*
> >
> >
> > *skype: enoch.ampadu*
> > *The highest evidence of nobility is self-control*.
> >
> > *A simple act of kindness creates an endless ripple*.
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Random forest for forest cover classification

Jin Li
In reply to this post by Enoch Gyamfi Ampadu
Hi Enoch,
Here is a book chapter that provides R scripts for using random forest to
classify categorical data: 'Predicting seabed hardness using random forest
in R' 2013.

   - DOI:
   - 10.1016/B978-0-12-411511-8.00011-6.
   - In book: Data Mining Applications with R.
   -
   - Publisher: Elsevier.
   - Editors: Y. Zhao and Y. Cen

If you are looking for further materials, the R package, spm, provides more
advanced functions, including RFcv with an example for categorical data.

Hope this helps.
Kind regards,
Jin


On Tue, 13 Aug. 2019, 18:50 Enoch Gyamfi Ampadu, <[hidden email]> wrote:

> Dear List,
>
> Please I am now learning R to apply to my analysis. I am doing a forest
> cover classification using Random forest. So far I have stacked the image,
> done atmospheric correction, and clipped out my study area. I have created
> some training sites of the cover classes in ArcMap and imported into R as
> well as the clipped image of my study sites. I have been doing try and
> error with some online codes. I will be glad if you could provide me with
> some codes to apply.
>
> Thank you.
>
> Best regards,
>
> Enoch
>
> --
> *Enoch Gyamfi - Ampadu*
>
> *Geography & Environmental Sciences*
>
> *College of Agriculture, Engineering & Science*
>
> *University of KwaZulu-Natal, Westville Campus*
>
> *Private Bag X54001*
> *Durban, South Africa **– 4000**.*
> *Phone: +27 835 828255*
>
> *email: [hidden email] <[hidden email]>*
>
>
> *skype: enoch.ampadu*
> *The highest evidence of nobility is self-control*.
>
> *A simple act of kindness creates an endless ripple*.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo