CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
cmp::classifier::KNN Class Reference

K-Nearest Neighbors classifier helper class. More...

#include <classifier.h>

Public Member Functions

void compute (const std::vector< Eigen::VectorXd > &points, size_t k)
 Computes the nearest neighbor index map for the given points.
 
const std::vector< size_t > & operator[] (size_t i) const
 Accesses the list of nearest neighbor indices for the i-th point.
 
size_t nPoints () const
 Returns the total number of points in the dataset.
 
size_t k () const
 Returns the value of k (number of neighbors).
 

Private Attributes

std::vector< std::vector< size_t > > neighbours_
 Cache containing nearest neighbor indices for each data point.
 
size_t kNearestValue_
 The number of nearest neighbors (k).
 
size_t nPoints_
 Total number of data points.
 

Detailed Description

K-Nearest Neighbors classifier helper class.

Mathematical Formulation For a query point \(\mathbf{x}\), K-Nearest Neighbors identifies the set of \(k\) closest points \(\mathcal{N}_k(\mathbf{x})\) in the reference dataset \(\mathcal{D} = \{\mathbf{x}_i\}_{i=1}^N\) under a chosen distance metric \(d(\mathbf{x}, \mathbf{x}_i)\) (typically Euclidean distance):

\[ d(\mathbf{x}, \mathbf{x}_i) = \|\mathbf{x} - \mathbf{x}_i\|_2 \]

Implementation Algorithm The compute method builds an indexing/neighbour structure. For each point in the dataset, it searches and stores the indices of its \(k\) nearest neighbors.

Member Function Documentation

◆ compute()

void cmp::classifier::KNN::compute ( const std::vector< Eigen::VectorXd > &  points,
size_t  k 
)

Computes the nearest neighbor index map for the given points.

Parameters
pointsVector of data points.
kNumber of nearest neighbors to compute.

◆ k()

size_t cmp::classifier::KNN::k ( ) const

Returns the value of k (number of neighbors).

Returns
Number of neighbors.

◆ nPoints()

size_t cmp::classifier::KNN::nPoints ( ) const

Returns the total number of points in the dataset.

Returns
Size of the dataset.

◆ operator[]()

const std::vector< size_t > & cmp::classifier::KNN::operator[] ( size_t  i) const

Accesses the list of nearest neighbor indices for the i-th point.

Parameters
iIndex of the query point.
Returns
Reference to a vector of neighbor indices.

Member Data Documentation

◆ kNearestValue_

size_t cmp::classifier::KNN::kNearestValue_
private

The number of nearest neighbors (k).

◆ neighbours_

std::vector<std::vector<size_t> > cmp::classifier::KNN::neighbours_
private

Cache containing nearest neighbor indices for each data point.

◆ nPoints_

size_t cmp::classifier::KNN::nPoints_
private

Total number of data points.


The documentation for this class was generated from the following files: