为什么load(...)返回对象的字符名而不是对象本身?

时间:2021-10-23 20:46:15

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

使用R中的包e1071创建svm模型。要使用该模型,我需要保存它并根据需要读取。该软件包有write.svm,但没有read.svm。如果我使用

model <- svm(x, y)

save(model, 'modelfile.rdata')
M <- load('modelfile.rdata')

object M contains just the word 'model'.

对象M只包含“模型”一词。

How to save the svm model and read back later, to apply to some new data?

如何保存svm模型并稍后回读,以应用于一些新数据?

2 个解决方案

#1


5  

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.

因此,“模型”确实是M的预期价值。您的svm已经恢复为原始名称,即模型。

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和readRDS。

saveRDS(model, 'modelfile.rds')
M <- readRDS('modelfile.rds')

and M should contain your svm model.

和M应该包含你的svm模型。

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.

我更喜欢saveRDS和readRDS,因为我知道我在工作空间中创建了什么对象 - 请参阅Gavin Simpson的博客文章(在他的回答中链接)进行详细讨论。

#2


3  

You misunderstand what load does. It restores an object to the same name it had when you save()d it. What you are seeing in M is the return value of the load() function. Calling load() has the additional side effect of loading the object back under the same name that it was saved with.

你误解了负载的作用。它将对象恢复为与save()d时的名称相同。你在M中看到的是load()函数的返回值。调用load()会产生额外的副作用,即以与保存对象相同的名称加载对象。

Consider:

require("e1071")
data(iris)

## classification mode
# default with factor response:
model <- svm (Species~., data=iris)
## Save it
save(model, file = "my-svm.RData")
## delete model
rm(model)
## load the model
M <- load("my-svm.RData")

Now look at the workspace

现在看看工作区

> ls()
[1] "iris"  "M"     "model"

Hence model was restored as a side effect of load().

因此,模型被恢复为load()的副作用。

From ?load we see the reason M contains the name of the objects created (and hence saved originally)

从?load我们看到M包含所创建对象的名称的原因(因此最初保存)

Value:

     A character vector of the names of objects created, invisibly.

If you want to restore an object to a new name, use saveRDS() and readRDS():

如果要将对象还原为新名称,请使用saveRDS()和readRDS():

saveRDS(model, "svm-model.rds")
newModel <- readRDS( "svm-model.rds")
ls()

> ls()
 [1] "iris"     "M"        "model"    "newModel"

If you want to know more about saveRDS() and readRDS() see the relevant help ?saveRDS() and you might be interested in a blog post I wrote on this topic.

如果您想了解有关saveRDS()和readRDS()的更多信息,请参阅相关帮助?saveRDS(),您可能对我在此主题上撰写的博客文章感兴趣。

#1


5  

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.

因此,“模型”确实是M的预期价值。您的svm已经恢复为原始名称,即模型。

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和readRDS。

saveRDS(model, 'modelfile.rds')
M <- readRDS('modelfile.rds')

and M should contain your svm model.

和M应该包含你的svm模型。

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.

我更喜欢saveRDS和readRDS,因为我知道我在工作空间中创建了什么对象 - 请参阅Gavin Simpson的博客文章(在他的回答中链接)进行详细讨论。

#2


3  

You misunderstand what load does. It restores an object to the same name it had when you save()d it. What you are seeing in M is the return value of the load() function. Calling load() has the additional side effect of loading the object back under the same name that it was saved with.

你误解了负载的作用。它将对象恢复为与save()d时的名称相同。你在M中看到的是load()函数的返回值。调用load()会产生额外的副作用,即以与保存对象相同的名称加载对象。

Consider:

require("e1071")
data(iris)

## classification mode
# default with factor response:
model <- svm (Species~., data=iris)
## Save it
save(model, file = "my-svm.RData")
## delete model
rm(model)
## load the model
M <- load("my-svm.RData")

Now look at the workspace

现在看看工作区

> ls()
[1] "iris"  "M"     "model"

Hence model was restored as a side effect of load().

因此,模型被恢复为load()的副作用。

From ?load we see the reason M contains the name of the objects created (and hence saved originally)

从?load我们看到M包含所创建对象的名称的原因(因此最初保存)

Value:

     A character vector of the names of objects created, invisibly.

If you want to restore an object to a new name, use saveRDS() and readRDS():

如果要将对象还原为新名称,请使用saveRDS()和readRDS():

saveRDS(model, "svm-model.rds")
newModel <- readRDS( "svm-model.rds")
ls()

> ls()
 [1] "iris"     "M"        "model"    "newModel"

If you want to know more about saveRDS() and readRDS() see the relevant help ?saveRDS() and you might be interested in a blog post I wrote on this topic.

如果您想了解有关saveRDS()和readRDS()的更多信息,请参阅相关帮助?saveRDS(),您可能对我在此主题上撰写的博客文章感兴趣。