GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
KNN Class Reference
Inheritance diagram for KNN:
Classifier MLBase GRTBase Observer< TrainingResult > Observer< TestInstanceResult >

Public Types

enum  DistanceMethods { EUCLIDEAN_DISTANCE =0, COSINE_DISTANCE, MANHATTAN_DISTANCE }
 
- Public Types inherited from Classifier
enum  ClassifierModes { STANDARD_CLASSIFIER_MODE =0, TIMESERIES_CLASSIFIER_MODE }
 
typedef std::map< std::string, Classifier *(*)() > StringClassifierMap
 
- Public Types inherited from MLBase
enum  BaseType {
  BASE_TYPE_NOT_SET =0, CLASSIFIER, REGRESSIFIER, CLUSTERER,
  PRE_PROCSSING, POST_PROCESSING, FEATURE_EXTRACTION, CONTEXT
}
 

Public Member Functions

 KNN (UINT K=10, bool useScaling=false, bool useNullRejection=false, Float nullRejectionCoeff=10.0, bool searchForBestKValue=false, UINT minKSearchValue=1, UINT maxKSearchValue=10)
 
 KNN (const KNN &rhs)
 
virtual ~KNN (void)
 
KNNoperator= (const KNN &rhs)
 
virtual bool deepCopyFrom (const Classifier *classifier)
 
virtual bool train_ (ClassificationData &trainingData)
 
virtual bool predict_ (VectorFloat &inputVector)
 
virtual bool clear ()
 
virtual bool save (std::fstream &file) const
 
virtual bool load (std::fstream &file)
 
virtual bool recomputeNullRejectionThresholds ()
 
UINT getK ()
 
UINT getDistanceMethod ()
 
bool setK (UINT K)
 
bool setMinKSearchValue (UINT minKSearchValue)
 
bool setMaxKSearchValue (UINT maxKSearchValue)
 
bool enableBestKValueSearch (bool searchForBestKValue)
 
bool setNullRejectionCoeff (Float nullRejectionCoeff)
 
bool setDistanceMethod (UINT distanceMethod)
 
- Public Member Functions inherited from Classifier
 Classifier (const std::string &classifierId="")
 
virtual ~Classifier (void)
 
bool copyBaseVariables (const Classifier *classifier)
 
virtual bool reset ()
 
virtual bool computeAccuracy (const ClassificationData &data, Float &accuracy)
 
std::string getClassifierType () const
 
bool getSupportsNullRejection () const
 
bool getNullRejectionEnabled () const
 
Float getNullRejectionCoeff () const
 
Float getMaximumLikelihood () const
 
Float getBestDistance () const
 
Float getPhase () const
 
Float getTrainingSetAccuracy () const
 
virtual UINT getNumClasses () const
 
UINT getClassLabelIndexValue (const UINT classLabel) const
 
UINT getPredictedClassLabel () const
 
VectorFloat getClassLikelihoods () const
 
VectorFloat getClassDistances () const
 
VectorFloat getNullRejectionThresholds () const
 
Vector< UINT > getClassLabels () const
 
Vector< MinMaxgetRanges () const
 
bool enableNullRejection (const bool useNullRejection)
 
virtual bool setNullRejectionThresholds (const VectorFloat &newRejectionThresholds)
 
bool getTimeseriesCompatible () const
 
Classifiercreate () const
 
 GRT_DEPRECATED_MSG ("createNewInstance is deprecated, use create instead.", Classifier *createNewInstance() const )
 
 GRT_DEPRECATED_MSG ("createInstanceFromString is deprecated, use create instead.", static Classifier *createInstanceFromString(const std::string &id))
 
ClassifierdeepCopy () const
 
const ClassifiergetClassifierPointer () const
 
const ClassifiergetBaseClassifier () const
 
- Public Member Functions inherited from MLBase
 MLBase (const std::string &id="", const BaseType type=BASE_TYPE_NOT_SET)
 
virtual ~MLBase (void)
 
bool copyMLBaseVariables (const MLBase *mlBase)
 
virtual bool train (ClassificationData trainingData)
 
virtual bool train (RegressionData trainingData)
 
virtual bool train_ (RegressionData &trainingData)
 
virtual bool train (RegressionData trainingData, RegressionData validationData)
 
virtual bool train_ (RegressionData &trainingData, RegressionData &validationData)
 
