gpt4 book ai didi

r - 使插入符号的遗传特征选择更快

转载 作者:行者123 更新时间:2023-12-05 04:02:39 25 4
gpt4 key购买 nike

我正在尝试通过插入符号遗传算法使用特征选择来优化 xgboost 树

results <- gafs(iris[,1:4], iris[,5],
iters = 2,
method = "xgbTree",
metric = "Accuracy",
gafsControl = gafsControl(functions=caretGA, method="cv", repeats=2, verbose = TRUE),
trConrol = trainControl(method = "cv", classProbs = TRUE, verboseIter = TRUE)
)

但是这非常慢,即使我只是使用 iters = 2 而不是 iters = 200 也更合适。我可以做些什么来加快速度?

最佳答案

这是一个使用 doParallel 包并行化 gafs() 函数并修改其他一些参数以使其更快的示例。在可能的情况下,我包括运行时间。

原始代码是使用交叉验证(method = "cv")而不是重复交叉验证(method = "repeatedcv"),所以我相信repeats = 2 参数被忽略。我没有在并行示例中包含该选项。

首先,使用没有任何修改或并行化的原始代码:

> library(caret)
> data(iris)

> set.seed(1)
> st.01 <- system.time(results.01 <- gafs(iris[,1:4], iris[,5],
iters = 2,
method = "xgbTree",
metric = "Accuracy",
gafsControl = gafsControl(functions = caretGA,
method = "cv",
repeats = 2,
verbose = TRUE),
trConrol = trainControl(method = "cv",
classProbs = TRUE,
verboseIter = TRUE)))

Fold01 1 0.9596575 (1)
Fold01 2 0.9596575->0.9667641 (1->1, 100.0%) *
Fold02 1 0.9598146 (1)
Fold02 2 0.9598146->0.9641482 (1->1, 100.0%) *
Fold03 1 0.9502661 (1)

我在一夜之间(8 到 10 小时)运行了上面的代码,但由于完成时间太长而停止运行。粗略估计运行时间至少为 24 小时。

其次,包括减少 popSize 参数(从 50 到 20)、allowParallelgenParallel 选项到 gafsControl() 并最终在 gafsControl()trControl() 中减少了折叠的数量(从 10 到 5) >:

> library(doParallel)
> cl <- makePSOCKcluster(detectCores() - 1)
> registerDoParallel(cl)

> set.seed(1)
> st.09 <- system.time(results.09 <- gafs(iris[,1:4], iris[,5],
iters = 2,
popSize = 20,
method = "xgbTree",
metric = "Accuracy",
gafsControl = gafsControl(functions = caretGA,
method = "cv",
number = 5,
verbose = TRUE,
allowParallel = TRUE,
genParallel = TRUE),
trConrol = trainControl(method = "cv",
number = 5,
classProbs = TRUE,
verboseIter = TRUE)))

final GA
1 0.9508099 (4)
2 0.9508099->0.9561501 (4->1, 25.0%) *
final model
> st.09
user system elapsed
3.536 0.173 4152.988

我的系统有 4 个内核,但按照规定它只使用了 3 个,我验证它正在运行 3 个 R 进程。

gafsControl() 文档对allowParallelgenParallel 的描述如下:

  • allowParallel:如果并行后端已加载且可用,函数应该使用它吗?

  • genParallel:如果并行后端已加载且可用,应该 'gafs' 使用它并行化内部的适应度计算 重采样中的一代?

插入符号文档表明 allowParallel 选项将比 genParallel 选项提供更大的运行时间改进: https://topepo.github.io/caret/feature-selection-using-genetic-algorithms.html

与原始代码相比,我预计并行化代码的结果至少会略有不同。以下是并行化代码的结果:

> results.09

Genetic Algorithm Feature Selection

150 samples
4 predictors
3 classes: 'setosa', 'versicolor', 'virginica'

Maximum generations: 2
Population per generation: 20
Crossover probability: 0.8
Mutation probability: 0.1
Elitism: 0

Internal performance values: Accuracy, Kappa
Subset selection driven to maximize internal Accuracy

External performance values: Accuracy, Kappa
Best iteration chose by maximizing external Accuracy
External resampling method: Cross-Validated (5 fold)

During resampling:
* the top 4 selected variables (out of a possible 4):
Petal.Width (80%), Petal.Length (40%), Sepal.Length (20%), Sepal.Width (20%)
* on average, 1.6 variables were selected (min = 1, max = 4)

In the final search using the entire training set:
* 4 features selected at iteration 1 including:
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
* external performance at this iteration is

Accuracy Kappa
0.9467 0.9200

关于r - 使插入符号的遗传特征选择更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54255794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com