2016-07-16 9 views
1

ich svm von e1071 für einen Datensatz wie folgt aus:Tune SVM in R - Abhängige Variable hat falschen Typ

sdewey <- svm(x = as.matrix(trainS), 
       y = trainingSmall$DEWEY, 
       type="C-classification") 

Das funktioniert gut, aber wenn ich versuche, die Kosten und Gamma wie diese Melodie :

svm_tune <- tune(svm, train.x=as.matrix(trainS), train.y=trainingSmall$DEWEY, type="C-classification", ranges=list(cost=10^(-1:6), gamma=1^(-1:1))) 

ich diesen Fehler:

Error in tune(svm, train.x = as.matrix(trainS), train.y = trainingSmall$DEWEY, : Dependent variable has wrong type!

Die Struktur meiner Trainingsdaten ist das, aber mit vielen weiteren Zeilen:

'data.frame': 1000 obs. of 1542 variables: 
$ women.prisoners         : int 1 0 0 0 0 0 0 0 0 0 ... 
$ reformatories.for.women       : int 1 0 0 0 0 0 0 0 0 0 ... 
$ women           : int 1 0 0 0 0 0 0 0 0 0 ... 
$ criminal.justice         : int 1 0 0 0 0 0 0 0 0 0 ... 
$ soccer           : int 0 1 0 0 0 0 0 0 0 0 ... 
$ coal.mines.and.mining       : int 0 0 1 0 0 0 0 0 0 0 ... 
$ coal            : int 0 0 1 0 0 0 0 0 0 0 ... 
$ engineering.geology        : int 0 0 1 0 0 0 0 0 0 0 ... 
$ family.violence         : int 0 0 0 1 0 0 0 0 0 0 ... 

Es ist ein Multi-Klassen-Problem. Ich bin nicht sicher, wie ich das lösen könnte oder ob es andere Möglichkeiten gibt, den optimalen Wert für die Kosten- und Gamma-Parameter herauszufinden.

Here is an example of my data und trainS ist, dass Daten ohne die ersten 4 Spalten (DEWEY, D1, D2 und D3)

Dank

Antwort

1
require(e1071) 

trainingSmall<-read.csv("trainingSmallExtra.csv") 

sdewey <- svm(x  = as.matrix(trainingSmall[,4:nrow(trainingSmall)]), 
       y  = trainingSmall$DEWEY, 
       type = "C-classification", 
       kernel = "linear" # same as no kernel 
      ) 

Dies funktioniert, weil svmDEWEY zu einem Faktor automatisch konvertiert hat.

Das Modell tune ist fehlgeschlagen, weil es, da es für die Benutzeranpassung erstellt wurde, darauf angewiesen ist, dass Sie den richtigen Datentyp angeben. Seit DEWEY war Ganzzahl statt factor es fehlgeschlagen. Wir können das beheben:

trainingSmall$DEWEY <- as.factor(trainingSmall$DEWEY) 

svm_tune <- tune(svm, train.x = as.matrix(trainingSmall[,4:nrow(trainingSmall)]), 
         train.y = trainingSmall$DEWEY, # the way I'm formatting your 
         kernel = "linear",   # code is Google's R style 
         type = "C-classification",  
         ranges = list(
             cost = 10^(-1:6), 
             gamma = 1^(-1:1) 
            ) 
       ) 
+1

Vielen Dank, das hat gut funktioniert. Ich erkannte, dass ich das type = "lineare" Flag verlassen habe, was keinen Sinn macht, da es nicht notwendig ist, Kosten und Gamma für diesen bestimmten Typ zu finden (ich habe versucht zu sehen, ob linearer schlechter/schlechter als RBF funktioniert). Ich habe die Frage bearbeitet, um das zu beheben. – moondaisy

Verwandte Themen