Skip to content

Commit

Permalink
Made GaussianNaiveBayes a multiclass machine
Browse files Browse the repository at this point in the history
- Added NativeMulticlassMachine for classifiers without submachines
  • Loading branch information
lisitsyn committed May 21, 2012
1 parent d91047e commit 54eef53
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Multiclass.i
Expand Up @@ -16,6 +16,7 @@
%rename(MulticlassOneVsRestStrategy) CMulticlassOneVsRestStrategy;
%rename(MulticlassOneVsOneStrategy) CMulticlassOneVsOneStrategy;
%rename(MulticlassMachine) CMulticlassMachine;
%rename(NativeMulticlassMachine) CNativeMulticlassMachine;
%rename(LinearMulticlassMachine) CLinearMulticlassMachine;
%rename(KernelMulticlassMachine) CKernelMulticlassMachine;
%rename(MulticlassSVM) CMulticlassSVM;
Expand Down Expand Up @@ -53,6 +54,7 @@
%include <shogun/multiclass/MulticlassOneVsRestStrategy.h>
%include <shogun/multiclass/MulticlassOneVsOneStrategy.h>
%include <shogun/machine/MulticlassMachine.h>
%include <shogun/machine/NativeMulticlassMachine.h>
%include <shogun/machine/LinearMulticlassMachine.h>
%include <shogun/machine/KernelMulticlassMachine.h>
%include <shogun/multiclass/MulticlassSVM.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Multiclass_includes.i
Expand Up @@ -4,6 +4,7 @@
#include <shogun/multiclass/MulticlassOneVsRestStrategy.h>
#include <shogun/multiclass/MulticlassOneVsOneStrategy.h>
#include <shogun/machine/MulticlassMachine.h>
#include <shogun/machine/NativeMulticlassMachine.h>
#include <shogun/machine/LinearMulticlassMachine.h>
#include <shogun/machine/KernelMulticlassMachine.h>
#include <shogun/multiclass/MulticlassSVM.h>
Expand Down
76 changes: 76 additions & 0 deletions src/shogun/machine/NativeMulticlassMachine.h
@@ -0,0 +1,76 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2011 Soeren Sonnenburg
* Written (W) 2012 Fernando José Iglesias García and Sergey Lisitsyn
* Copyright (C) 2012 Sergey Lisitsyn, Fernando José Iglesias Garcia
*/

#ifndef _NATIVEMULTICLASSMACHINE_H___
#define _NATIVEMULTICLASSMACHINE_H___

#include <shogun/machine/MulticlassMachine.h>

namespace shogun
{

/** @brief experimental abstract native multiclass machine class */
class CNativeMulticlassMachine : public CMulticlassMachine
{
public:
/** default constructor */
CNativeMulticlassMachine()
{
}

/** destructor */
virtual ~CNativeMulticlassMachine()
{
}

/** get name */
virtual const char* get_name() const
{
return "NativeMulticlassMachine";
}

protected:
/** init strategy */
void init_strategy() { }

/** clear machines */
void clear_machines() { }

/** abstract init machine for training method */
virtual bool init_machine_for_train(CFeatures* data) { return true; }

/** abstract init machines for applying method */
virtual bool init_machines_for_apply(CFeatures* data) { return true; }

/** check whether machine is ready */
virtual bool is_ready() { return true; }

/** obtain machine from trained one */
virtual CMachine* get_machine_from_trained(CMachine* machine) { return NULL; }

/** get num rhs vectors */
virtual int32_t get_num_rhs_vectors() { return 0; }

/** set subset to the features of the machine, deletes old one
*
* @param subset subset indices to set
*/
virtual void add_machine_subset(SGVector<index_t> subset) { }

/** deletes any subset set to the features of the machine */
virtual void remove_machine_subset() { }

/** whether the machine is acceptable in set_machine */
virtual bool is_acceptable_machine(CMachine *machine) { return true; }

};
}
#endif
15 changes: 7 additions & 8 deletions src/shogun/multiclass/GaussianNaiveBayes.cpp
Expand Up @@ -9,7 +9,6 @@
*/

#include <shogun/multiclass/GaussianNaiveBayes.h>
#include <shogun/machine/Machine.h>
#include <shogun/features/Features.h>
#include <shogun/labels/Labels.h>
#include <shogun/labels/RegressionLabels.h>
Expand All @@ -19,15 +18,15 @@

