【问题标题】:Use SVM predict raster file in R在 R 中使用 SVM 预测栅格文件
【发布时间】:2018-02-04 04:51:27
【问题描述】:

我是 R 新手。我已经在 R 中有一个 SVM 模型。现在,我有两个光栅图像,一个是高程,另一个是坡度。海拔和坡度将用作 SVM 的预测因子。而且我还想将结果绘制成地图。

现在我的代码如下,但是两个光栅图像输入的预测都返回0。应该是0或1。有什么问题吗?

library("e1071")
tornado=read.csv(file="~/Desktop/new.csv",header=TRUE,sep=",")

err<- rep(0,5)
m<-0

for (i in c(1:5)) {
 #split the data sets into testing and training
 training.indices <- sample(nrow(tornado), 1800)
 training <- rep(FALSE, nrow(tornado))
 training[training.indices] <- TRUE

 tornado.input<- tornado[training,]
 tornado.input=data.frame(tornado.input)
 tornado=data.frame(tornado)

 tornado$Sig <- factor(tornado$Sig)

 model <- svm(Sig~slope+elevation, data=tornado.input)

 pred<- predict(model, tornado[!training,] )

 ConfM1<- table(tornado$Sig[!training], pred=pred)

 err[i]<-(sum(ConfM1)-sum(diag(ConfM1)))/sum(ConfM1)

}

library("raster")
library("rgdal")
elevation <- raster("~/Desktop/elevation.tif")
slope<- raster("~/Desktop/slope.tif")
#plot(elevation)
#plot(slope)

logo <- brick(elevation, slope)

r1 <- predict(logo,model)

plot(r1)

【问题讨论】:

    标签: r svm r-raster


    【解决方案1】:

    也许现在回答这个问题有点晚了,但我遇到了同样的问题。 raster::predict 函数似乎没有提供与 stats:predict 相同的输出。 我的替代解决方案是简单地从预测栅格(坡度和高程)中提取值,然后使用 ggplot 在空间上投影结果。

    ####Convert raster into dataframe
    logo_df <- as.data.frame(values(logo))
    logo_df[c("x","y")] <- coordinates(logo)
    logo_df <- logo_df[complete.cases(logo_df),] # in case you had holes in your raster
    
    #### predict to this new data
    pred <- predict(model, logo_df, probability = T)
    logo_df$svm.fit <- attr(pred, "probabilities")[,2]
    
    ###map the predictions
    ggplot(logo_df, aes(x,y,fill=svm.fit)) + 
      geom_tile() + 
      scale_fill_gradientn(colours = rev(colorRamps::matlab.like(100))) +
      coord_fixed()
    

    【讨论】:

      【解决方案2】:

      我遇到了这个问题,发现当我将 RasterStack 的层重命名为它们的变量名并添加类型选项时,它起作用了!

      例如

      names(logo)<-c("elevation","slope")
      r1<-predict(logo,model,type="response")
      

      【讨论】:

        猜你喜欢
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        相关资源
        最近更新 更多