philsupertramp/game-math
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
KNN Class Reference

#include <KNN.h>

Inheritance diagram for KNN:
Classifier Predictor

Public Member Functions

 KNN (int neighbors)
 
void fit (const Matrix< double > &X, const Matrix< double > &y) override
 
Matrix< double > predict (const Matrix< double > &x) override
 
- Public Member Functions inherited from Classifier
 Classifier ()
 
void initialize_weights (size_t numRows, size_t numColumns=1)
 
void update_weights (const Matrix< double > &update, const Matrix< double > &delta)
 
Matrix< double > transform (const Matrix< double > &in) override
 
virtual void fit (const Matrix< double > &X, const Matrix< double > &y)=0
 
virtual Matrix< double > predict (const Matrix< double > &)=0
 
virtual Matrix< double > transform (const Matrix< double > &)=0
 

Private Attributes

int nearest_neighbors
 
Matrix< double > trainLabels
 

Additional Inherited Members

- Public Attributes inherited from Classifier
Matrix< double > weights
 Vector holding weights. More...
 
Matrix< double > costs
 Vector holding classification error per epoch. More...
 
- Protected Attributes inherited from Classifier
bool w_initialized = false
 flag to initialize weights only once More...
 

Detailed Description

K Nearest Neightbor Classifier (KNN) for linear and non-linear classification.

Works well on

Implementation:

fit model: not required

prediction:

  1. Compute distance between test data X and training data weights: $||weights - X_i||_2$
  2. Sort distances, in ascending order, select K shortest distances: $\text{argsort} ||weights - X_i||_2$
  3. For each class in trainLabels count the number of members inside the distance set: $\gamma_{y_k} \leftarrow \gamma_{y_k} + 1$
  4. Select the class with most members inside distance set: $\text{argmax}_k(\gamma)
Examples
ds/TestKNN.cpp.

Constructor & Destructor Documentation

◆ KNN()

KNN::KNN ( int  neighbors)
inline

Member Function Documentation

◆ fit()

void KNN::fit ( const Matrix< double > &  X,
const Matrix< double > &  y 
)
inlineoverridevirtual

Helper method to 'fit' the model.

Stores given X in weights and y in trainLabels

Parameters
XMatrix of known input data
yvector of corresponding labels

Implements Predictor.

◆ predict()

Matrix< double > KNN::predict ( const Matrix< double > &  x)
inlineoverridevirtual

K-Nearest Neighbor Classifier Prediction

  1. Compute distance between test data X and training data weights: $||weights - X_i||_2$
  2. Sort distances, in ascending order, select K shortest distances: $\text{argsort} ||weights - X_i||_2$
  3. For each class in trainLabels count the number of members inside the distance set: $\gamma_{y_k} \leftarrow \gamma_{y_k} + 1$
  4. Select the class with most members inside distance set: $\text{argmax}_k(\gamma)

Implements Predictor.

Member Data Documentation

◆ nearest_neighbors

int KNN::nearest_neighbors
private

◆ trainLabels

Matrix<double> KNN::trainLabels
private

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