lccknn

R package implements an adaptive k-nearest neighbor (k-NN) classifier based on local curvature estimation

https://github.com/gabrielforest/lccknn

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.2%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

R package implements an adaptive k-nearest neighbor (k-NN) classifier based on local curvature estimation

Basic Info
  • Host: GitHub
  • Owner: Gabrielforest
  • License: other
  • Language: R
  • Default Branch: master
  • Homepage:
  • Size: 30.3 KB
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created 11 months ago · Last pushed 11 months ago
Metadata Files
Readme

README.md

kKNN: Adaptive k-Nearest Neighbor Classifier

Project Overview

The LCCkNN R package implements an adaptive k-nearest neighbor (k-NN) classifier based on local curvature estimation. Unlike the traditional k-NN algorithm, which uses a fixed number of neighbors ($k$) for all data points, the kK-NN algorithm dynamically adjusts the neighborhood size for each sample.

The core idea is that data points with low curvature could have larger neighborhoods, as the tangent space approximates well the underlying data shape. Conversely, points with high curvature could have smaller neighborhoods, because the tangent space is a loose approximation. This adaptive strategy is capable of avoiding both underfitting and overfitting, and improves classification performance, especially when dealing with a limited number of training samples. The algorithm's key components include:

  • Curvature Estimation: The local Gaussian curvature is estimated by approximating the local shape operator in terms of the local covariance matrix and the local Hessian matrix. This approximation for the local shape operator of the data manifold improves the robustness to noise and outliers.
  • Adaptive Neighborhood Sizing: The curvatures are quantized into ten different scores. Based on these scores, the adaptive neighborhood adjustment is performed by pruning the edges of the k-NN graph, reducing the neighborhood size for high-curvature points and retaining a larger neighborhood for low-curvature points.
  • Improved Performance: Results on many real-world datasets indicate that the kK-NN algorithm yields superior balanced accuracy compared to the established k-NN method and another adaptive k-NN algorithm. The results consistently show that the kK-NN classifier is superior to the regular k-NN and also the competing adaptive k-NN algorithm.

Key Feature: Dual Quantization Support

This package offers two distinct quantization methods for adaptive neighborhood sizing:

Paper Method: The default approach, which quantizes curvatures into ten different scores (from 0 to 9), as described in the original paper.

Log2n Method: A slightly modified approach, also developed by the same author, that quantizes curvatures into k scores, where k is the number of neighbors, which is calculated as k= log2n.

This dual-method support allows users to test and compare both quantization strategies within a single package.

📦 Installation

You can install the LCCkNN package directly from CRAN or GitHub using devtools.

r install.packages("LCCkNN") devtools::install_github("gabrielforest/LCCkNN")

Usage Example

Here is a quick example of how to use the kKNN function on the built-in iris dataset.

```r

Load necessary libraries

library(caret)

Load and prepare data (e.g., the Iris dataset)

datairis <- iris data <- as.matrix(datairis[, 1:4]) target <- as.integer(data_iris$Species)

Standardize the data

data <- scale(data)

Split data into training and testing sets

set.seed(42) trainindex <- caret::createDataPartition(target, p = 0.5, list = FALSE) traindata <- data[trainindex, ] testdata <- data[-trainindex, ] trainlabels <- target[train_index]

Determine initial k value as log2(n)

initialk <- round(log2(nrow(traindata))) if (initialk %% 2 == 0) { initialk <- initial_k + 1 }

Run the kK-NN classifier using the default quantization method ('paper')

predictionspaper <- LCCkNN::kKNN( train = traindata, test = testdata, traintarget = trainlabels, k = initialk )

Run the kK-NN classifier using the 'log2n' quantization method

predictionslog2n <- LCCkNN::kKNN( train = traindata, test = testdata, traintarget = trainlabels, k = initialk, quantize_method = 'log2n' )

Evaluate the results (e.g., calculate balanced accuracy)

testlabels <- target[-trainindex] balaccpaper <- LCCkNN::balancedaccuracyscore(testlabels, predictionspaper) balacclog2n <- LCCkNN::balancedaccuracyscore(testlabels, predictionslog2n) cat("Balanced Accuracy (paper Method):", balaccpaper, "\n") cat("Balanced Accuracy (log2n Method):", balacclog2n, "\n") ```

Citation

This package is an R implementation of the algorithm proposed in the following research paper. For more details on the methodology, please refer to the original article:

Levada, A. L. M., Nielsen, F., & Haddad, M. F. C. (2024). ADAPTIVE k-NEAREST NEIGHBOR CLASSIFIER BASED ON THE LOCAL ESTIMATION OF THE SHAPE OPERATOR. arXiv preprint arXiv:2409.05084.

Read the full paper here

Owner

  • Name: Gabriel de Freitas Pereira
  • Login: Gabrielforest
  • Kind: user

GitHub Events

Total
  • Watch event: 1
  • Push event: 1
Last Year
  • Watch event: 1
  • Push event: 1

Packages

  • Total packages: 1
  • Total downloads: unknown
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: LCCkNN

Adaptive k-Nearest Neighbor Classifier Based on Local Curvature Estimation

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 0 Last month
Rankings
Dependent packages count: 25.7%
Dependent repos count: 31.5%
Average: 47.5%
Downloads: 85.4%
Last synced: 10 months ago

Dependencies

DESCRIPTION cran
  • FNN * imports
  • MASS * imports
  • MLmetrics * imports
  • caret * imports
  • class * imports
  • dplyr * imports
  • matrixStats * imports
  • psych * imports
  • stats * imports