【问题标题】:append fold column to training data frame using splitTools使用 splitTools 将折叠列附加到训练数据框
【发布时间】:2021-04-03 01:03:40
【问题描述】:

我正在使用此代码和包splitTools

library(splitTools)

set.seed(3451)
inds <- partition(iris$Sepal.Length, p = c(train = 0.8, test = 0.2))

train <- iris[inds$train,]
test <- iris[inds$test,]

folds <- create_folds(train$Sepal.Length, k = 5)

返回的对象 folds 是一个整数列表。是否可以将列折叠附加到包含折叠数(在本例中为 1、2、3、4 或 5)的数据框序列?谢谢。

PS:

悲惨的尝试:

results <- NULL
index <- 1
for (fold in folds) {
    t <- train[-fold,]
    t$fold <- index
    index <- index + 1
    results <- rbind(results, t)
}

table(results$fold)
train <- results
head(train)

【问题讨论】:

    标签: r


    【解决方案1】:

    您需要将折叠转换为数据框,然后为行创建索引并根据折叠将值添加为 TRUE/FALSE。代码如下:

    library(dplyr)
    #Bind
    L <- lapply(folds, function(x) data.frame(val=x))
    dffolds <- do.call(rbind,L)
    dffolds$Fold <- gsub('\\..*','',rownames(dffolds))
    rownames(dffolds)<-NULL
    #Reshape
    Folds <- dffolds %>% group_by(Fold) %>%
      mutate(V=T) %>%
      pivot_wider(names_from = Fold,values_from=V,values_fill=F)
    #Merge
    train2 <- train %>%
      mutate(val=row_number()) %>%
      left_join(Folds) %>%
      select(-val)
    

    输出(一些行和列):

        Sepal.Length Sepal.Width Petal.Length Petal.Width    Species Fold1 Fold2 Fold3
    1            4.9         3.0          1.4         0.2     setosa  TRUE FALSE  TRUE
    2            4.7         3.2          1.3         0.2     setosa FALSE  TRUE  TRUE
    3            5.0         3.6          1.4         0.2     setosa  TRUE  TRUE FALSE
    4            5.4         3.9          1.7         0.4     setosa FALSE  TRUE  TRUE
    5            4.6         3.4          1.4         0.3     setosa  TRUE FALSE  TRUE
    6            5.0         3.4          1.5         0.2     setosa  TRUE FALSE  TRUE
    7            4.9         3.1          1.5         0.1     setosa FALSE  TRUE  TRUE
    8            5.4         3.7          1.5         0.2     setosa  TRUE FALSE  TRUE
    9            4.8         3.0          1.4         0.1     setosa  TRUE  TRUE FALSE
    10           4.3         3.0          1.1         0.1     setosa  TRUE FALSE  TRUE
    

    【讨论】:

    • 谢谢 - 我在我原来的问题中添加了一个 PS,这似乎有效,但可能非常笨拙
    • @cs0815 我看到了,也不错,干得好:)
    • 不知道为什么你最终只有 3 折,它也应该是长格式?
    • @cs0815 请省略,我最终得到了 5 折,但由于空间问题我没有包括 :)
    【解决方案2】:

    如果您有兴趣获取每行“非折叠”分区的索引,通过partition 更容易。 create_folds 本身调用partition,所以这样做不会丢失任何逻辑:

    iris$fold <- partition(iris$Sepal.Length, p = rep(0.2, 5), split_into_list = FALSE)
    
    # Gives
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species fold
    1          5.1         3.5          1.4         0.2  setosa    4
    2          4.9         3.0          1.4         0.2  setosa    2
    3          4.7         3.2          1.3         0.2  setosa    4
    4          4.6         3.1          1.5         0.2  setosa    5
    5          5.0         3.6          1.4         0.2  setosa    4
    6          5.4         3.9          1.7         0.4  setosa    3
    > 
    

    免责声明:我是 splitTools 的作者,非常感谢您提供如何改进包的提示 :-)。

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2020-09-14
      • 2019-01-22
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      相关资源
      最近更新 更多