shark::NBClassifier< InputType, OutputType > Class Template Reference

Naive Bayes classifier. More...

#include <shark/Models/NBClassifier.h>

+ Inheritance diagram for shark::NBClassifier< InputType, OutputType >:

Public Types

typedef base_type::BatchInputType BatchInputType
 
typedef base_type::BatchOutputType BatchOutputType
 
typedef std::vector< double > ClassPriorsType
 Type of class distribution. More...
 
typedef boost::shared_ptr< AbstractDistributionAbstractDistPtr
 
typedef std::vector< std::vector< AbstractDistPtr > > FeatureDistributionsType
 Type of features distribution. More...
 
typedef std::pair< std::size_t, std::size_t > DistSizeType
 Size of distribution in format of (number of classes, number of features) More...
 
- Public Types inherited from shark::AbstractModel< InputType, OutputType >
enum  Feature
 
typedef InputType InputType
 Defines the input type of the model. More...
 
typedef OutputType 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

 NBClassifier (std::size_t classSize, std::size_t featureSize)
 
 NBClassifier (FeatureDistributionsType const &featureDists)
 
std::string name () const
 From INameable: return the class name. More...
 
AbstractDistributiongetFeatureDist (std::size_t classIndex, std::size_t featureIndex) const
 
DistSizeType getDistSize () const
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const
 see AbstractModel::eval More...
 
void setClassPrior (OutputType classToAdd, double probability)
 
RealVector parameterVector () const
 This model does not have any parameters. More...
 
void setParameterVector (const RealVector &param)
 This model does not have any parameters. More...
 
- Public Member Functions inherited from shark::AbstractModel< InputType, OutputType >
 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 read (InArchive &archive)
 From ISerializable, reads a model from an archive. More...
 
virtual void write (OutArchive &archive) const
 writes a model to an archive More...
 
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 ()
 
virtual std::size_t numberOfParameters () const
 Return the number of parameters. More...
 
- 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

FeatureDistributionsType m_featureDistributions
 
ClassPriorsType m_classPriors
 
- Protected Attributes inherited from shark::AbstractModel< InputType, OutputType >
Features m_features
 

Detailed Description

template<class InputType = RealVector, class OutputType = unsigned int>
class shark::NBClassifier< InputType, OutputType >

Naive Bayes classifier.

This model summarizes a Naive Bayes classifier, which assumes that the data X is generated by a mixture of class-conditional (i.e., dependent on the value of the class variable Y) distributions. Furthermore, the Naive Bayes assumption introduces the additional constraint that the attribute values Xi are independent of one another within each of these mixture components.

Definition at line 56 of file NBClassifier.h.

Member Typedef Documentation

◆ AbstractDistPtr

template<class InputType = RealVector, class OutputType = unsigned int>
typedef boost::shared_ptr<AbstractDistribution> shark::NBClassifier< InputType, OutputType >::AbstractDistPtr

Definition at line 72 of file NBClassifier.h.

◆ BatchInputType

template<class InputType = RealVector, class OutputType = unsigned int>
typedef base_type::BatchInputType shark::NBClassifier< InputType, OutputType >::BatchInputType

Definition at line 66 of file NBClassifier.h.

◆ BatchOutputType

template<class InputType = RealVector, class OutputType = unsigned int>
typedef base_type::BatchOutputType shark::NBClassifier< InputType, OutputType >::BatchOutputType

Definition at line 67 of file NBClassifier.h.

◆ ClassPriorsType

template<class InputType = RealVector, class OutputType = unsigned int>
typedef std::vector<double> shark::NBClassifier< InputType, OutputType >::ClassPriorsType

Type of class distribution.

Definition at line 70 of file NBClassifier.h.

◆ DistSizeType

template<class InputType = RealVector, class OutputType = unsigned int>
typedef std::pair<std::size_t, std::size_t> shark::NBClassifier< InputType, OutputType >::DistSizeType

Size of distribution in format of (number of classes, number of features)

Definition at line 78 of file NBClassifier.h.

◆ FeatureDistributionsType

