| cuda_ml_knn | R Documentation |
Build a k-nearest-neighbor model for classification or regression tasks.
cuda_ml_knn(x, ...)
## Default S3 method:
cuda_ml_knn(x, ...)
## S3 method for class 'data.frame'
cuda_ml_knn(
x,
y,
algo = c("brute", "ivfflat", "ivfpq"),
metric = c("euclidean", "l2", "l1", "cityblock", "taxicab", "manhattan", "braycurtis",
"canberra", "minkowski", "lp", "chebyshev", "linf", "jensenshannon", "cosine",
"correlation"),
p = 2,
neighbors = 5L,
...
)
## S3 method for class 'matrix'
cuda_ml_knn(
x,
y,
algo = c("brute", "ivfflat", "ivfpq"),
metric = c("euclidean", "l2", "l1", "cityblock", "taxicab", "manhattan", "braycurtis",
"canberra", "minkowski", "lp", "chebyshev", "linf", "jensenshannon", "cosine",
"correlation"),
p = 2,
neighbors = 5L,
...
)
## S3 method for class 'formula'
cuda_ml_knn(
formula,
data,
algo = c("brute", "ivfflat", "ivfpq"),
metric = c("euclidean", "l2", "l1", "cityblock", "taxicab", "manhattan", "braycurtis",
"canberra", "minkowski", "lp", "chebyshev", "linf", "jensenshannon", "cosine",
"correlation"),
p = 2,
neighbors = 5L,
...
)
## S3 method for class 'recipe'
cuda_ml_knn(
x,
data,
algo = c("brute", "ivfflat", "ivfpq"),
metric = c("euclidean", "l2", "l1", "cityblock", "taxicab", "manhattan", "braycurtis",
"canberra", "minkowski", "lp", "chebyshev", "linf", "jensenshannon", "cosine",
"correlation"),
p = 2,
neighbors = 5L,
...
)
x |
Depending on the context:
|
... |
Optional arguments; currently unused. |
y |
A numeric vector (for regression) or factor (for classification) of desired responses. |
algo |
The query algorithm to use. For most workflows, pass one of
{"brute", "ivfflat", "ivfpq"} or a KNN algorithm specification
constructed using the Descriptions of supported algorithms:
Default: "brute". |
metric |
Distance metric to use. Must be one of {"euclidean", "l2", "l1", "cityblock", "taxicab", "manhattan", "braycurtis", "canberra", "minkowski", "lp", "chebyshev", "linf", "jensenshannon", "cosine", "correlation"}. The approximate algorithms support only "euclidean", "l2", "cosine", and "correlation". Default: "euclidean". |
p |
Parameter for the Minkowski metric. If p = 1, then the metric is equivalent to manhattan distance (l1). If p = 2, the metric is equivalent to euclidean distance (l2). |
neighbors |
Number of nearest neighbors to query. Default: 5L. |
formula |
A formula specifying the outcome terms on the left-hand side, and the predictor terms on the right-hand side. |
data |
When a recipe or formula is used, |
A KNN model that can be used with the 'predict' S3 generic to make predictions on new data points. The model object contains the following:
"knn_index": a GPU pointer to the KNN index.
"algo": enum value of the algorithm being used for the KNN query.
"metric": enum value of the distance metric used in KNN computations.
"p": parameter for the Minkowski metric.
"n_samples": number of input data points.
"n_dims": dimension of each input data point.
library(cuda.ml)
if (interactive() && cuda_ml_backend_info()$runtime_installed) {
library(MASS)
library(purrr)
set.seed(0)
centers <- list(c(3, 3), c(-3, -3), c(-3, 3))
gen_pts <- function(cluster_sz) {
pts <- centers |>
map(\(center) mvrnorm(cluster_sz, mu = center, Sigma = diag(2)))
do.call(rbind, pts)
}
gen_labels <- function(cluster_sz) {
seq_along(centers) |>
sapply(\(x) rep(x, cluster_sz)) |>
factor()
}
sample_cluster_sz <- 1000
sample_pts <- cbind(
gen_pts(sample_cluster_sz) |> as.data.frame(),
label = gen_labels(sample_cluster_sz)
)
model <- cuda_ml_knn(
label ~ ., sample_pts, algo = "ivfflat", metric = "euclidean"
)
test_cluster_sz <- 10
test_pts <- gen_pts(test_cluster_sz) |> as.data.frame()
predictions <- predict(model, test_pts)
print(predictions, n = 30)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.