Add a devision line in scatter plot of perceptron

1 vue (au cours des 30 derniers jours)
Hans Huybrechts
Hans Huybrechts le 31 Mar 2020
Modifié(e) : Jayanti le 1 Juil 2025
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 commentaires
Hans Huybrechts
Hans Huybrechts le 2 Avr 2020
here's the training data
Eduardo Morales
Eduardo Morales le 12 Jan 2022
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.

Connectez-vous pour commenter.

Réponses (1)

Jayanti
Jayanti le 1 Juil 2025
Modifié(e) : Jayanti le 1 Juil 2025
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:

Catégories

En savoir plus sur Deep Learning Toolbox dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by