我正在比较R中的不同机器学习技术。情况就是这样:我做了几个函数,这些函数可以自动创建每个不同的预测模型(例如:逻辑回归,随机森林,神经网络,混合集成)。等),预测,混淆矩阵,一些统计信息(例如AUC和Fscore)以及不同的图。
现在,我想在R中创建一个S4(或S3?)对象的列表,其中每个对象都包含模型,预测,曲线图,混淆矩阵,auc和fscore。
想法是,每个函数都会创建此类对象,然后将其附加到return语句中的对象列表中。
我该如何编程此类课程?以及如何定义每个模型可以具有不同的类型(我假设我创建的所有模型都是S3对象,那么如何在我的S4类中定义它呢?
最终结果应该能够执行以下操作:例如,modelList [i] @plot应该召唤所请求的图。和names(modelList [i])应该给出所使用模型的名称(如果不可能的话,请使用modelList [i] @name)。同样,应该有可能基于诸如AUC之类的参数从列表中选择最佳模型。我没有创建此类对象的经验,所以这是我目前的代码/想法:
modelObject <- setClass(
# Set the name for the class
"modelObject",
# Define the slots
slots = c(
modelName = "character"
model = #should contain a glm, neural network, random forest , etc model
predictions = #should contain a matrix or dataframe of custid and prediction
rocCurve = #when summoned, the ROC curve should be plotted
plotX = #when summoned, plot X should be plotted
AUC = "numeric" #contains the value of the AUC
confusionMatrix = "matrix" #prints the confusion matrix in the console
statX = "numeric"#contains statistic X about the confusion matrix e.g. Fscore
),
# Set the default values for the slots. (optional)
prototype=list(
# I guess i can assign NULL to each variable of the S4 object
),
# Make a function that can test to see if the data is consistent.
# This is not called if you have an initialize function defined!
validity=function(object)
{
#not really an idea how to handle this
}
return(TRUE)
}
)
我要做的是,针对modelObject
班级中您想要的每个插槽,确定期望值的范围。例如,您的model
插槽必须支持模型训练功能(例如lm(),glm(),nnet()等)可以返回的所有可能的对象类别。在示例情况下,您看到返回以下对象:
```
x <- y <- 1:10
class(lm(x~y))
class(glm(x~y))
class(nnet(x~y, size=10))
```
由于返回的对象之间没有通用的类,因此使用S3可能更有意义,因为S3的语法不太严格,并且允许您将各种输出类分配给同一字段名称。考虑到R的无数OO系统有许多不同的方法,您的问题实际上很难回答。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句