
Exhaustive grid search with K-fold CV (Geron Ch 2, morie.fn grgs)
Source:R/geron_train_native.R
morie_geron_grid_search_cv.RdCartesian product of param_grid (first name varies slowest, matching
itertools.product) crossed with morie_geron_kfold_cv() splits.
best_index is 0-based; std_scores uses ddof = 1.
Value
List with best_params, best_score, best_index,
best_std, mean_scores, std_scores, all_scores, candidates,
n_fits.
Examples
if (requireNamespace("ranger", quietly = TRUE)) {
set.seed(5)
X <- matrix(rnorm(60), 30, 2)
y <- as.numeric(X[, 1] > 0)
fs <- function(Xtr, ytr, Xte, yte, params) {
b <- qr.solve(cbind(1, as.matrix(Xtr)), as.numeric(ytr))
pred <- as.numeric(cbind(1, as.matrix(Xte)) %*% b)
-mean((pred - yte)^2) * params$w
}
morie_geron_grid_search_cv(X, y, param_grid = list(w = c(1, 2)),
K = 3, fit_score = fs)
}
#> $best_params
#> $best_params$w
#> [1] 1
#>
#>
#> $best_score
#> [1] -0.08928899
#>
#> $best_index
#> [1] 0
#>
#> $best_std
#> [1] 0.04236076
#>
#> $mean_scores
#> [1] -0.08928899 -0.17857797
#>
#> $std_scores
#> [1] 0.04236076 0.08472153
#>
#> $all_scores
#> [,1] [,2] [,3]
#> [1,] -0.1139315 -0.1135600 -0.04037546
#> [2,] -0.2278631 -0.2271199 -0.08075092
#>
#> $candidates
#> $candidates[[1]]
#> $candidates[[1]]$w
#> [1] 1
#>
#>
#> $candidates[[2]]
#> $candidates[[2]]$w
#> [1] 2
#>
#>
#>
#> $n_fits
#> [1] 6
#>
#> $estimate
#> $estimate$w
#> [1] 1
#>
#>
#> $n
#> [1] 30
#>