【问题标题】:Fail to implement SVM in R无法在 R 中实现 SVM
【发布时间】:2019-06-04 10:10:08
【问题描述】:

我正在尝试使用 svm 运行图像分类,但我遇到了一个错误,虽然已在此论坛上报告,但解决方案不适合我的情况。

我要分类的数据是2层的raster stack

> S1_images
class       : RasterStack 
dimensions  : 1000, 1414, 1414000, 2  (nrow, ncol, ncell, nlayers)
resolution  : 10, 10  (x, y)
extent      : 670860, 685000, 6163420, 6173420  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : X20180415_VH, X20180415_VV 
min values  : 1.621079e-05, 1.929869e-04 
max values  :      24.6396,     159.7452 

我的训练数据是使用多边形作为参考并提取这些位置的像素值获得的:

training_S<-raster::extract(S_images_t, training, df=TRUE)
training_S$Class<-factor(training_S$Class) 

> head(training_S)
  ID X20180415_VH X20180415_VV Class
1  1  0.006463605   0.05813200     1
2  1  0.006663103   0.06266786     1
3  1  0.007048910   0.06308612     1
4  1  0.006351015   0.04774158     1
5  1  0.006822301   0.05248845     1
6  1  0.007194918   0.05911565     1

> str(training_S)
'data.frame':   33239 obs. of  4 variables:
 $ ID          : num  1 1 1 1 1 1 1 1 1 1 ...
 $ X20180415_VH: num  0.00646 0.00666 0.00705 0.00635 0.00682 ...
 $ X20180415_VV: num  0.0581 0.0627 0.0631 0.0477 0.0525 ...
 $ Class       : Factor w/ 9 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...

tune.svm 为最佳参数选择之后,我创建了模型(到目前为止,一切都很好)

SVM<-svm(x=training_S[ ,c(1:(length(training_S)-1))], y=training_S$Class, gamma = 0.1, cost = 10)

接下来,我尝试使用predict对我的输入数据进行分类:

LC<-predict(S1_images, model=SVM, na.rm=TRUE) 

我的错误来了:

> LC<-predict(S1_images, model=SVM, na.rm=TRUE) 
Error in newdata[, object$scaled, drop = FALSE] : 
  (subscript) logical subscript too long

按照R Bloggers 的示例,我将raster stack 转换为数据框,并正确重命名列:

S1_images_df <- data.frame(getValues(S1_images))
names(S1_images_df) <- c("X20180415_VH", "X20180415_VV")

尝试再次运行分类时:

LC<-predict(SVM, S1_images_df) 

> LC<-predict(SVM, S1_images_df)
Error in newdata[, object$scaled, drop = FALSE] : 
  (subscript) logical subscript too long

关于我的数据的一些额外信息:

> str(training_S)
'data.frame':   33239 obs. of  4 variables:
 $ ID          : num  1 1 1 1 1 1 1 1 1 1 ...
 $ X20180415_VH: num  0.00646 0.00666 0.00705 0.00635 0.00682 ...
 $ X20180415_VV: num  0.0581 0.0627 0.0631 0.0477 0.0525 ...
 $ Class       : Factor w/ 9 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 

> str(S1_images_df)
    'data.frame':   1414000 obs. of  2 variables:
     $ X20180415_VH: num  0.005 0.00531 0.00514 0.0048 0.00461 ...
     $ X20180415_VV: num  0.0954 0.0947 0.0933 0.0952 0.0951 ...

> dim(training_S)
    [1] 33239     4


> dim(S1_images_df)
    [1] 1414000       2

我一直在查看这两个较旧的帖子,但不确定如何在我的情况下实施解决方案:

HereHere

【问题讨论】:

    标签: r machine-learning svm


    【解决方案1】:

    您在训练模型时似乎将ID 作为协变量。如果ID 有意义,并且您想将其包含在模型中,则需要在S1_images_df 中添加对应的ID 字段。更有可能的是,在将训练数据传递给 svm 时应该排除它:

    SVM<-svm(x=training_S[, -c(1, ncol(training_S))], y=training_S$Class, gamma = 0.1, cost = 10)
    

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2015-02-14
      • 2020-09-26
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2020-12-20
      • 2015-01-08
      • 2012-01-31
      相关资源
      最近更新 更多