1

I am trying run an image classification using svm but I am facing an error that, while being already reported on this forum, the solutions do not fit my case.

The data I want to classify is a raster stack of 2 layers:

> 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 

My training data is obtained using polygons as reference and extracting the pixel values at those locations:

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

and

> 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 ...

After tune.svm for best parameter choice, I create the model (so far, so good)

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

Next, I try to use predict to classify my input data:

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

and here comes my error:

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

Following the example of R Bloggers, I converted my raster stack into a dataframe, and rename the columns properly:

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

When trying to run the classification again:

LC<-predict(SVM, S1_images_df) 

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

Some extra information on my data:

> 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

I have been checking this two older posts but not sure how to implement there solution in my case:

Here and Here

GCGM
  • 901
  • 1
  • 17
  • 38

1 Answers1

1

It looks like you're including ID as a covariate when training the model. If ID is meaningful and you want to include it in the model, you need to add a corresponding ID field to S1_images_df. More likely, you should exclude it when passing your training data to svm:

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