1

I have the following dataframe containing training data to be used in a SVM classification. The first column ID must be avoided and the column Class contains the response variable.

> training_S
      ID Coherence_VV_Stack2.1 Coherence_VV_Stack2.2 Coherence_VV_Stack2.3 Coherence_VV_Stack2.4 Coherence_VV_Stack2.5 Class
1      1            0.37249821            0.40778583            0.61994231            0.26051590            0.66157836     1
2      1            0.24540116            0.25959459            0.10963936            0.37917945            0.42228147     1
3      1            0.41568330            0.34043241            0.25767127            0.24456473            0.75776720     1
4      1            0.74053413            0.55324554            0.49598694            0.27220318            0.69603848     1
5      1            0.87046194            0.93123823            0.89269984            0.63971967            0.83846021     1
6      1            0.59682816            0.55433798            0.39686579            0.77071124            0.51787984     1
7      1            0.59135294            0.64881891            0.58359951            0.75902539            0.54283893     1
8      1            0.62668669            0.79115856            0.50775266            0.80211383            0.63855529     1
9      1            0.77357692            0.68257052            0.74601513            0.85265881            0.43013501     1
10     1            0.50508022            0.43725273            0.26380399            0.40273756            0.32667077     1
11     1            0.79765278            0.78279233            0.43636456            0.97429311            0.54599607     1

I am trying to follow a previous approach used in another classification to train my model:

SVMachine<-svm(x=training_S[ ,c(2:(length(training_S)-1))], y=training_S$Class, gamma = 10^(-6:-1), cost = 10^(-1:1))

But I am definetly doing something wrong as I get

Error in predict.svm(ret, xhold, decision.values = TRUE) : 
  Model is empty!

Any suggestion on how to run a SVM classification using as input the data structure I have?

-- EDIT --

To better check my data, here is the str

> str(training_S)
'data.frame':   86745 obs. of  7 variables:
 $ ID                   : num  1 1 1 1 1 1 1 1 1 1 ...
 $ Coherence_VV_Stack2.1: num  0.372 0.245 0.416 0.741 0.87 ...
 $ Coherence_VV_Stack2.2: num  0.408 0.26 0.34 0.553 0.931 ...
 $ Coherence_VV_Stack2.3: num  0.62 0.11 0.258 0.496 0.893 ...
 $ Coherence_VV_Stack2.4: num  0.261 0.379 0.245 0.272 0.64 ...
 $ Coherence_VV_Stack2.5: num  0.662 0.422 0.758 0.696 0.838 ...
 $ Class                : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...

and here is the str of the data proposed by @spacedman:

> str(training_S2)
'data.frame':   10 obs. of  5 variables:
 $ ID   : num  1 1 1 1 1 1 1 1 1 1
 $ C1   : num  0.266 0.372 0.573 0.908 0.202 ...
 $ C2   : num  0.206 0.177 0.687 0.384 0.77 ...
 $ C3   : num  0.935 0.212 0.652 0.126 0.267 ...
 $ Class: num  1 1 1 1 1 2 2 2 2 2

I see two main differences. First, my Class variable only contains 1 value (not 2). Second, I have defined the Class variable as factor to make sure SVM runs a classification? Could that be the problem? Is not it suppose to be the righ procedure?

When trying to run withouth Class being changed to factor, I get the following error:

Error in svm.default(x = training_S[, c(1:(length(training_S) - 1))],  : 
  NA/NaN/Inf in foreign function call (arg 4)

However, checking my data for NA/NaN or infinite does not reveal any wrong value

> apply(training_S, 2, function(x) any(is.na(x)))
Coherence_VV_Stack2.1 Coherence_VV_Stack2.2 Coherence_VV_Stack2.3 Coherence_VV_Stack2.4 Coherence_VV_Stack2.5 
                FALSE                 FALSE                 FALSE                 FALSE                 FALSE 
                Class 
                FALSE 
> apply(training_S, 2, function(x) any(is.nan(x)))
Coherence_VV_Stack2.1 Coherence_VV_Stack2.2 Coherence_VV_Stack2.3 Coherence_VV_Stack2.4 Coherence_VV_Stack2.5 
                FALSE                 FALSE                 FALSE                 FALSE                 FALSE 
                Class 
                FALSE 
> apply(training_S, 2, function(x) any(is.infinite(x)))
Coherence_VV_Stack2.1 Coherence_VV_Stack2.2 Coherence_VV_Stack2.3 Coherence_VV_Stack2.4 Coherence_VV_Stack2.5 
                FALSE                 FALSE                 FALSE                 FALSE                 FALSE 
                Class 
                FALSE 
  • What package does your svm function come from? And is this really a GIS-related question? Perhaps you should ask on StackOverflow instead? – Spacedman Sep 17 '18 at 16:05
  • 1
    This reproducible data frame set.seed(1); training_S = data.frame(ID=rep(1,10), C1 = runif(10), C2=runif(10), C3=runif(10), Class=c(rep(1,5),rep(2,5))) runs e1071::svm with no problems. Must be something in your data which we don't have... – Spacedman Sep 17 '18 at 16:09
  • 1
    Are you sure that the gamma and cost parameters can take multiple values? – Jeffrey Evans Sep 17 '18 at 17:14
  • 1
    @JeffreyEvans I don't get any errors running that same svm call with those (multiple) parameters with the sample data frame constructed in my comment. Can't reproduce that error. – Spacedman Sep 17 '18 at 17:48
  • I am using svm from e1071. Using your example, my code runs properly. I have edited the questions to add extra information comparing your test dataset and mine – GCGM Sep 18 '18 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.