weighted_knn: Weighted k-Nearest Neighbors

View source: R/knn_weights.R

weighted_knnR Documentation

Weighted k-Nearest Neighbors

Description

This function computes a weighted k-nearest neighbors graph or adjacency matrix from a data matrix. The function takes into account the Euclidean distance between instances and applies a kernel function to convert the distances into similarities.

Usage

weighted_knn(
  X,
  k = 5,
  FUN = heat_kernel,
  type = c("normal", "mutual", "asym"),
  as = c("igraph", "sparse"),
  ...
)

Arguments

X

A data matrix where rows are instances and columns are features.

k

An integer specifying the number of nearest neighbors to consider (default: 5).

FUN

A kernel function used to convert Euclidean distances into similarities (default: heat_kernel).

type

A character string indicating the type of k-nearest neighbors graph to compute. One of "normal", "mutual", or "asym" (default: "normal").

as

A character string specifying the format of the output. One of "igraph" or "sparse" (default: "igraph").

...

Additional arguments passed to the nearest neighbor search function (rflann::Neighbour).

Value

If 'as' is "igraph", an igraph object representing the weighted k-nearest neighbors graph. If 'as' is "sparse", a sparse adjacency matrix.

Examples

X <- matrix(rnorm(10 * 10), 10, 10)
w <- weighted_knn(X, k = 5)


bbuchsbaum/graphweights documentation built on April 4, 2024, 7:19 p.m.