2016-05-31 6 views
0

Ich schreibe ein R-Skript, um die Random Forest-Klassifizierung mehrmals für meinen Datensatz auszuführen. Ich möchte einen Durchschnitt von mindestens 10 Läufen verwenden, um robustere Ergebnisse zu erhalten. Also ich habe diese Funktion mit for-Schleife, die Random Forest-Klassifizierer ausgeführt wird, so oft ich es wünsche (n = Iterationen).Schreiben von zufälligen Gesamtstrukturergebnissen in eine Datei

iterateRandomForest <- function (samples,iterations,output_text,outname,pVSURF,b) { 
    for (i in (1: iterations)) { 

    cat("\n Loop starts", "\n", file=output_text,append=TRUE)  
    time <- toString(Sys.time()) 
    cat(time,"\n", file=output_text,append=TRUE) 
    cat("Iteration number ",i," for variable set: ", outname, "\n", sep="",file=output_text,append=TRUE) 

    load(pVSURF) 
    sel.vars <- x$varselect.pred + 1 
    colnames(samples[,sel.vars]) 

    ptm <- proc.time()                # Start timer to calculate processing length 
    (rf.final_ntree501 = randomForest(samples[,"species_na"], x=samples[,sel.vars], 
         ntree=b, importance=TRUE, norm.votes=TRUE, proximity=TRUE)) # Run randomForest 

    ### PROBLEM HERE 
    cat(rf.final_ntree501,file=output_text,append=TRUE) 
    ### PROBLEM ENDS 

    cat("Processing time: ",proc.time() - ptm, "\n", file=output_text,append=TRUE)  # Stop timer 
    cat("Loop ends\n", file=output_text,append=TRUE) 
    } 
} 

Normalerweise können Sie schreiben Sie einfach den Namen der erstellten Zufallswald Objekt (rf.final_ntree501) die Ergebnisse wie folgt drucken:

Call: 
    randomForest(x = samples[, sel.vars], y = samples[, "species_na"],  ntree = b, importance = TRUE, proximity = TRUE, norm.votes = TRUE) 
      Type of random forest: classification 
       Number of trees: 501 
No. of variables tried at each split: 4 

    OOB estimate of error rate: 45.43% 
Confusion matrix: 
       Acacia mearnsii Cupressus lusitanica Eucalyptus sp. Euphorbia sp. Ficus sp. Grevillea robusta Maesa lanceolata other Persea americana class.error 
Acacia mearnsii     34     1    3    0   0     7    0 28    0 0.5342466 
Cupressus lusitanica    4     3    8    0   0    13    0 16    0 0.9318182 
Eucalyptus sp.      5     0    35    0   0    15    0  8    0 0.4444444 
Euphorbia sp.      0     0    1   16   0     2    0 15    0 0.5294118 
Ficus sp.       0     0    0    1   1     5    0 17    0 0.9583333 
Grevillea robusta     5     2    3    0   1    91    0 29    1 0.3106061 
Maesa lanceolata     4     0    0    0   0     2    0 14    0 1.0000000 
other        16     0    3    4   1    27    1 189    1 0.2190083 
Persea americana     5     1    0    0   0     6    0 33    1 0.9782609 

So wünsche ich schreibe diese Information in eine Datei innerhalb der Schleife (siehe PROBLEM HIER Teil). Ich weiß, dass ich RF-Objekt nicht direkt schreiben kann, wie es eine Liste ist. Wenn ich versuche, die Konfusionsmatrix separat mit rf.final_ntree501 $ confusion mit cat zu speichern. Es wird die Information speichern, aber es wird die Formulierung der Matrix durcheinander bringen und alle Informationen auf eine Zeile setzen, ausgenommen die Klassennamen.

Hat jemand gute Ideen, wie man richtig damit umgeht?

Cheers, Rami

Antwort

1

Verwenden capture.output() statt cat() das Ergebnis in eine Datei, wie es in der Konsole schreiben angezeigt wird.

# generate random data 
samples <- matrix(runif(675), ncol = 9) 
resp <- as.factor(sample(LETTERS[1:9], 75, replace = TRUE)) 

# random forest 
rf <- randomForest(x = samples, y = resp, ntree = 501, 
    importance = TRUE, norm.votes = TRUE, proximity = TRUE) 

# save desired information into a file 
capture.output(rf, file = output_text, append = TRUE) 

Speichern der Konfusionsmatrix getrennt, können Sie write.table() verwenden. Das Ergebnis wird auf eine maschinenlesbare Weise mit einem gewählten Trennzeichen (Registerkarte im Beispiel) formatiert.

write.table(rf$confusion, file = "filename.txt", sep = "\t") 
+0

Dies löste das Problem! Vielen Dank :) – RaimoGIS

+0

Betrachten Sie die Antwort dann annehmen :) – nya

+1

Oh ja, sorry :). – RaimoGIS