问题描述
我有一些非常低效的代码,希望有人可以帮助我。我没有很好的 reprex,但已经创建了我正在使用的当前代码/工作流程的示例。
这就是我想要做的简洁
- 将数据集过滤成组
- 将过滤后的数据集传递给 3 个独立的函数(特征选择、验证和应用)
组(过滤后)从特征选择到验证再到应用都是一致的。
工作流程是:
- 特征选择采用过滤后的数据并返回每组特征的特征向量
- 验证采用 2 个参数:按组过滤的数据,以及从 1(特征选择)中得出的与该组对应的字符向量结果。为每个组返回
df
并仅选择列prediction
和linear_weight
。然后行绑定 - Application 采用与 2(Validation) 相同的 2 个参数。为每个组返回
df
并根据每个组(从 1)、prediction
和linear_weight
中存在的特征选择列。然后行绑定
我毫不怀疑 purrr
的某些版本可能会使我的代码更加高效并显着改善运行时间。我的一个想法是将选择特征的结果保存到 df
中,特征驻留在列中,然后将该列结果传递给 validate_data
和 applicate_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
解决方法
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]],linear_weight))
applicate_data=function(feat_list) {
d=feat_list[[1]]
ret=mutate(d,linear_weight=runif(nrow(d)))
return (ret)
}
appl_list=purrr::map(features_list,applicate_data)
Reduce(function(x,appl_list[2:3],init=select(appl_list[[1]],linear_weight))