virtual bool train (TimeSeriesClassificationData trainingData)
 
virtual bool train_ (TimeSeriesClassificationData &trainingData)
 
virtual bool train (ClassificationDataStream trainingData)
 
virtual bool train_ (ClassificationDataStream &trainingData)
 
virtual bool train (UnlabelledData trainingData)
 
virtual bool train_ (UnlabelledData &trainingData)
 
virtual bool train (MatrixFloat data)
 
virtual bool train_ (MatrixFloat &data)
 
virtual bool predict (VectorFloat inputVector)
 
virtual bool predict (MatrixFloat inputMatrix)
 
virtual bool predict_ (MatrixFloat &inputMatrix)
 
virtual bool map (VectorFloat inputVector)
 
virtual bool map_ (VectorFloat &inputVector)
 
virtual bool print () const
 
virtual bool save (const std::string &filename) const
 
virtual bool load (const std::string &filename)
 
 GRT_DEPRECATED_MSG ("saveModelToFile(std::string filename) is deprecated, use save(const std::string &filename) instead", virtual bool saveModelToFile(const std::string &filename) const )
 
 GRT_DEPRECATED_MSG ("saveModelToFile(std::fstream &file) is deprecated, use save(std::fstream &file) instead", virtual bool saveModelToFile(std::fstream &file) const )
 
 GRT_DEPRECATED_MSG ("loadModelFromFile(std::string filename) is deprecated, use load(const std::string &filename) instead", virtual bool loadModelFromFile(const std::string &filename))
 
 GRT_DEPRECATED_MSG ("loadModelFromFile(std::fstream &file) is deprecated, use load(std::fstream &file) instead", virtual bool loadModelFromFile(std::fstream &file))
 
virtual bool getModel (std::ostream &stream) const
 
virtual std::string getModelAsString () const
 
DataType getInputType () const
 
DataType getOutputType () const
 
BaseType getType () const
 
UINT getNumInputFeatures () const
 
UINT getNumInputDimensions () const
 
UINT getNumOutputDimensions () const
 
UINT getMinNumEpochs () const
 
UINT getMaxNumEpochs () const
 
UINT getBatchSize () const
 
UINT getNumRestarts () const
 
UINT getValidationSetSize () const
 
UINT getNumTrainingIterationsToConverge () const
 
Float getMinChange () const
 
Float getLearningRate () const
 
Float getRMSTrainingError () const
 
 GRT_DEPRECATED_MSG ("getRootMeanSquaredTrainingError() is deprecated, use getRMSTrainingError() instead", Float getRootMeanSquaredTrainingError() const )
 
Float getTotalSquaredTrainingError () const
 
Float getRMSValidationError () const
 
Float getValidationSetAccuracy () const
 
VectorFloat getValidationSetPrecision () const
 
VectorFloat getValidationSetRecall () const
 
bool getUseValidationSet () const
 
bool getRandomiseTrainingOrder () const
 
bool getTrained () const
 
 GRT_DEPRECATED_MSG ("getModelTrained() is deprecated, use getTrained() instead", bool getModelTrained() const )
 
bool getConverged () const
 
bool getScalingEnabled () const
 
bool getIsBaseTypeClassifier () const
 
bool getIsBaseTypeRegressifier () const
 
bool getIsBaseTypeClusterer () const
 
bool getTrainingLoggingEnabled () const
 
bool getTestingLoggingEnabled () const
 
bool enableScaling (const bool useScaling)
 
bool setMaxNumEpochs (const UINT maxNumEpochs)
 
bool setBatchSize (const UINT batchSize)
 
bool setMinNumEpochs (const UINT minNumEpochs)
 
bool setNumRestarts (const UINT numRestarts)
 
bool setMinChange (const Float minChange)
 
bool setLearningRate (const Float learningRate)
 
bool setUseValidationSet (const bool useValidationSet)
 
bool setValidationSetSize (const UINT validationSetSize)
 
bool setRandomiseTrainingOrder (const bool randomiseTrainingOrder)
 
bool setTrainingLoggingEnabled (const bool loggingEnabled)
 
bool setTestingLoggingEnabled (const bool loggingEnabled)
 
bool registerTrainingResultsObserver (Observer< TrainingResult > &observer)
 
bool registerTestResultsObserver (Observer< TestInstanceResult > &observer)
 
