我正在使用 R
的 RMOA
包来实现具有保留评估功能的 Hoeffding Tree 流分类器。
一切都训练正确,除了当我尝试从保留的测试流中评估我的模型时,我收到以下错误消息:
Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('HoeffdingTree', 'MOA_classifier', 'MOA_model')"
检查了 this question 的答案,问题可能源于 predict()
方法同时存在于 stats
和 RMOA
包中。我尝试使用 ::
表示法来指定哪个包,但我似乎无法指向 RMOA Predict()
。我还尝试完全卸载 stats
但没有帮助。
有人知道如何直接指向 RMOA
的 predict()
,还是我的问题完全是由其他原因引起的?
我的 R 代码如下。我现在只是对 iris 数据集进行流式传输,并提取前 30 个流项目以用于保留评估。
holdout<-function(){
require("RMOA")
#Initialise streams
stream<-datastream_dataframe(iris)
test<-stream$get_points(n=30)
test<-datastream_dataframe(test)
#Specify model
mymodel<-HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
#Record execution time for training
start_time<-Sys.time()
while(!stream$finished)
{
mymodel <<- trainMOA(model=mymodel, formula = Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data=stream)
}
end_time<-Sys.time()
time_taken <- end_time - start_time
cat("Finished training. Elapsed time: ", time_taken)
#Empty vector to store individual accuracy results of holdout stream elements
accuracies<-c()
#Record the execution time of holdout evaluation
start_time<-Sys.time()
while(!test$finished)
{
samp<-test$get_points(n=1)
pred <- predict(mymodel, samp, type="response")
}
end_time<-Sys.time()
time_taken <- end_time - start_time
cat("Finished training. Elapsed time: ", time_taken)
}
最佳答案
RMOA 包中的预测方法是一个内部变量,您可以这样调用它:
RMOA:::predict.MOA_trainedmodel
完整示例:
library(RMOA)
data(iris)
stream <- datastream_dataframe(iris)
test <- stream$get_points(n = 30)
test <- datastream_dataframe(test)
mymodel <- HoeffdingTree(numericEstimator = "GaussianNumericAttributeClassObserver")
mymodel <- trainMOA(model = mymodel, formula = Species ~ Sepal.Length+Sepal.Width+Petal.Length+Petal.Width, data = stream)
在我的例子中,预测函数没有被屏蔽(如果没有导出,这很奇怪):
pred1 <- predict(mymodel, iris, type = "response")
但如果是的话我可以使用:
pred2 <- RMOA:::predict.MOA_trainedmodel(mymodel, iris, type = "response")
结果是一样的:
all.equal(pred1, pred2)
#output
TRUE
我检查了 RMOA 的命名空间并预测函数已导出,但由于某种原因
RMOA::predict.MOA_trainedmodel
结果
Error: 'predict.MOA_trainedmodel' is not an exported object from 'namespace:RMOA'
同时
RMOA:::predict.MOA_trainedmodel
没有
关于带保留评估的 RMOA Hoeffding 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48870181/