RapidLib  v2.2.0
A simple library for interactive machine learning
knnClassification< T > Class Template Referencefinal

#include <knnClassification.h>

Inheritance diagram for knnClassification< T >:
Inheritance graph
Collaboration diagram for knnClassification< T >:
Collaboration graph

Public Member Functions

 knnClassification (const int &num_inputs, const std::vector< size_t > &which_inputs, const std::vector< trainingExampleTemplate< T > > &trainingSet, const int k)
 
 ~knnClassification ()
 
void addNeighbour (const int &classNum, const std::vector< T > &features)
 
run (const std::vector< T > &inputVector) override
 
void train (const std::vector< trainingExampleTemplate< T > > &trainingSet) override
 
void train (const std::vector< trainingExampleTemplate< T > > &trainingSet, const std::size_t whichOutput) override
 
void reset () override
 
size_t getNumInputs () const override
 
std::vector< size_t > getWhichInputs () const override
 
int getK () const
 
void setK (int newK)
 
void getJSONDescription (Json::Value &currentModel) override
 
- Public Member Functions inherited from baseModel< T >
virtual ~baseModel ()
 

Additional Inherited Members

- Protected Member Functions inherited from baseModel< T >
template<typename TT , class Dummy = int>
Json::Value vector2json (TT vec)
 
template<class Dummy = int>
Json::Value vector2json (std::vector< unsigned long > vec)
 

Detailed Description

template<typename T>
class knnClassification< T >

Class for implementing a knn classifier

Constructor & Destructor Documentation

◆ knnClassification()

template<typename T >
knnClassification< T >::knnClassification ( const int &  num_inputs,
const std::vector< size_t > &  which_inputs,
const std::vector< trainingExampleTemplate< T > > &  trainingSet,
const int  k 
)

Constructor that takes training examples in

Parameters
intNumber of inputs expected in the training and input vectors
vectorof input numbers to be fed into the classifer.
vectorof training examples
inthow many near neighbours to evaluate

◆ ~knnClassification()

template<typename T >
knnClassification< T >::~knnClassification

Member Function Documentation

◆ addNeighbour()

template<typename T >
void knnClassification< T >::addNeighbour ( const int &  classNum,
const std::vector< T > &  features 
)

add another example to the existing training set

Parameters
classnumber of example
featurevector of example

◆ getJSONDescription()

template<typename T >
void knnClassification< T >::getJSONDescription ( Json::Value &  currentModel)
overridevirtual

Populate a JSON value with a description of the current model

Parameters
AJSON value to be populated

Implements baseModel< T >.

◆ getK()

template<typename T >
int knnClassification< T >::getK

Get the number of nearest neighbours used by the kNN algorithm.

◆ getNumInputs()

template<typename T >
size_t knnClassification< T >::getNumInputs
overridevirtual

Find out how many inputs the model expects

Returns
Integer number of intpus

Implements baseModel< T >.

◆ getWhichInputs()

template<typename T >
std::vector< size_t > knnClassification< T >::getWhichInputs
overridevirtual

Find out which inputs in a vector will be used

Returns
Vector of ints, specifying input indices.

Implements baseModel< T >.

◆ reset()

template<typename T >
void knnClassification< T >::reset
overridevirtual

Reset the model to its empty state.

Implements baseModel< T >.

◆ run()

template<typename T >
T knnClassification< T >::run ( const std::vector< T > &  inputVector)
overridevirtual

Generate an output value from a single input vector.

Parameters
Astandard vector of type T to be evaluated.
Returns
A single value of type T: the nearest class as determined by k-nearest neighbor.

Implements baseModel< T >.

◆ setK()

template<typename T >
void knnClassification< T >::setK ( int  newK)

Change the number of nearest neighbours used by the kNN algorithm.

Parameters
newvalue for k

◆ train() [1/2]

template<typename T >
void knnClassification< T >::train ( const std::vector< trainingExampleTemplate< T > > &  trainingSet)
overridevirtual

Fill the model with a vector of examples.

Parameters
Thetraining set is a vector of training examples that contain both a vector of input values and a value specifying desired output class.

Implements baseModel< T >.

◆ train() [2/2]

template<typename T >
void knnClassification< T >::train ( const std::vector< trainingExampleTemplate< T > > &  trainingSet,
const std::size_t  whichOutput 
)
overridevirtual

Fill the model with a vector of examples. Use this when the model is part of a modelSet.

Parameters
Thetraining set is a vector of training examples that contain both a vector of input values and a value specifying desired output class.

Implements baseModel< T >.


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