Write SVM Object to File
write.svm.RdThis function exports an SVM object (trained by svm) to two
specified files. One is in the format that the
function 'svm_load_model()' of libsvm can read. The other is for
scaling data, containing a data with centers and scales for all variables.
Usage
write.svm(object, svm.file = "Rdata.svm",
scale.file = "Rdata.scale", yscale.file = "Rdata.yscale")Details
This function is useful when SVM models trained in R shall be used in other environments. The SVM model is saved in the standard format of libsvm. The scaling data are written to separate files because scaling data are not included in the standard format of libsvm. The format of the scaling data file is a n times 2 matrix: the n-th row corresponds to the n-th dimension of the data, the columns being formed of the corresponding mean and scale. If scaling information for the dependent variable exists (in case of regression models), it is stored in yet another file (1 times 2 matrix).
Author
Tomomi TAKASHINA (based on 'predict.svm' by David Meyer) t.takashina@computer.org
Examples
data(iris)
attach(iris)
#> The following objects are masked from iris (pos = 4):
#>
#> Petal.Length, Petal.Width, Sepal.Length, Sepal.Width, Species
#> The following objects are masked from iris (pos = 6):
#>
#> Petal.Length, Petal.Width, Sepal.Length, Sepal.Width, Species
## classification mode
# default with factor response:
model <- svm (Species~., data=iris)
# export SVM object to (temporary) files
svm_file <- tempfile()
scale_file <- tempfile()
write.svm(model, svm.file = svm_file, scale.file = scale_file)
# read scale file
# the n-th row is corresponding to n-th dimension. The 1st column contains the
# center value, the 2nd column is the scale value.
read.table(scale_file)
#> V1 V2
#> 1 5.843333 0.8280661
#> 2 3.057333 0.4358663
#> 3 3.758000 1.7652982
#> 4 1.199333 0.7622377
# clean up
unlink(svm_file)
unlink(scale_file)