Skip to content

Commit

Permalink
Fixed GNB zero variance handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jun 1, 2012
1 parent f100cae commit e915d01
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/shogun/multiclass/GaussianNaiveBayes.cpp
Expand Up @@ -215,10 +215,12 @@ float64_t CGaussianNaiveBayes::apply_one(int32_t idx)
}
else
m_rates.vector[i] = CMath::log(m_label_prob.vector[i]);

// product all conditional gaussian probabilities
for (k=0; k<m_dim; k++)
m_rates.vector[i]+= CMath::log(normal_exp(feature_vector.vector[k],i,k)/CMath::sqrt(m_variances(k, i)));
if (m_variances(k,i)!=0.0)
m_rates.vector[i]+= CMath::log(0.39894228/CMath::sqrt(m_variances(k, i))) -
0.5*CMath::sq(feature_vector.vector[k]-m_means(k, i))/(m_variances(k, i));
}

// find label with maximum rate
Expand Down
11 changes: 0 additions & 11 deletions src/shogun/multiclass/GaussianNaiveBayes.h
Expand Up @@ -117,17 +117,6 @@ class CGaussianNaiveBayes : public CNativeMulticlassMachine
/// a priori probabilities of labels
SGVector<float64_t> m_label_prob;

/** computes gaussian exponent by x, indexes, m_means and m_variances
* @param x feature value
* @param l_idx index of label
* @param f_idx index of feature
* @return exponent value
*/
float64_t inline normal_exp(float64_t x, int32_t l_idx, int32_t f_idx)
{
return CMath::exp(-CMath::sq(x-m_means(f_idx, l_idx))/(2*m_variances(f_idx, l_idx)));
}

/// label rates
SGVector<float64_t> m_rates;
};
Expand Down

0 comments on commit e915d01

Please sign in to comment.