bool removeTrainingResultsObserver (const Observer< TrainingResult > &observer)
 
bool removeTestResultsObserver (const Observer< TestInstanceResult > &observer)
 
bool removeAllTrainingObservers ()
 
bool removeAllTestObservers ()
 
bool notifyTrainingResultsObservers (const TrainingResult &data)
 
bool notifyTestResultsObservers (const TestInstanceResult &data)
 
MLBasegetMLBasePointer ()
 
const MLBasegetMLBasePointer () const
 
Vector< TrainingResultgetTrainingResults () const
 
- Public Member Functions inherited from GRTBase
 GRTBase (const std::string &id="")
 
virtual ~GRTBase (void)
 
bool copyGRTBaseVariables (const GRTBase *GRTBase)
 
 GRT_DEPRECATED_MSG ("getClassType is deprecated, use getId() instead!", std::string getClassType() const )
 
std::string getId () const
 
std::string getLastWarningMessage () const
 
std::string getLastErrorMessage () const
 
std::string getLastInfoMessage () const
 
bool setInfoLoggingEnabled (const bool loggingEnabled)
 
bool setWarningLoggingEnabled (const bool loggingEnabled)
 
bool setErrorLoggingEnabled (const bool loggingEnabled)
 
bool setDebugLoggingEnabled (const bool loggingEnabled)
 
GRTBasegetGRTBasePointer ()
 
const GRTBasegetGRTBasePointer () const
 
