【问题标题】:R Error: Expecting a single string value: [type=character; extent=5]R 错误:期望单个字符串值:[type=character;范围=5]
【发布时间】:2021-11-21 21:43:03
【问题描述】:

我正在使用 R 编程语言。我在 R 中使用“CORELS”库,这里是一个例子(CORELS 是一个类似于决策树的统计模型):

library(corels)

logdir <- tempdir()
rules_file <- system.file("sample_data", "compas_train.out", package="corels")
labels_file <- system.file("sample_data", "compas_train.label", package="corels")
meta_file <- system.file("sample_data", "compas_train.minor", package="corels")

stopifnot(file.exists(rules_file),
          file.exists(labels_file),
          file.exists(meta_file),
          dir.exists(logdir))

corels(rules_file, labels_file, logdir, meta_file,
       verbosity_policy = "silent",
       regularization = 0.015,
       curiosity_policy = 2,   # by lower bound
       map_type = 1)       # permutation map
cat("See ", logdir, " for result file.")

输出可以在这里查看:

OPTIMAL RULE LIST
if ({sex:Male,juvenile-crimes:>0}) then ({recidivate-within-two-years:Yes})
else if ({priors:>3}) then ({recidivate-within-two-years:Yes})
else ({recidivate-within-two-years:No})

我的问题:对于上述函数的语法如何工作,我仍然有些困惑。例如,我正在尝试在“iris”数据集上使用上述函数:

data(iris)
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

现在,我尝试在 iris 数据集上应用“corles”函数:

logdir <- tempdir()
rules_file <- iris
labels_file <- colnames(iris)

corels(rules_file, labels_file, logdir, 
       verbosity_policy = "silent",
       regularization = 0.015,
       curiosity_policy = 2,   # by lower bound
       map_type = 1)       # permutation map
cat("See ", logdir, " for result file.")

但这会产生以下错误:

Error in corels(rules_file, labels_file, logdir, verbosity_policy = "silent",  : 
  Expecting a single string value: [type=character; extent=5].

谁能告诉我如何解决这个错误?

谢谢

参考资料:

【问题讨论】:

  • 我遇到了类似的(令人困惑的)错误。这是解析错误本身的方法。显然,它期待一个字符串。但是,然后它列出了你给它的内容,而不是一个范围为 5(这意味着 5 个元素)的数组,每个元素都是字符类型。
  • 首先,我很困惑,因为我认为它是 ASKING FOR a string, type_character with extent=5 并假设它意味着一个 5 个字符的字符串。只有当我意识到 5(在我的情况下是 2)是元素的数量时,我才意识到括号中的部分描述了我错误地给出的数组,而不是它想要的字符串。

标签: r function search optimization tree


【解决方案1】:

问题是您试图将数据集传递给corels() 函数,但它希望您传递文件名corels 软件的 github 的 Data Format 部分详细介绍了如何格式化数据结构。这些应该保存在文件中,然后传递给corels()

数据文件中的每一行都应该包含一个用大括号括起来的规则名称,{},后跟一个 01 以指示该规则是否适用于给定的数据行.

这里有一个简单的脚本来构造一些:

library(corels)

# Write a function to take a dataset & expression and format it correctly
make_rule <- function(data,expr){
  rule_name <- deparse1(substitute(expr))
  rule_name <- gsub(" ","",rule_name)
  out <- eval(substitute(expr),data)
  paste0("{",rule_name,"} ",paste0(1*out,collapse=" "))
}

# Create names for our rule/label files

rules_file <- "rule_data"
labels_file <- "label_data"

# Create some example rules (must always be binary operations)
iris_rules <- c(
  make_rule(iris,Sepal.Length < 5.84),
  make_rule(iris,Sepal.Width < 3.05),
  make_rule(iris,Petal.Length < 3.76),
  make_rule(iris,Petal.Width < 1.2)
)

#Label data appropriately. Must be a pair of rules
# where the first is the negative option & the 2nd is the
# positive outcome. Here we want to know how to find when
# the flower is a setosa
iris_labels <- c(
  make_rule(iris,Species != "setosa"),
  make_rule(iris,Species == "setosa")
)

# Save the data in the files
writeLines(iris_rules,rules_file)
writeLines(iris_labels,labels_file)


# reference the files and set verbosity to high so
# we get the full output
corels(rules_file, labels_file, ".",
       verbosity_policy = "loud")

【讨论】:

  • @Michael Barrowman:谢谢你的回复!两个问题
  • 1) 为什么这个步骤是必要的? iris_rules
  • 2) 这个库只能用于二进制分类吗?它不能处理多类输出?或者它只处理二进制分类?例如。 iris_labels
  • 1) 这是必要的,因为它需要采用那种格式。 make_rule() 函数处理这个问题。如果输出是合乎逻辑的,则此函数允许定义任何规则。 2) 关于多类标签,我无法回答这个问题,您必须阅读我链接到的文档才能了解corels 软件的功能。但是,据我了解,如果可能,您必须为每个类别定义标签(例如c(make_rule(iris,Species=="setosa"),make_rule(iris,Species=="versicolor"),make_rule(iris,Species=="virginica"))
  • 感谢您的回复!为什么你只决定定义 4 条规则?以这种格式定义它们有什么原因吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
  • 2019-09-19
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
相关资源
最近更新 更多