gpt4 book ai didi

R 错误 : Expecting a single string value: [type=character; extent=5]

转载 作者:行者123 更新时间:2023-12-04 12:27:38 28 4
gpt4 key购买 nike

我正在使用 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].
有人可以告诉我如何解决这个错误吗?
谢谢
引用:
  • https://www.rdocumentation.org/packages/corels/versions/0.0.2/topics/corels
  • https://cran.r-project.org/web/packages/corels/index.html
  • 最佳答案

    问题是您正试图将数据集传递给 corels()函数,但是它希望您传递文件名。 Data Format corels 的 github 部分软件有关于如何格式化数据结构的详细信息。然后这些应该保存在文件中,然后传递给 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")

    关于R 错误 : Expecting a single string value: [type=character; extent=5],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69385603/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com