Add a devision line in scatter plot of perceptron

조회 수: 7 (최근 30일)
Hans Huybrechts
Hans Huybrechts 2020년 3월 31일
편집: Jayanti 2025년 7월 1일
I have made a single layer perceptron which will divide a set of points based on the class which is given in the last column of my dataset. I want to visualize the obtained weight vector by a line in the scatter plot of the datapoints. Can anyone help me how I can put the weight vector as a line in the scatter plot? Here's the code for the training dataset
%Data
%Training Data
DataTR=readtable('D:\Universiteit\Hans\Erasmus\Machine Learning\KOM6110_HansHuybrechts_000E19503601_Assignment1\class2_tr.txt');
i_tr=height(DataTR);
X0=ones(i_tr,1);
X1_TR=DataTR.Var1;
X2_TR=DataTR.Var2;
X_TR=[X0 X1_TR X2_TR];
t_TR=DataTR.Var3;
%Training Code
W=rand(3,1);
y_tr=zeros(i_tr,1);
o_TR=zeros(i_tr,1);
dW=ones(3,1);
iterations=1;
%Learning with gradient descent
theta=0.1;
while mean(abs(dW))>0.001
y_tr=W(1)+ X1_TR.*W(2)+ X2_TR.*W(3);
o_TR=hardlim(y_tr);
error(iterations)=1/2*sum((t_TR-o_TR).^2);
dW=theta*(X_TR.'*(t_TR-o_TR));
W=W+dW;
iterations=iterations+1;
end
%Learning with stochastic gradient descent
j=1;
theta=0.005;
while j<=i_tr
y_tr(j)=W(1)+X1_TR(j)*W(2)+X2_TR(j)*W(3);
o_TR(j)=hardlim(y_tr(j));
dW=theta*(X_TR(j,:).'*(t_TR(j)-o_TR(j)));
W=W+dW;
j=j+1;
end
gscatter(X1_TR,X2_TR,t_TR,'br','xo',8,'on')
  댓글 수: 3
Hans Huybrechts
Hans Huybrechts 2020년 4월 2일
here's the training data
Eduardo Morales
Eduardo Morales 2022년 1월 12일
quetal encontraste alguna solucion ?
figure
gscatter(X1_TR,X2_TR,t_TR,'br','xo',8,'on')
hold on
line(x,y)
le agrege esto pero nose si esta bien.

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

답변 (1개)

Jayanti
Jayanti 2025년 7월 1일
편집: Jayanti 2025년 7월 1일
Hi Hans,
Once your model is trained, the weight vector W = [w1; w2; w3] defines a linear decision boundary of the form:
On rearranging this to solve for "y" as a function of "x":
We can plot this on top of your scatter plot to visualize the decision boundary.
You can refer to the below code for more details. It creates 100 evenly spaced "x" values and corresponding "y" values using the decision boundary.
% Plotting Data
figure
gscatter(X1_TR, X2_TR, t_TR, 'br', 'xo', 8, 'on')
hold on
% Plotting Decision Boundary Line
x_vals = linspace(min(X1_TR), max(X1_TR), 100);
y_vals = -(W(2)/W(3)) * x_vals - (W(1)/W(3));
plot(x_vals, y_vals, 'k-', 'LineWidth', 2)
hold off
I am also attaching official MathWorks documentation on "plot" for your reference:

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by