【问题标题】:Prequential Evaluation in R Causing Error MessageR中的预先评估导致错误消息
【发布时间】:2018-09-22 17:42:00
【问题描述】:

我正在使用 RMOA 在 R 中实现先行评估。

这对于这个问题来说并不太重要,但基本上这通过首先测试然后训练模型中的每个观察来评估流挖掘分类器的性能流。

我已基本完成,但在进行预测时遇到此错误消息:

要替换的项目数不是替换长度的倍数

我正在使用 Iris 数据集,但奇怪的是,它可以预测类别标签并训练前 50 个观察值,正如我所期望的那样。但是,对于剩下的 100 个,它会产生上述错误。数据集按默认顺序排列:

Label        Number

Setosa       50
Versicolor   50
Virginica    50

这意味着无论出于何种原因,该模型都会对所有 Setosa 观察结果进行预测,然后对其他两个类别标签产生此错误。如果我将行随机化,我会遇到同样的问题;一旦遇到第二类标签,就会产生错误。

我已阅读有关此错误的类似问题,例如 this。现有问题似乎都没有解决分类背景下的问题。

相关R代码:

require(RMOA)
require(stream)

#data<-iris[sample(nrow(iris)),] #use this for randomising the iris dataset
df = datastream_dataframe(as.data.frame(iris))

opts<-MOAoptions(model="HoeffdingTree")
tree<-HoeffdingTree(control = opts)
mymodel <- NULL

i<- 1
#Iterate over the stream until the stream is empty
repeat
{
    element <- df$get_points(1)

    #Check to see if stream is empty - not elegant but it works just now
    if(is.null(element$Species)){
        break
    }

    #Prequential evaluation
    tryCatch(
    {
        #First test
        pred <- RMOA:::predict.MOA_trainedmodel(mymodel, element, type="response")
        cat("Tested element ", i, "Pred: ",pred, ". Label: ", element$Species,"\n")

        #Then train
        #If first element in stream, the model is initially trained in the error block below
        if(!i==1){
            mymodel <<- trainMOA(model = mymodel$model, 
                formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 
                data = datastream_dataframe(element))
            cat("Trained model using element ", i,"\n")
        }
    },

    error = function(err){
        cat("Error processing element ", i,"\n")
        if(i==1){
            cat("The model was initially untrained.\n")
            mymodel <<- trainMOA(model = tree, Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = datastream_dataframe(element))
        }
        else{
            message(err)
        }
    })#end try-catch block

    i<<- i+ 1
}#end while

请注意,模型最初设置为null。对于第一次观察,由于模型未经训练,因此尝试的预测将产生错误。这是在try-catch 中处理的。所有后续观察都应在训练前进行测试。

我的模型有什么问题会导致此错误?

【问题讨论】:

  • 在该警告之前没有打印“In ... :”吗?这告诉你在哪里看。在发出警告的行之前调用browser(),并检查对象的长度。此外,问题的最小示例更有可能得到答案。
  • 之前没有打印过“ln”。至于最少,通常我会删掉多余的文本,但我试图避免关于为什么训练/测试结构的潜在问题
  • 您看到的警告并非特定于您正在执行的操作。当关于向量长度的假设被证明是错误的时,它通常会出现。您应该逐步检查您的代码并找出哪一行引发了警告。这可能是您正在使用的软件包中的一个缺陷;在这种情况下,你最好让包维护者知道这个错误。

标签: r machine-learning classification


【解决方案1】:

经过一番挖掘,我发现了最初在MOA-users Google group 上报告的错误。

issue I opened on the GitHub repository 中所述,包作者已将问题作为提交39665f9 中的错误解决。

通过 CRAN 安装 RMOA 会导致 1.0 版仍然包含此错误。为避免这种情况,请安装 1.1 版,该版本可从包 GitHub repository 或使用以下命令获得

install_github("jwijffels/RMOA", subdir="RMOA/pkg")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多