|
CMP++: Uncertainty Quantification & Bayesian Calibration
|
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. | |
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.
| void cmp::classifier::KNN::compute | ( | const std::vector< Eigen::VectorXd > & | points, |
| size_t | k | ||
| ) |
Computes the nearest neighbor index map for the given points.
| points | Vector of data points. |
| k | Number of nearest neighbors to compute. |
| size_t cmp::classifier::KNN::k | ( | ) | const |
Returns the value of k (number of neighbors).
| size_t cmp::classifier::KNN::nPoints | ( | ) | const |
Returns the total number of points in the dataset.
| 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.
| i | Index of the query point. |
|
private |
The number of nearest neighbors (k).
|
private |
Cache containing nearest neighbor indices for each data point.
|
private |
Total number of data points.