using namespace shogun;

CGaussianNaiveBayes::CGaussianNaiveBayes() : CMachine(), m_features(NULL),
CGaussianNaiveBayes::CGaussianNaiveBayes() : CNativeMulticlassMachine(), m_features(NULL),
m_min_label(0), m_num_classes(0), m_dim(0), m_means(), m_variances(),
m_label_prob(), m_rates()
{

};

CGaussianNaiveBayes::CGaussianNaiveBayes(CFeatures* train_examples,
CLabels* train_labels) : CMachine(), m_features(NULL),
CLabels* train_labels) : CNativeMulticlassMachine(), m_features(NULL),
m_min_label(0), m_num_classes(0), m_dim(0), m_means(),
m_variances(), m_label_prob(), m_rates()
{
Expand Down Expand Up @@ -61,7 +60,7 @@ void CGaussianNaiveBayes::set_features(CFeatures* features)
m_features = (CDotFeatures*)features;
}

bool CGaussianNaiveBayes::train(CFeatures* data)
bool CGaussianNaiveBayes::train_machine(CFeatures* data)
{
// init features with data if necessary and assure type is correct
if (data)
Expand Down Expand Up @@ -173,7 +172,7 @@ bool CGaussianNaiveBayes::train(CFeatures* data)
return true;
}

CLabels* CGaussianNaiveBayes::apply(CFeatures* data)
CMulticlassLabels* CGaussianNaiveBayes::apply_multiclass(CFeatures* data)
{
if (data)
set_features(data);
Expand All @@ -184,20 +183,20 @@ CLabels* CGaussianNaiveBayes::apply(CFeatures* data)
int32_t num_vectors = m_features->get_num_vectors();

// init result labels
CRegressionLabels* result = new CRegressionLabels(num_vectors);
CMulticlassLabels* result = new CMulticlassLabels(num_vectors);

// classify each example of data
SG_PROGRESS(0, 0, num_vectors);
for (int i = 0; i < num_vectors; i++)
{
result->set_label(i,apply(i));
result->set_label(i,apply_one(i));
SG_PROGRESS(i + 1, 0, num_vectors);
}
SG_DONE();
return result;
};

float64_t CGaussianNaiveBayes::apply(int32_t idx)
float64_t CGaussianNaiveBayes::apply_one(int32_t idx)
{
// get [idx] feature vector
SGVector<float64_t> feature_vector = m_features->get_computed_dot_feature_vector(idx);
Expand Down
22 changes: 12 additions & 10 deletions src/shogun/multiclass/GaussianNaiveBayes.h
Expand Up @@ -11,7 +11,7 @@
#ifndef GAUSSIANNAIVEBAYES_H_
#define GAUSSIANNAIVEBAYES_H_

#include <shogun/machine/Machine.h>
#include <shogun/machine/NativeMulticlassMachine.h>
#include <shogun/mathematics/Math.h>
#include <shogun/features/DotFeatures.h>

Expand All @@ -32,7 +32,7 @@ class CFeatures;
* \f]
*
*/
class CGaussianNaiveBayes : public CMachine
class CGaussianNaiveBayes : public CNativeMulticlassMachine
{

public:
Expand Down Expand Up @@ -62,23 +62,17 @@ class CGaussianNaiveBayes : public CMachine
*/
virtual CFeatures* get_features();

/** train classifier
* @param data train examples
* @return true if successful
*/
virtual bool train(CFeatures* data=NULL);

/** classify specified examples
* @param data examples to be classified
* @return labels corresponding to data
*/
virtual CLabels* apply(CFeatures* data=NULL);
virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL);

/** classifiy specified example
* @param idx example index
* @return label
*/
virtual float64_t apply(int32_t idx);
virtual float64_t apply_one(int32_t idx);

/** get name
* @return classifier name
Expand All @@ -92,6 +86,14 @@ class CGaussianNaiveBayes : public CMachine

protected:

/** train classifier
* @param data train examples
* @return true if successful
*/
virtual bool train_machine(CFeatures* data=NULL);

protected:

/// features for training or classifying
CDotFeatures* m_features;

Expand Down

0 comments on commit 54eef53

Please sign in to comment.