Naive Bayes classifier. More...
#include <shark/Models/NBClassifier.h>
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< AbstractDistribution > | AbstractDistPtr |
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< Feature > | Features |
typedef TypedFeatureNotAvailableException< Feature > | FeatureNotAvailableException |
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... | |
AbstractDistribution & | getFeatureDist (std::size_t classIndex, std::size_t featureIndex) const |
DistSizeType | getDistSize () const |
boost::shared_ptr< State > | createState () 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 ¶m) |
This model does not have any parameters. More... | |
Public Member Functions inherited from shark::AbstractModel< InputType, OutputType > | |
AbstractModel () | |
virtual | ~AbstractModel () |
const Features & | features () 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< OutputType > | operator() (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 ¶meterDerivative, 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 |
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.
typedef boost::shared_ptr<AbstractDistribution> shark::NBClassifier< InputType, OutputType >::AbstractDistPtr |
Definition at line 72 of file NBClassifier.h.
typedef base_type::BatchInputType shark::NBClassifier< InputType, OutputType >::BatchInputType |
Definition at line 66 of file NBClassifier.h.
typedef base_type::BatchOutputType shark::NBClassifier< InputType, OutputType >::BatchOutputType |
Definition at line 67 of file NBClassifier.h.
typedef std::vector<double> shark::NBClassifier< InputType, OutputType >::ClassPriorsType |
Type of class distribution.
Definition at line 70 of file NBClassifier.h.
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.
typedef std::vector<std::vector<AbstractDistPtr> > shark::NBClassifier< InputType, OutputType >::FeatureDistributionsType |
Type of features distribution.
Definition at line 75 of file NBClassifier.h.
|
inline |
Ctor Will build hypothesis that all features in each class follows Normal distribution
classSize | size of class |
featureSize | size of feature |
Definition at line 84 of file NBClassifier.h.
References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.
|
inlineexplicit |
Ctor The distributions for each feature in each class are given by featureDists
featureDists | the distribution of features |
Definition at line 100 of file NBClassifier.h.
References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.
|
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.
|
inlinevirtual |
Implements shark::AbstractModel< InputType, OutputType >.
Definition at line 139 of file NBClassifier.h.
References shark::NBClassifier< InputType, OutputType >::m_classPriors, shark::NBClassifier< InputType, OutputType >::m_featureDistributions, remora::max(), shark::safeLog(), SHARK_ASSERT, and SIZE_CHECK.
|
inline |
Get the size of distribution in format of (class size, feature size)
Definition at line 126 of file NBClassifier.h.
References shark::AbstractModel< InputType, OutputType >::eval(), shark::NBClassifier< InputType, OutputType >::m_featureDistributions, and SIZE_CHECK.
|
inline |
Get a feature distribution for feature featureIndex given class classIndex
classIndex | index of class |
featureIndex | index of feature |
Definition at line 114 of file NBClassifier.h.
References shark::NBClassifier< InputType, OutputType >::m_featureDistributions, SHARK_ASSERT, and SIZE_CHECK.
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 107 of file NBClassifier.h.
|
inlinevirtual |
This model does not have any parameters.
Reimplemented from shark::IParameterizable.
Definition at line 185 of file NBClassifier.h.
|
inline |
Set prior distribution of class to be probability
classToAdd | the class of which probability will be updated |
probability | the probability of the class |
OutputType | the type of output class |
Definition at line 176 of file NBClassifier.h.
References shark::NBClassifier< InputType, OutputType >::m_classPriors, and SHARKEXCEPTION.
|
inlinevirtual |
This model does not have any parameters.
Reimplemented from shark::IParameterizable.
Definition at line 190 of file NBClassifier.h.
References SHARK_ASSERT.
|
protected |
Definition at line 199 of file NBClassifier.h.
Referenced by shark::NBClassifier< InputType, OutputType >::eval(), and shark::NBClassifier< InputType, OutputType >::setClassPrior().
|
protected |
Feature and class distributions
Definition at line 198 of file NBClassifier.h.
Referenced by shark::NBClassifier< InputType, OutputType >::eval(), shark::NBClassifier< InputType, OutputType >::getDistSize(), shark::NBClassifier< InputType, OutputType >::getFeatureDist(), and shark::NBClassifier< InputType, OutputType >::NBClassifier().