Plot Data to find the decision boundary for logisitc regression.

조회 수: 10(최근 30일)
Vivian Chow
Vivian Chow 2021년 2월 23일
댓글: Vivian Chow 2021년 2월 28일
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;
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('Benign','Malignant', 'Decision Boundary')
axis([1,10, 1, 10])
%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;
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)
hold off
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
Vivian Chow
Vivian Chow 2021년 2월 28일
Thank you so much for your help! This defintely cleared up my confusion as to why my graph always looked different.

댓글을 달려면 로그인하십시오.


Gaurav Garg
Gaurav Garg 2021년 2월 26일
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!

Translated by