The svm model is created with the package e1071 in R. To use the model, I need to save it and read as needed. The package has write.svm
, but does not have read.svm
. If I use
model <- svm(x, y)
save(model, 'modelfile.rdata')
M <- load('modelfile.rdata')
object M
contains just the word 'model'.
How to save the svm model and read back later, to apply to some new data?
names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object.
To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.
Look at the return value for the function load
in the help file:
Value:
A character vector of the names of objects created, invisibly.
So "model" is indeed the expected value of M
. Your svm has been restored under its original name, which is model
.
If you find it a bit confusing that load
does not return the object loaded but instead restores it under the name used in saving it, consider using saveRDS
and readRDS
.
saveRDS(model, 'modelfile.rds')
M <- readRDS('modelfile.rds')
and M
should contain your svm model.
I prefer saveRDS
and readRDS
because with them I know what objects I'm creating in my workspace - see the blog post of Gavin Simpson (linked in his answer) for a detailed discussion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With