template<class InputType = RealVector, class OutputType = unsigned int>
typedef std::vector<std::vector<AbstractDistPtr> > shark::NBClassifier< InputType, OutputType >::FeatureDistributionsType

Type of features distribution.

Definition at line 75 of file NBClassifier.h.

Constructor & Destructor Documentation

◆ NBClassifier() [1/2]

template<class InputType = RealVector, class OutputType = unsigned int>
shark::NBClassifier< InputType, OutputType >::NBClassifier ( std::size_t  classSize,
std::size_t  featureSize 
)
inline

Ctor Will build hypothesis that all features in each class follows Normal distribution

Parameters
classSizesize of class
featureSizesize of feature

Definition at line 84 of file NBClassifier.h.

References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.

◆ NBClassifier() [2/2]

template<class InputType = RealVector, class OutputType = unsigned int>
shark::NBClassifier< InputType, OutputType >::NBClassifier ( FeatureDistributionsType const &  featureDists)
inlineexplicit

Ctor The distributions for each feature in each class are given by featureDists

Parameters
featureDiststhe distribution of features

Definition at line 100 of file NBClassifier.h.

References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.

Member Function Documentation

◆ createState()

template<class InputType = RealVector, class OutputType = unsigned int>
boost::shared_ptr<State> shark::NBClassifier< InputType, OutputType >::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, OutputType >.

Definition at line 134 of file NBClassifier.h.

◆ eval()

template<class InputType = RealVector, class OutputType = unsigned int>
void shark::NBClassifier< InputType, OutputType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
inlinevirtual

◆ getDistSize()

template<class InputType = RealVector, class OutputType = unsigned int>
DistSizeType shark::NBClassifier< InputType, OutputType >::getDistSize ( ) const
inline

Get the size of distribution in format of (class size, feature size)

Returns
the size of distribution

Definition at line 126 of file NBClassifier.h.

References shark::AbstractModel< InputType, OutputType >::eval(), shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.

◆ getFeatureDist()

template<class InputType = RealVector, class OutputType = unsigned int>
AbstractDistribution& shark::NBClassifier< InputType, OutputType >::getFeatureDist ( std::size_t  classIndex,
std::size_t  featureIndex 
) const
inline

Get a feature distribution for feature featureIndex given class classIndex

Parameters
classIndexindex of class
featureIndexindex of feature
Returns
the distribution for feature featureIndex given class classIndex

Definition at line 114 of file NBClassifier.h.

References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, SHARK_ASSERT, and SIZE_CHECK.

◆ name()

template<class InputType = RealVector, class OutputType = unsigned int>
std::string shark::NBClassifier< InputType, OutputType >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 107 of file NBClassifier.h.

◆ parameterVector()

template<class InputType = RealVector, class OutputType = unsigned int>
RealVector shark::NBClassifier< InputType, OutputType >::parameterVector ( ) const
inlinevirtual

This model does not have any parameters.

Reimplemented from shark::IParameterizable.

Definition at line 185 of file NBClassifier.h.

◆ setClassPrior()

template<class InputType = RealVector, class OutputType = unsigned int>
void shark::NBClassifier< InputType, OutputType >::setClassPrior ( OutputType  classToAdd,
double  probability 
)
inline

Set prior distribution of class to be probability

Parameters
classToAddthe class of which probability will be updated
probabilitythe probability of the class
Template Parameters
OutputTypethe type of output class

Definition at line 176 of file NBClassifier.h.

References shark::NBClassifier< InputType, OutputType >::m_classPriors, and SHARKEXCEPTION.

◆ setParameterVector()

template<class InputType = RealVector, class OutputType = unsigned int>
void shark::NBClassifier< InputType, OutputType >::setParameterVector ( const RealVector &  param)
inlinevirtual

This model does not have any parameters.

Reimplemented from shark::IParameterizable.

Definition at line 190 of file NBClassifier.h.

References SHARK_ASSERT.

Member Data Documentation

◆ m_classPriors

template<class InputType = RealVector, class OutputType = unsigned int>
ClassPriorsType shark::NBClassifier< InputType, OutputType >::m_classPriors
protected

◆ m_featureDistributions


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