# Revision history [back]

### How to modify EM to show loglikelihood every iteration

Hello,

i'm having troubles changing the source code for em algorithm. The em algorithm in opencv gives loglikelihood values for every data point. I would like to get the sum of the loglikelihood values for every iteration of the em algorithm. I have tried to modify "opencv\sources\modules\ml\src\em.cpp" by adding :

Mat loglike;
void setloglike(double f) {
if (loglike.empty())
loglike = Mat(1, 1, CV_64F);
loglike.push_back(f);
}
Mat getloglike() const {
return loglike;
}


After rebuilding i couldnt use this function, it was not "there", so i added to "opencv\sources\modules\ml\include\opencv2\ml.hpp" my lines :

CV_WRAP virtual Mat getloglike() const = 0;
CV_WRAP virtual void setloglike(double f) = 0;


but the result is crashing when i sue that function. thank you for your help.

### How to modify EM to show loglikelihood every iteration

Hello,

i'm having troubles changing the source code for em algorithm. The em algorithm in opencv gives loglikelihood values for every data point. I would like to get the sum of the loglikelihood values for every iteration of the em algorithm. I have tried to modify "opencv\sources\modules\ml\src\em.cpp" by adding :

Mat loglike;
void setloglike(double f) {
if (loglike.empty())
loglike = Mat(1, 1, CV_64F);
loglike.push_back(f);
}
Mat getloglike() const {
return loglike;
}


After rebuilding i couldnt use this function, it was not "there", so i added to "opencv\sources\modules\ml\include\opencv2\ml.hpp" my lines :

CV_WRAP virtual Mat getloglike() const = 0;
CV_WRAP virtual void setloglike(double f) = 0;


but the result is crashing when i sue that function. I'm calling the function in my own program through this code:

Mat loglikelihood, _label, _probs;
Ptr<EM> em = EM::create();
em->setClustersNumber(K);
em->setCovarianceMatrixType(EM::COV_MAT_DIAGONAL);
em->setTermCriteria(TermCriteria(TermCriteria::TermCriteria::COUNT, 50, 0));
em->trainEM(DATA, loglikelihood, _label, _probs);
Mat Log = em->getloglike();