Add a devision line in scatter plot of perceptron
이전 댓글 표시
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
Ameer Hamza
2020년 3월 31일
Can you attach you dataset. It will help in suggesting a solution.
Hans Huybrechts
2020년 4월 2일
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개)
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:
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!