【问题标题】:Using Purrr or Furrr to filter, and pass character vectors into additional functions使用 Purrr 或 Furrr 进行过滤,并将字符向量传递给附加函数
【发布时间】:2021-06-16 04:09:13
【问题描述】:

我有一些非常低效的代码,希望有人能帮助我。我没有很好的代表,但创建了我正在使用的当前代码/工作流的示例。

这就是我想要简洁地做的事情

  • 将数据集过滤成组
  • 将过滤后的数据集传递到 3 个独立的函数(功能选择、验证和应用)

从特征选择到验证再到应用,组(过滤)是一致的。
工作流程是:

  1. 特征选择采用过滤后的数据并返回每个组的特征特征向量
  2. 验证采用 2 个参数:按组过滤的数据,以及与来自 1(特征选择)的该组对应的字符向量结果。为每个组返回df 并仅选择predictionlinear_weight 列。然后行绑定
  3. 应用程序采用与 2(验证)相同的 2 个参数。为每个组返回 df,并根据每个组中存在的特征(从 1 开始)选择列,predictionlinear_weight。然后行绑定

我毫不怀疑purrr 的某些版本可能会使我的代码更加高效并显着改善运行时间。我的一个想法是将选择特征的结果保存到 df 中,特征位于列中,然后将该列结果传递给 validate_dataapplicate_data 函数。

抱歉,没有完全可重现的东西。希望这个例子能很好地说明我想要实现的目标。

library(gapminder)

data <- gapminder_unfiltered

# Filter data
group_1_data <- gapminder_unfiltered %>% 
  filter(country %in% c("Algeria", "Benin"))

group_2_data <- gapminder_unfiltered %>% 
  filter(country == "United States")

group_3_data <- gapminder_unfiltered %>% 
  filter(country %in% c("Italy", "France"))


# Feature Selection
group_1_features <- select_features(group_1_data)
group_2_features <- select_features(group_2_data)
group_3_features <- select_features(group_3_data)

# Example of group_1_features output
c("pop", "gdpPercap")

# Validation
group_1_validation <- validate_data(group_1_data, group_1_features)
group_2_validation <- validate_data(group_2_data, group_2_features)
group_3_validation <- validate_data(group_3_data, group_3_features)

# Row bind Validations selecting only created columns of "prediction" & "linear_weight"
all_validations

# Application: Same Inputs as Validation
group_1_application <- applicate_data(group_1_data, group_1_features)
group_2_application <- applicate_data(group_2_data, group_2_features)
group_3_application <- applicate_data(group_3_data, group_3_features)

# Row bind applications.  Select columns/features that exist in every group based on the feature selection.  Also select columns "prediction" & "linear_weight"
total_applications

【问题讨论】:

    标签: r dplyr purrr furrr


    【解决方案1】:

    purrr::map 可以使用,因为一个列表中存储了三个数据帧,然后您可以将结果归约以将它们全部绑定在一起。

    library(dplyr)
    
    groups_data=list(group_1_data, group_2_data, group_3_data)
    
    select_features=function(d) {
      features=c()
      if (sample(c(0,1), 1)==0) {
        features=c("pop", "gdpPercap")
      } else {
        features=c("pop", "gdpPercap", "lifeExp")
      }
      return(list(d,
                  features))
    }
    
    features_list=purrr::map(groups_data, select_features)
    
    validate_data=function(feat_list) {
      d=feat_list[[1]]
      ret=mutate(d, prediction=rnorm(nrow(d)), linear_weight=runif(nrow(d)))
      return (ret)
    }
    
    val_list=purrr::map(features_list, validate_data)
    
    Reduce(function(x, y) {
      return(rbind(x, select(y, prediction, linear_weight))) 
    }, val_list[2:3], init=select(val_list[[1]], prediction, linear_weight))
    
    applicate_data=function(feat_list) {
      d=feat_list[[1]]
      ret=mutate(d, prediction=rnorm(nrow(d)), linear_weight=runif(nrow(d)))
      return (ret)
    }
    
    appl_list=purrr::map(features_list, applicate_data)
    
    Reduce(function(x, y) {
      return(rbind(x, select(y, prediction, linear_weight))) 
    }, appl_list[2:3], init=select(appl_list[[1]], prediction, linear_weight))
    

    【讨论】:

    • 我创建了一个选择功能的函数。我试图弄清楚如何将特征结果按组传递给其他 2 个函数,并根据我概述的列返回 df。
    • 使用 purrr::map?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    相关资源
    最近更新 更多