Skip to contents

k-nearest-neighbour (instance-based) prediction (Geron Ch 1, morie.fn hmins)

Usage

morie_geron_instance_based(
  X_train,
  y_train,
  x_query,
  k = 1,
  task = "auto",
  weights = "uniform"
)

Arguments

X_train, y_train

Stored instances/targets.

x_query

Query point(s).

k

Neighbours.

task

auto/classification/regression.

weights

uniform/distance.

Value

List with prediction, neighbors, distances, task, feature_ranges, estimate, n, method.

Examples

X <- matrix(c(1, 2, 3, 4), ncol = 1)
y <- c(3, 6, 9, 12)
vshape <- function(L, Xt, yt, Xv, yv) abs(L - 3) + 0.5
morie_geron_hidden_layers_heuristic(vshape, X, y, max_layers = 10,
    patience = 2)
#> $best_n_layers
#> [1] 3
#> 
#> $best_error
#> [1] 0.5
#> 
#> $errors
#> $errors$`1`
#> [1] 2.5
#> 
#> $errors$`2`
#> [1] 1.5
#> 
#> $errors$`3`
#> [1] 0.5
#> 
#> $errors$`4`
#> [1] 1.5
#> 
#> $errors$`5`
#> [1] 2.5
#> 
#> 
#> $depths_tried
#> [1] 5
#> 
#> $stopped_early
#> [1] TRUE
#> 
#> $estimate
#> [1] 0.5
#> 
#> $n
#> [1] 4
#> 
#> $method
#> [1] "Depth selection by validation-error plateau"
#> 
morie_geron_instance_based(X_train = X, y_train = X, x_query = X)
#> $prediction
#> [1] 1 2 3 4
#> 
#> $neighbors
#>      [,1]
#> [1,]    1
#> [2,]    2
#> [3,]    3
#> [4,]    4
#> 
#> $distances
#>      [,1]
#> [1,]    0
#> [2,]    0
#> [3,]    0
#> [4,]    0
#> 
#> $task
#> [1] "regression"
#> 
#> $feature_ranges
#> [1] 3
#> 
#> $estimate
#> [1] 1
#> 
#> $n
#> [1] 4
#> 
#> $method
#> [1] "k-nearest-neighbour (instance-based) prediction"
#>