I am trying to visualize the decision boundary when using a Bayesian classifier in MATLAB. To do this, I have written an implicit functions which uses training data to determine which of two classes a datapoint P=(x,y) belongs to. This is done be evaluating if the function is positive or negative. The decision boundary corresponds to points where the function is zero.
However, when trying to use this function (stored in a seperate .m file), ezplot fail to draw any lines and instead issues the warning: Warning: Contour not rendered for constant ZData
In contour>parseargs at 204 In contour at 72 In ezplot>ezimplicit at 312 In ezplot at 155
I've made some sample code so that the problem can be recreated:
%Create synthetic dataset. Bivariate gaussian mixture
p1 = 0.7;
p2 = 1 - p1;
%Number of datapoints
N = 50;
%Means
mu1 = [0; 0];
mu2 = [2.5; 2.5];
%Covariance
Sigma = eye(2);
%Loss matrix
L = [0 1; 1 0];
%Create data, 2 classes
D1 = mvnrnd(mu1, Sigma, round(N*p1));
D2 = mvnrnd(mu2, Sigma, round(N*p2));
% Visualize decision boundary using kNN classifier
f2=figure(2);
plot(D1(:,1),D1(:,2),'b.',D2(:,1),D2(:,2),'r.');
hold on;
k=5;
ezp=ezplot(@(x,y) kNNdbEval(x,y,D1,D2,k,L)); %<----- Problem?
axis tight
title(['k = ' num2str(k)]);
with my function kNNdbEval being:
function dbEval = kNNdbEval(x,y,X1,X2,k,L);
%Calculates decision boundary by eg. 2.116 in Pattern Recognition
%
%X1:Data from class 1
%X2:Data from class 2
%x:1. coordinate of point to evaluate
%y:2. coordinate of point to evaluate
%k:nearest neighbor parameters
%dbEval=x.^2+y*x+sqrt(x.^2+y.^2);
[N1 d] = size(X1);
[N2 d] = size(X2);
%Estimate priors
N = N1+N2;
p1 = N1/N;
p2 = N2/N;
[~, d1] = knnsearch(X1,[x y],'k',k);
d1 = d1(k);
[~, d2] = knnsearch(X2,[x y],'k',k);
d2=d2(k);
dbEval = (N2*(1/(d2)^d))/(N1*(1/(d1)^d)) ...
-((p2*(L(2,1)-L(2,2)))/(p1*(L(1,2)-L(1,1))));
I belive my classifier is functioning correctly, as I can hack out an approximation to the decision boundary by evaluating lots of points in the plane where the points lie and plot an image of where kNNdbEval is close to zero. See the attached image.
My suspicion is that the function (being made up of two knn-searches) does not necessarily become exactly zero at any point, but I am interested in detecting when it flips from positive to negative. Any ideas of either getting ezplot to work to this end, or by some other method?