Saving and loading a model in R

风格不统一 提交于 2019-11-29 05:13:54

问题


When working with caret, how can I save a model after training, and load it later (e.g. in a different session) for prediction?


回答1:


A better solution nowadays is to use saveRDS to save and readRDS to read:

saveRDS(model, "model.rds")
my_model <- readRDS("model.rds")

This lets you to choose a new name for the object (you don't need to remember the name you used when you saved it)




回答2:


The correct syntax would be to use:

save(model, file="model.Rdata")

Thereafter, it can be loaded using the load() command.




回答3:


The following code assumes that your model's variable name is 'model':

save(model, "model.RData")

This will save your model as "model.RData" in the current working directory. You can find out what the working directory is by issuing the following:

getwd()

To load it back in, ensure that your model is saved in your working directory and issue:

load("model.RData")


来源:https://stackoverflow.com/questions/14761496/saving-and-loading-a-model-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!