【发布时间】:2016-06-10 20:18:35
【问题描述】:
我正忙于比较 R 中的不同机器学习技术。情况就是这样:我制作了几个函数,它们能够以自动方式创建每个不同的预测模型(例如:逻辑回归、随机森林、神经网络、混合集成等)、预测、混淆矩阵、几种统计数据(例如 AUC 和 Fscore)以及不同的图。
我设法创建了一个能够存储所需数据的 S3 对象。 但是,当我尝试创建已定义对象的列表时,这会失败,并且所有数据都按顺序存储在 1 个大列表中。
这是我的 S3 对象(因为这是我第一次创建 S3,我不确定代码是否 100% 正确):
modelObject <- function(modelName , modelObject, modelPredictions , rocCurve , aUC , confusionMatrix )
{
modelObject <- list(
model.name = modelName,
model.object = modelObject,
model.predictions = modelPredictions,
roc.curve = rocCurve,
roc.auc = aUC,
confusion.matrix = confusionMatrix
)
## Set the name for the class
class(modelObject) <- "modelObject"
return(modelObject)
}
在每个机器学习函数的最后,我定义并返回对象: 缩短示例:
NeuralNetworkAnalysis<- function() {
#I removed the unnecessary code, as only the end of the code is relevant
nn.model <- modelObject(modelName = "Neural.Network" , modelObject = NN , modelPredictions = predNN , rocCurve = roc , aUC = auc , confusionMatrix = confu )
return(nn.model)
}
最后,在我的“脚本”函数中,我创建了一个空列表并尝试附加不同的对象
#function header and arguments before this part are irrelevant
# Build predictive model(s)
modelList = list("model" = modelObject)
modelList <- append(modelList , NeuralNetworkAnalysis())
modelList <- append(modelList, RandomForestAnalysis())
mod <<- RandomForestAnalysis() #this is to test what the outcome is when I do not put it in a list
return(modelList) } #end of the function ModelBuilding
models <- ModelBuilding( '01/01/2013' , '01/01/2014' , '02/01/2014' , '02/01/2015' )
现在,当我查看模型列表时,我没有对象列表,我只有一个包含每个算法的所有数据的列表。
类(模型)[1]“列表”
类(mod)[1]“模型对象”
我怎样才能解决这个问题,这样我就可以有一个列表,例如:
list$random.forest$variable.I.want.to.access(最优惠)
或
list[i]$variable.of.random.forest.that.I.want.to.access
提前谢谢!
奥利维尔
【问题讨论】: