shark::NearestNeighborClassifier< InputType > Class Template Reference

Nearest Neighbor Classifier. More...

#include <shark/Models/NearestNeighborClassifier.h>

+ Inheritance diagram for shark::NearestNeighborClassifier< InputType >:

Public Types

enum  DistanceWeights { UNIFORM, ONE_OVER_DISTANCE }
 Type of distance-based weights. More...
 
typedef AbstractNearestNeighbors< InputType, unsigned int > NearestNeighbors
 
typedef base_type::BatchInputType BatchInputType
 
typedef base_type::BatchOutputType BatchOutputType
 
- Public Types inherited from shark::AbstractModel< InputType, unsigned int >
enum  Feature
 
typedef InputType InputType
 Defines the input type of the model. More...
 
typedef unsigned int OutputType
 Defines the output type of the model. More...
 
typedef Batch< InputType >::type BatchInputType
 defines the batch type of the input type. More...
 
typedef Batch< OutputType >::type BatchOutputType
 defines the batch type of the output type More...
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 

Public Member Functions

 NearestNeighborClassifier (NearestNeighbors const *algorithm, std::size_t neighbors=3)
 Constructor. More...
 
std::string name () const
 From INameable: return the class name. More...
 
std::size_t neighbors () const
 return the number of neighbors More...
 
void setNeighbors (std::size_t neighbors)
 set the number of neighbors More...
 
DistanceWeights getDistanceWeightType () const
 query the way distances enter as weights More...
 
void setDistanceWeightType (DistanceWeights dw)
 set the way distances enter as weights More...
 
virtual RealVector parameterVector () const
 get internal parameters of the model More...
 
virtual void setParameterVector (RealVector const &newParameters)
 set internal parameters of the model More...
 
virtual std::size_t numberOfParameters () const
 return the size of the parameter vector More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &output, State &state) const
 soft k-nearest-neighbor prediction More...
 
void read (InArchive &archive)
 from ISerializable, reads a model from an archive More...
 
void write (OutArchive &archive) const
 from ISerializable, writes a model to an archive More...
 
- Public Member Functions inherited from shark::AbstractModel< InputType, unsigned int >
 AbstractModel ()
 
virtual ~AbstractModel ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 Returns true when the first parameter derivative is implemented. More...
 
bool hasSecondParameterDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented. More...
 
bool hasSecondInputDerivative () const
 Returns true when the second parameter derivative is implemented. More...
 
bool isSequential () const
 
virtual void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
virtual void eval (InputType const &pattern, OutputType &output) const
 Standard interface for evaluating the response of the model to a single pattern. More...
 
Data< OutputTypeoperator() (Data< InputType > const &patterns) const
 Model evaluation as an operator for a whole dataset. This is a convenience function. More...
 
OutputType operator() (InputType const &pattern) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
BatchOutputType operator() (BatchInputType const &patterns) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, RealVector &derivative) const
 calculates the weighted sum of derivatives w.r.t the parameters. More...
 
virtual void weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, Batch< RealMatrix >::type const &errorHessian, State const &state, RealVector &derivative, RealMatrix &hessian) const
 calculates the weighted sum of derivatives w.r.t the parameters More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, typename Batch< RealMatrix >::type const &errorHessian, State const &state, RealMatrix &derivative, Batch< RealMatrix >::type &hessian) const
 calculates the weighted sum of derivatives w.r.t the inputs More...
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, RealVector &parameterDerivative, BatchInputType &inputDerivative) const
 calculates weighted input and parameter derivative at the same time More...
 
- Public Member Functions inherited from shark::IParameterizable
virtual ~IParameterizable ()
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Protected Attributes

NearestNeighbors const * m_algorithm
 
std::size_t m_classes
 number of classes More...
 
std::size_t m_neighbors
 number of neighbors to be taken into account More...
 
DistanceWeights m_distanceWeights
 type of distance-based weights computation More...
 
- Protected Attributes inherited from shark::AbstractModel< InputType, unsigned int >
Features m_features
 

Detailed Description

template<class InputType>
class shark::NearestNeighborClassifier< InputType >

Nearest Neighbor Classifier.

The NearestNeighborClassifier predicts a class label according to a local majority decision among its k nearest neighbors. It is not specified how ties are broken.

This model requires the use of one of sharks nearest neighhbor Algorithms.

See also
AbstractNearestNeighbors

Definition at line 56 of file NearestNeighborClassifier.h.

