I want to perform classification of two classes using Gaussian Mixture Models with MATLAB.
I doing training by creating two models with the function gmdistribution.fit
NComponents = 1;
for class=1:2
model(class).obj = gmdistribution.fit(trainData(class).feature,NComponents,'Regularize',.1);
end
Then, given test data points, I want to know how to classify them. What I am doing now is to obtain the posterior probability for each point in each model:
vectorClasses = zeros(1,2);
for class=1:2
Pos= posterior(model(class).obj,testDataPoint);
suma=0;
for k=1:NComponents
suma = suma + Pos(1,k)*model(class).obj.PComponents(k);
end
vectorClasses(class)=suma;
end
[v ix] = sort(vectorClasses,'descend');
if ix(1)==realClass
success= 1;
else
success= 0;
end
I sum the multiplication of the posterior probability of each component and the probability of the component in the model. Then I sort the probabilities obtained in each model. I say the test data point correspond to the class with the highest probability.
Am I doing it ok? How is the correct way? Is there an easiest way to do it?