R中的L2升压
我正在用纯R编写一个L2增强的玩具实现(第13页,共页),只是出于教学的原因 由于代码有问题,我编写了这个特别的案例,效果很好:R中的L2升压,r,lazy-evaluation,R,Lazy Evaluation,我正在用纯R编写一个L2增强的玩具实现(第13页,共页),只是出于教学的原因 由于代码有问题,我编写了这个特别的案例,效果很好: library(MASS) set.seed(1234) idx <- sample(1:nrow(Boston), size = round(0.5*nrow(Boston)), replace = FALSE) training <- Boston[idx,] test <- Boston[-idx,] library(tree) lam
library(MASS)
set.seed(1234)
idx <- sample(1:nrow(Boston), size = round(0.5*nrow(Boston)), replace = FALSE)
training <- Boston[idx,]
test <- Boston[-idx,]
library(tree)
lambda <- 0.01
B <- 3
psi <- vector("list", B)
g <- vector("list", B)
res <- vector("list", B)
X <- subset(training, select = -medv)
psi_0 <- function(x) rep(mean(training$medv), nrow(training))
medv <- training$medv - psi_0(X)
res[[1]] <- cbind(X, medv)
g[[1]] <- function(x) predict(tree(medv ~ ., res[[1]]), x)
psi[[1]] <- function(x) psi_0(x) + lambda * g[[1]](x)
medv <- training$medv - psi[[1]](X)
res[[2]] <- cbind(X, medv)
g[[2]] <- function(x) predict(tree(medv ~ ., res[[2]]), x)
psi[[2]] <- function(x) psi[[1]](x) + lambda * g[[2]](x)
medv <- training$medv - psi[[2]](X)
res[[3]] <- cbind(X, medv)
g[[3]] <- function(x) predict(tree(medv ~ ., res[[3]]), x)
psi[[3]] <- function(x) psi[[2]](x) + lambda * g[[3]](x)
psi[[3]](test)
库(MASS)
种子集(1234)
idx惰性评估。您可能需要在某处使用force(b)
,例如psi[[b]]谢谢您,本。我还是不能让它工作。我会补充更多细节。对不起,@Gregor!固定的。谢谢。懒惰的评价。您可能需要在某处使用force(b)
,例如psi[[b]]谢谢您,本。我还是不能让它工作。我会补充更多细节。对不起,@Gregor!固定的。谢谢
library(MASS)
set.seed(1234)
idx <- sample(1:nrow(Boston), size = round(0.5*nrow(Boston)), replace = FALSE)
training <- Boston[idx,]
test <- Boston[-idx,]
library(tree)
lambda <- 0.01
B <- 3
psi <- vector("list", B)
g <- vector("list", B)
res <- vector("list", B)
X <- subset(training, select = -medv)
psi_0 <- function(x) rep(mean(training$medv), nrow(training))
for (b in 1:B) {
if (b == 1) {
medv <- training$medv - psi_0(X)
res[[1]] <- cbind(X, medv)
g[[1]] <- function(x) predict(tree(medv ~ ., res[[1]]), x)
psi[[1]] <- function(x) psi_0(x) + lambda * g[[1]](x)
} else {
medv <- training$medv - psi[[b-1]](X)
res[[b]] <- cbind(X, medv)
g[[b]] <- function(x) predict(tree(medv ~ ., res[[b]]), x)
psi[[b]] <- function(x) psi[[b-1]](x) + lambda * g[[b]](x)
}
}
psi[[B]](test)