【问题标题】:R rfe feature selection caretR rfe 特征选择插入符号
【发布时间】:2016-04-26 11:13:08
【问题描述】:

我正在使用 R 和 caret 包进行分类任务。对于特征消除,我使用的是 rfe,它有不同的选项,其中,我想要最大化/最小化的指标是什么。

问题是 rfe 接受诸如 RMSE、kappa 之类的指标,我想使用不同的指标来最大化,以防万一我想从 Metrics 库中最大化 ScoreQuadraticWeightedKappa,但我不知道该怎么做.

我有以下代码:

control <- rfeControl(functions = rfFuncs, method="cv", number=2)
results <- rfe(dataset[, -59], dataset[, 59], 
               sizes = c(1:58), rfeControl = control)

如何编辑它,让 rfe 最大化 ScoreQuadraticWeightedKappa?

【问题讨论】:

    标签: r machine-learning classification r-caret feature-selection


    【解决方案1】:

    您需要修改postResample 函数,或者创建自己的类似函数,然后将其插入rfFuncs$summary。默认postResample函数如下:

    > postResample
    function (pred, obs) 
    {
        isNA <- is.na(pred)
        pred <- pred[!isNA]
        obs <- obs[!isNA]
        if (!is.factor(obs) & is.numeric(obs)) {
            if (length(obs) + length(pred) == 0) {
                out <- rep(NA, 2)
            }
            else {
                if (length(unique(pred)) < 2 || length(unique(obs)) < 
                    2) {
                    resamplCor <- NA
                }
                else {
                    resamplCor <- try(cor(pred, obs, use = "pairwise.complete.obs"), 
                      silent = TRUE)
                    if (class(resamplCor) == "try-error") 
                      resamplCor <- NA
                }
                mse <- mean((pred - obs)^2)
                n <- length(obs)
                out <- c(sqrt(mse), resamplCor^2)
            }
            names(out) <- c("RMSE", "Rsquared")
        }
        else {
            if (length(obs) + length(pred) == 0) {
                out <- rep(NA, 2)
            }
            else {
                pred <- factor(pred, levels = levels(obs))
                requireNamespaceQuietStop("e1071")
                out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag", 
                    "kappa")]
            }
            names(out) <- c("Accuracy", "Kappa")
        }
        if (any(is.nan(out))) 
            out[is.nan(out)] <- NA
        out
    }
    

    更具体地说,由于您正在进行分类,因此您需要修改 postResample 的部分内容:

        else {
            if (length(obs) + length(pred) == 0) {
                out <- rep(NA, 2)
            }
            else {
                pred <- factor(pred, levels = levels(obs))
                requireNamespaceQuietStop("e1071")
                out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag", 
                                        "kappa")]
            }
            names(out) <- c("Accuracy", "Kappa")
        }
    

    编辑postResample,或创建自己的等效函数后,您可以运行:

    rfFuncs$summary <- function (data, lev = NULL, model = NULL) {
        if (is.character(data$obs)) 
            data$obs <- factor(data$obs, levels = lev)
        postResample(data[, "pred"], data[, "obs"])
    }
    

    只需确保 postResample 已被编辑或将其替换为等效函数的名称即可。

    【讨论】:

      猜你喜欢
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 2016-01-02
      • 2023-03-04
      相关资源
      最近更新 更多