Float scale (const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
 
Float SQR (const Float &x) const
 
- Public Member Functions inherited from Observer< TrainingResult >
virtual void notify (const TrainingResult &data)
 
- Public Member Functions inherited from Observer< TestInstanceResult >
virtual void notify (const TestInstanceResult &data)
 

Static Public Member Functions

static std::string getId ()
 
- Static Public Member Functions inherited from Classifier
static Classifiercreate (const std::string &id)
 
static Vector< std::string > getRegisteredClassifiers ()
 
- Static Public Member Functions inherited from GRTBase
static std::string getGRTVersion (bool returnRevision=true)
 
static std::string getGRTRevison ()
 

Protected Member Functions

bool train_ (const ClassificationData &trainingData, const UINT K)
 
bool predict (const VectorFloat &inputVector, const UINT K)
 
bool loadLegacyModelFromFile (std::fstream &file)
 
Float computeEuclideanDistance (const VectorFloat &a, const VectorFloat &b)
 
Float computeCosineDistance (const VectorFloat &a, const VectorFloat &b)
 
Float computeManhattanDistance (const VectorFloat &a, const VectorFloat &b)
 
- Protected Member Functions inherited from Classifier
bool saveBaseSettingsToFile (std::fstream &file) const
 
bool loadBaseSettingsFromFile (std::fstream &file)
 
- Protected Member Functions inherited from MLBase
bool saveBaseSettingsToFile (std::fstream &file) const
 
bool loadBaseSettingsFromFile (std::fstream &file)
 

Protected Attributes

UINT K
 
UINT distanceMethod
 

The number of neighbours to search for


 
bool searchForBestKValue
 

The distance method used to compute the distance between each data point


 
UINT minKSearchValue
 

Sets if the best K value should be searched for or if the model should be trained with K


 
UINT maxKSearchValue
 

The minimum K value to start the search from


 
ClassificationData trainingData
 

The maximum K value to end the search at


 
VectorFloat trainingMu
 

Holds the trainingData to perform the predictions


 
VectorFloat trainingSigma
 

Holds the average max-class distance of the training data for each of classes


 
- Protected Attributes inherited from Classifier
bool supportsNullRejection
 
bool useNullRejection
 
UINT numClasses
 
UINT predictedClassLabel
 
UINT classifierMode
 
Float nullRejectionCoeff
 
Float maxLikelihood
 
Float bestDistance
 
Float phase
 
Float trainingSetAccuracy
 
VectorFloat classLikelihoods
 
VectorFloat classDistances
 
VectorFloat nullRejectionThresholds
 
Vector< UINT > classLabels
 
Vector< MinMaxranges
 
- Protected Attributes inherited from MLBase
bool trained
 
bool useScaling
 
bool converged
 
DataType inputType
 
DataType outputType
 
BaseType baseType
 
UINT numInputDimensions
 
UINT numOutputDimensions
 
UINT numTrainingIterationsToConverge
 
UINT minNumEpochs
 
UINT maxNumEpochs
 
UINT batchSize
 
UINT validationSetSize
 
UINT numRestarts
 
Float learningRate
 
Float minChange
 
Float rmsTrainingError
 
Float rmsValidationError
 
Float totalSquaredTrainingError
 
Float validationSetAccuracy
 
bool useValidationSet
 
bool randomiseTrainingOrder
 
VectorFloat validationSetPrecision
 
VectorFloat validationSetRecall
 
Random random
 
Vector< TrainingResulttrainingResults
 
TrainingResultsObserverManager trainingResultsObserverManager
 
TestResultsObserverManager testResultsObserverManager
 
TrainingLog trainingLog
 
TestingLog testingLog
 
- Protected Attributes inherited from GRTBase
std::string classId
 Stores the name of the class (e.g., MinDist)
 
DebugLog debugLog
 
ErrorLog errorLog
 
InfoLog infoLog
 
WarningLog warningLog
 

Additional Inherited Members

- Static Protected Member Functions inherited from Classifier
static StringClassifierMapgetMap ()
 

Detailed Description

Constructor & Destructor Documentation

KNN::KNN ( UINT  K = 10,
bool  useScaling = false,
bool  useNullRejection = false,
Float  nullRejectionCoeff = 10.0,
bool  searchForBestKValue = false,
UINT  minKSearchValue = 1,
UINT  maxKSearchValue = 10 
)

Default Constructor

Parameters
Kthe number of neigbors the algorithm will us to perform a classification. Default value is K = 10
useScalingsets if the training and prediction data should be scaled to a specific range. Default value is useScaling = false
useNullRejectionsets if null rejection will be used for the realtime prediction. If useNullRejection is set to true then the predictedClassLabel will be set to 0 (which is the default null label) if the distance between the inputVector and the top K datum is greater than the null rejection threshold for the top predicted class. The null rejection threshold is computed for each class during the training phase. Default value is useNullRejection = false
nullRejectionCoeffsets the null rejection coefficient, this is a multipler controlling the null rejection threshold for each class. This will only be used if the useNullRejection parameter is set to true. Default value is nullRejectionCoeff = 10.0
searchForBestKValuesets if the training algorithm will search for the best K value. Default value is searchForBestKValue = false
minKSearchValuesets the minimum K value to use when searching for the best K value. Default value is minKSearchValue = 1
maxKSearchValuesets the maximum K value to use when searching for the best K value. Default value is maxKSearchValue = 1
KNN::KNN ( const KNN rhs)

Defines the copy constructor.

Parameters
constKNN &rhs: the instance from which all the data will be copied into this instance

Definition at line 48 of file KNN.cpp.

KNN::~KNN ( void  )
virtual

Default Destructor

Definition at line 54 of file KNN.cpp.

Member Function Documentation

bool KNN::clear ( )
virtual

This overrides the clear function in the Classifier base class. It will completely clear the ML module, removing any trained model and setting all the base variables to their default values.

Returns
returns true if the module was cleared succesfully, false otherwise

Reimplemented from Classifier.

Definition at line 515 of file KNN.cpp.

bool KNN::deepCopyFrom ( const Classifier classifier)
virtual

This is required for the Gesture Recognition Pipeline for when the pipeline.setClassifier method is called. It clones the data from the Base Class Classifier pointer (which should be pointing to a KNN instance) into this instance

Parameters
classifiera pointer to the Classifier Base Class, this should be pointing to another KNN instance
Returns
returns true if the clone was successfull, false otherwise

Reimplemented from Classifier.

Definition at line 76 of file KNN.cpp.

bool KNN::enableBestKValueSearch ( bool  searchForBestKValue)

Sets if the best K value should be searched for. If true then the best K value will be searched during the training phase. If false then the KNN algorithm will be trained with the K value set by the user.

Returns
returns true if the searchForBestKValue was set successfully, false otherwise

Definition at line 752 of file KNN.cpp.

UINT KNN::getDistanceMethod ( )
inline

Returns the current distance method being used to compute the neighest neighbours. See the enum DistanceMethods.

Returns
returns the current distance method being used to compute the neighest neighbours

Definition at line 164 of file KNN.h.

std::string KNN::getId ( )
static

Gets a string that represents the KNN class.

Returns
returns a string containing the ID of this class

Definition at line 28 of file KNN.cpp.

UINT KNN::getK ( )
inline

Gets the K nearest neighbours that will be searched for by the algorithm during prediction

Returns
returns the K nearest neighbours that will be searched for by the algorithm during prediction

Definition at line 156 of file KNN.h.

bool KNN::load ( std::fstream &  file)
virtual

This loads a trained KNN model from a file. This overrides the load function in the Classifier base class.

Parameters
filea reference to the file the KNN model will be loaded from
Returns
returns true if the model was loaded successfully, false otherwise

Reimplemented from MLBase.

Examples:
ClassificationModulesExamples/KNNExample/KNNExample.cpp.

Definition at line 581 of file KNN.cpp.

bool KNN::loadLegacyModelFromFile ( std::fstream &  file)
protected

Read the ranges if needed

Definition at line 810 of file KNN.cpp.

KNN & KNN::operator= ( const KNN rhs)

Defines how the data from the rhs KNN should be copied to this KNN

Parameters
rhsanother instance of a KNN
Returns
returns a pointer to this instance of the KNN

Definition at line 58 of file KNN.cpp.

bool KNN::predict_ ( VectorFloat inputVector)
virtual

This predicts the class of the inputVector. This overrides the predict function in the Classifier base class.

Parameters
inputVectorthe input vector to classify
Returns
returns true if the prediction was performed, false otherwise

Reimplemented from MLBase.

Definition at line 366 of file KNN.cpp.

bool KNN::recomputeNullRejectionThresholds ( )
virtual

This recomputes the null rejection thresholds for each of the classes in the KNN model. This will be called automatically if the setGamma(Float gamma) function is called. The KNN model needs to be trained first before this function can be called.

Returns
returns true if the null rejection thresholds were updated successfully, false otherwise

Reimplemented from Classifier.

Definition at line 715 of file KNN.cpp.

bool KNN::save ( std::fstream &  file) const
virtual

This saves the trained KNN model to a file. This overrides the save function in the Classifier base class.

Parameters
filea reference to the file the KNN model will be saved to
Returns
returns true if the model was saved successfully, false otherwise

Reimplemented from MLBase.

Examples:
ClassificationModulesExamples/KNNExample/KNNExample.cpp.

Definition at line 528 of file KNN.cpp.

bool KNN::setDistanceMethod ( UINT  distanceMethod)

Sets the current distance method being used to compute the neighest neighbours. This should be called prior to training a KNN model. See the enum DistanceMethods for a list of possible distance methods.

Returns
returns true if the distance method was updated successfully, false otherwise

Definition at line 766 of file KNN.cpp.

bool KNN::setK ( UINT  K)

Sets the K nearest neighbours that will be searched for by the algorithm during prediction. This function should be called prior to running the training algorithm.

Returns
returns true if the K was set successfully, false otherwise

Definition at line 734 of file KNN.cpp.

bool KNN::setMaxKSearchValue ( UINT  maxKSearchValue)

Sets the maximum K value to use when searching for the best K value. This value should be greater than the minKSearchValue.

Returns
returns true if the maxKSearchValue was set successfully, false otherwise

Definition at line 747 of file KNN.cpp.

bool KNN::setMinKSearchValue ( UINT  minKSearchValue)

Sets the minimum K value to use when searching for the best K value. This value should be less than the maxKSearchValue.

Returns
returns true if the minimumKValue was set successfully, false otherwise

Definition at line 742 of file KNN.cpp.

bool KNN::setNullRejectionCoeff ( Float  nullRejectionCoeff)
virtual

Sets the nullRejectionCoeff parameter. The nullRejectionCoeff parameter is a multipler controlling the null rejection threshold for each class. This function will also recompute the null rejection thresholds.

Returns
returns true if the nullRejectionCoeff parameter was updated successfully, false otherwise

Reimplemented from Classifier.

Examples:
ClassificationModulesExamples/KNNExample/KNNExample.cpp.

Definition at line 757 of file KNN.cpp.

bool KNN::train_ ( ClassificationData trainingData)
virtual

This trains the KNN model, using the labelled classification data. This overrides the train function in the Classifier base class.

Parameters
trainingDataa reference to the training data
Returns
returns true if the KNN model was trained, false otherwise

Reimplemented from MLBase.

Definition at line 99 of file KNN.cpp.


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