Skip to content

Commit

Permalink
Fixes gaussian naive bayes classifier memory handling, related to #351
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jan 9, 2012
1 parent 1f52747 commit 681147a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
22 changes: 19 additions & 3 deletions src/shogun/classifier/GaussianNaiveBayes.cpp
Expand Up @@ -47,6 +47,22 @@ CGaussianNaiveBayes::~CGaussianNaiveBayes()
m_label_prob.destroy_vector();
};

CFeatures* CGaussianNaiveBayes::get_features()
{
SG_REF(m_features);
return m_features;
}

void CGaussianNaiveBayes::set_features(CFeatures* features)
{
if (!features->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n");

SG_UNREF(m_features);
SG_REF(features);
m_features = (CDotFeatures*)features;
}

bool CGaussianNaiveBayes::train(CFeatures* data)
{
// init features with data if necessary and assure type is correct
Expand Down Expand Up @@ -153,6 +169,7 @@ bool CGaussianNaiveBayes::train(CFeatures* data)
m_label_prob.vector[i]/= m_num_classes;
}

feature_matrix.free_matrix();
train_labels.free_vector();

return true;
Expand All @@ -178,11 +195,9 @@ CLabels* CGaussianNaiveBayes::apply(CFeatures* data)
// check data correctness
if (!data)
SG_ERROR("No features specified\n");
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n");

// set features to classify
set_features((CDotFeatures*)data);
set_features(data);

// classify using features
return apply();
Expand Down Expand Up @@ -221,6 +236,7 @@ float64_t CGaussianNaiveBayes::apply(int32_t idx)
if (m_rates.vector[i]>m_rates.vector[max_label_idx])
max_label_idx = i;
}
feature_vector.free_vector();

return max_label_idx+m_min_label;
};
13 changes: 2 additions & 11 deletions src/shogun/classifier/GaussianNaiveBayes.h
Expand Up @@ -55,21 +55,12 @@ class CGaussianNaiveBayes : public CMachine
/** set features for classify
* @param features features to be set
*/
virtual inline void set_features(CDotFeatures* features)
{
SG_UNREF(m_features);
SG_REF(features);
m_features = features;
}
virtual void set_features(CFeatures* features);

/** get features for classify
* @return current features
*/
virtual inline CDotFeatures* get_features()
{
SG_REF(m_features);
return m_features;
}
virtual CFeatures* get_features();

/** train classifier
* @param data train examples
Expand Down

0 comments on commit 681147a

Please sign in to comment.