我正在尝试复制 Caruana 等人从模型库 (pdf) 中选择 Ensemble的方法。该方法的核心是一种用于将模型添加到集成中的贪心算法(模型可以多次添加)。我已经为这个贪婪优化算法编写了一个实现,但它非常慢:
library(compiler)
set.seed(42)
X <- matrix(runif(100000*10), ncol=10)
Y <- rnorm(100000)
greedOpt <- cmpfun(function(X, Y, iter=100){
weights <- rep(0, ncol(X))
while(sum(weights) < iter) {
errors <- sapply(1:ncol(X), function(y){
newweights <- weights
newweights[y] <- newweights[y] + 1
pred <- X %*% (newweights)/sum(newweights)
error <- Y - pred
sqrt(mean(error^2))
})
update <- which.min(errors)
weights[update] <- weights[update]+1
}
return(weights/sum(weights))
})
system.time(a <- greedOpt(X,Y))
我知道 R 不能很好地循环,但我想不出任何方法可以在没有循环的情况下进行这种类型的逐步搜索。
有什么改进这个功能的建议吗?