【发布时间】:2020-02-25 10:55:52
【问题描述】:
有什么方法可以通过微调训练数据的超参数来创建多个随机森林模型,并针对所有模型检查测试数据的性能并将其存储在 csv 文件中?
例如:- 我有一个模型,mtry 是 6,nodesize 是 3,另一个模型 mtry 是 10,nodesize 是 4 我需要做的是测试这两个模型测试数据的性能并存储关键模型指标,如混淆矩阵、敏感性和特异性。
我试过下面的代码
train_performance <- data.frame('TN'=0,'FP'=0,'FN'=0,'TP'=0,'accuracy'=0,'kappa'=0,'sensitivity'=0,'specificity'=0)
modellist <- list()
for (mtry in c(6,11)){
for (nodesize in c(2,3)){
fit_model <- randomForest(dv~., train_final,mtry = mtry, importance=TRUE, nodesize=nodesize,
sampsize = ceiling(.8*nrow(train_final)), proximity=TRUE,na.action = na.omit,
ntree=500)
Key_col <- paste0(mtry,"-",nodesize)
modellist[[Key_col]] <- fit_model
pred_train <- predict(fit_model, train_final)
cf <- confusionMatrix(pred_train, train_final$DV, mode = 'everything', positive = '1')
train_performance$TN <- cf$table[1]
train_performance$FP <- cf$table[2]
train_performance$FN <- cf$table[3]
train_performance$TP <- cf$table[4]
train_performance$accuracy=cf$overall[1]
train_performance$kappa=cf$overall[2]
train_performance$sensitivity=cf$byClass[1]
train_performance$specificity=cf$byClass[2]
train_performance$key=Key_col
}
}
【问题讨论】:
-
我只能获得 mtry 为 11 且节点大小为 3 的模型的最终结果。但并非所有模型都能存储结果。请帮助我。
标签: r machine-learning random-forest