【问题标题】:Get predictions on test sets in MLR在 MLR 中获取测试集的预测
【发布时间】:2018-11-09 18:06:49
【问题描述】:

我正在使用 R 中的 MLR 包为二进制问题拟合分类模型。对于每个模型,我使用“selectFeatures”函数对嵌入式特征选择执行交叉验证,并检索测试集上的平均 AUC。接下来我想检索每个折叠的测试集的预测,但这个函数似乎不支持。我已经尝试将选定的预测变量插入“重采样”函数来获取它。它有效,但性能指标不同,不适合我的分析。如果可能的话,我还尝试检查插入符号包,但乍一看我还没有看到解决方案。知道怎么做吗?

这是我的合成数据代码和我尝试使用“重采样”功能的代码(再次说明:不适合当前版本,因为性能指标不同)。

# 1. Find a synthetic dataset for supervised learning (two classes)
###################################################################

install.packages("mlbench")
library(mlbench)
data(BreastCancer)

# generate 1000 rows, 21 quantitative candidate predictors and 1 target variable 
p<-mlbench.waveform(1000) 

# convert list into dataframe
dataset<-as.data.frame(p)

# drop thrid class to get 2 classes
dataset2  = subset(dataset, classes != 3)

# 2. Perform cross validation with embedded feature selection
#############################################################

library(BBmisc)
library(nnet)
library(mlr)

# Choice of algorithm i.e. neural network
mL <- makeLearner("classif.nnet", predict.type = "prob")

# Choice of sampling plan: 10 fold cross validation with stratification of target classes 
mRD = makeResampleDesc("CV", iters = 10,stratify = TRUE)

# Choice of feature selection strategy   
ctrl = makeFeatSelControlSequential(method = "sffs", maxit = NA,alpha = 0.001)

# Choice of feature selection technique (stepwize family) and p-value 
mFSCS = makeFeatSelControlSequential(method = "sffs", maxit = NA,alpha = 0.001)

# Choice of seed 
set.seed(12)

# Choice of data 
mCT <- makeClassifTask(data =dataset2, target = "classes")

# Perform the method
result = selectFeatures(mL,mCT, mRD, control = ctrl, measures = list(mlr::auc,mlr::acc,mlr::brier))

# Retrieve AUC and selected variables
analyzeFeatSelResult(result)
# Result: auc.test.mean=0.9614525 Variables selected: x.10, x.11, x.15, x.17, x.18    

# 3. Retrieve predictions on tests sets (to later perform Delong tests on AUCs derived from multiple sets of candidate variables)
#################################################################################################################################

# create new dataset with selected predictors
keep <- c("x.10","x.11","x.15","x.17","x.18","classes")
dataset3 <- dataset2[ , names(dataset2) %in% keep]

# Perform same tasks with  resample function instead of selectFeatures function to get predictions on tests set
mL <- makeLearner("classif.nnet", predict.type = "prob")   
ctrl = makeFeatSelControlSequential(method = "sffs", maxit = NA,alpha = 0.001)
mRD = makeResampleDesc("CV", iters = 10,stratify = TRUE)
set.seed(12)
mCT <- makeClassifTask(data =dataset3, target = "classes")
r1r = resample(mL, mCT, mRD, measures = list(mlr::auc,mlr::acc,mlr::brier))
# Result: auc.test.mean=0.9673023

【问题讨论】:

    标签: cross-validation feature-selection mlr


    【解决方案1】:

    您的代码中缺少ctrl

    要获得对重采样对象的预测,只需使用 getRRPredictions(r1r)r1r$measures.test.

    【讨论】:

    • 确实缺少 ctrl。我已经添加了它。我的问题不在于从重采样对象中获取预测,我已经做到了(这是我的第一次尝试)。这种尝试的问题是重采样函数给出的 AUC 与 makeClassifTask 的不同。我已经编辑了我的问题以使其更清楚。谢谢!
    • 您可以使用“makeFeatSelWrapper”作为替代。我也得到了不同的结果,就像你一样......
    • makeFeatSelWrapperd 是否完成整个操作,即 CV+特征选择+预测值?
    • 这似乎确实是一个解决方案。然而奇怪的是,我得到的模型最后没有变量用于逻辑回归和神经网络的错误。我将为此打开一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2016-03-12
    • 2018-06-25
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多