Member Typedef Documentation

◆ BatchInputType

Definition at line 61 of file NearestNeighborClassifier.h.

◆ BatchOutputType

◆ NearestNeighbors

Definition at line 59 of file NearestNeighborClassifier.h.

Member Enumeration Documentation

◆ DistanceWeights

template<class InputType>
enum shark::NearestNeighborClassifier::DistanceWeights

Type of distance-based weights.

Enumerator
UNIFORM 

uniform (= no) distance-based weights

ONE_OVER_DISTANCE 

weight each neighbor's label with 1/distance

Definition at line 65 of file NearestNeighborClassifier.h.

Constructor & Destructor Documentation

◆ NearestNeighborClassifier()

template<class InputType>
shark::NearestNeighborClassifier< InputType >::NearestNeighborClassifier ( NearestNeighbors const *  algorithm,
std::size_t  neighbors = 3 
)
inline

Constructor.

Parameters
algorithmthe used algorithm for nearst neighbor search
neighborsnumber of neighbors

Definition at line 75 of file NearestNeighborClassifier.h.

Member Function Documentation

◆ createState()

template<class InputType>
boost::shared_ptr<State> shark::NearestNeighborClassifier< InputType >::createState ( ) const
inlinevirtual

Creates an internal state of the model.

The state is needed when the derivatives are to be calculated. Eval can store a state which is then reused to speed up the calculations of the derivatives. This also allows eval to be evaluated in parallel!

Reimplemented from shark::AbstractModel< InputType, unsigned int >.

Definition at line 123 of file NearestNeighborClassifier.h.

References shark::AbstractModel< InputType, unsigned int >::eval().

◆ eval()

◆ getDistanceWeightType()

template<class InputType>
DistanceWeights shark::NearestNeighborClassifier< InputType >::getDistanceWeightType ( ) const
inline

query the way distances enter as weights

Definition at line 98 of file NearestNeighborClassifier.h.

References shark::NearestNeighborClassifier< InputType >::m_distanceWeights.

◆ name()

template<class InputType>
std::string shark::NearestNeighborClassifier< InputType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 83 of file NearestNeighborClassifier.h.

Referenced by main().

◆ neighbors()

template<class InputType>
std::size_t shark::NearestNeighborClassifier< InputType >::neighbors ( ) const
inline

◆ numberOfParameters()

template<class InputType>
virtual std::size_t shark::NearestNeighborClassifier< InputType >::numberOfParameters ( ) const
inlinevirtual

return the size of the parameter vector

Reimplemented from shark::IParameterizable.

Definition at line 119 of file NearestNeighborClassifier.h.

◆ parameterVector()

template<class InputType>
virtual RealVector shark::NearestNeighborClassifier< InputType >::parameterVector ( ) const
inlinevirtual

get internal parameters of the model

Reimplemented from shark::IParameterizable.

Definition at line 106 of file NearestNeighborClassifier.h.

References shark::NearestNeighborClassifier< InputType >::m_neighbors.

◆ read()

template<class InputType>
void shark::NearestNeighborClassifier< InputType >::read ( InArchive archive)
inlinevirtual

◆ setDistanceWeightType()

template<class InputType>
void shark::NearestNeighborClassifier< InputType >::setDistanceWeightType ( DistanceWeights  dw)
inline

set the way distances enter as weights

Definition at line 102 of file NearestNeighborClassifier.h.

References shark::NearestNeighborClassifier< InputType >::m_distanceWeights.

◆ setNeighbors()

template<class InputType>
void shark::NearestNeighborClassifier< InputType >::setNeighbors ( std::size_t  neighbors)
inline

◆ setParameterVector()

template<class InputType>
virtual void shark::NearestNeighborClassifier< InputType >::setParameterVector ( RealVector const &  newParameters)
inlinevirtual

set internal parameters of the model

Reimplemented from shark::IParameterizable.

Definition at line 113 of file NearestNeighborClassifier.h.

References shark::NearestNeighborClassifier< InputType >::m_neighbors, and SHARK_RUNTIME_CHECK.

◆ write()

template<class InputType>
void shark::NearestNeighborClassifier< InputType >::write ( OutArchive archive) const
inlinevirtual

Member Data Documentation

◆ m_algorithm

template<class InputType>
NearestNeighbors const* shark::NearestNeighborClassifier< InputType >::m_algorithm
protected

◆ m_classes

◆ m_distanceWeights

◆ m_neighbors


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