Plot Data to find the decision boundary for logisitc regression.

10 views (last 30 days)
Figure 1
I am using the Wisconsin Breast Cancer Diagnostic Data Set found on the UCI Machine Learning website. I am trying to implement logistic regression to classify the tumour as either malignant or benign, I changed the class to 0 for benign and 1 for malignant, for two features only, Marginal Adhesion and Clump Thickness. This is for an assignment at uni and we did not study much beyond logistic regression. I am aware there are other classification techniques out there but that is beyond the scope of my skills. I am trying to plot Maginal Adhesion against Clump Thickness however my graph ends up looking like the one above. I have used the following code that I found online.
This is the code to plot the data in a graph.
function plotData(X,y)
figure; hold on
pos = find(y==1); %Malignant
neg = find(y==0); %Benign
plot(X(pos,1), X(pos, 2), 'k+','LineWidth',5,'MarkerSize',5);
plot(X(neg,1), X(neg,2), 'ko','MarkerFaceColor','b','MarkerSize',5);
hold off;
end
Edit: I have included the code I used to plot the Decision Boundary, sorry I should have included that before too.
function plotDecisionBoundary(theta, X, y)
%Plot Data
plotData(X(:,2:3), y);
hold on
if size(X, 2) <= 3
%Only need 2 points to define a line, so choose two endpoints
plot_x = [min(X(:,2))-4, max(X(:,2))+4];
%Calculate the decision boundary line
plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));
%Plot and adjust axes for better viewing
plot(plot_x, plot_y)
%Legend
legend('Benign','Malignant', 'Decision Boundary')
axis([1,10, 1, 10])
else
%Here is the grid range
u = linspace(-1, 1.5, 50);
v = linspace(-1, 1.5, 50);
z = zeros(length(u), length(v));
%Evaluate z = theta*x over the grid
for i = 1:length(u)
for j = 1:length(v)
z(i,j) = mapfeature(u(i), v(j))*theta;
end
end
z = z'; %important to transpose z before contour
%Plot z = 0
%Notice you need to specify the range [0,0]
countour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off
end
To be honest, I am not too sure whether the code is right or not as I am still quite new to Matlab and am trying to get it working from watching videos online.
I want my graph to look like the following below; can anyone let me know what I am doing wrong?
Any help is appreciated. Thank you in advance.
Figure 2
  6 Comments
the cyclist
the cyclist on 26 Feb 2021
I downloaded the data file, and it is immediately obvious that the data you posted are not the same as the data in the figure you are trying to reproduce. Specifically, the data you uploaded are integers, and the figure has values that are clearly non-integer.
So, it seems there is a problem with the data themselves.
Vivian Chow
Vivian Chow on 28 Feb 2021
Thank you so much for your help! This defintely cleared up my confusion as to why my graph always looked different.

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 26 Feb 2021
Hi Vivian,
You can look at the following link, where you can get more information on plotting model.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!