how to compute separate line 2 dimensional points

2 ビュー (過去 30 日間)
Michael sch
Michael sch 2014 年 6 月 17 日
回答済み: Michael sch 2014 年 6 月 17 日
hi all i have set of points and class + -1 i want to calculate the line i want to use 1. direct linear algebra (inverse matrix , cross product) 2. use gradient descent i dont want to use the svm function or quadratic programming
x=[ 1 2 ; 3 3 ; 4 5; 45 17 ; 33 20];
label=[ 1 ;1 ;1;-1 ;-1];
gscatter(x(:,1),x(:,2),label)
so how to calculate the line wx+b , i need w,b

回答 (6 件)

David Sanchez
David Sanchez 2014 年 6 月 17 日
Once you have the plot, go to tools -> Basic fitting
select the data (1 or -1 in your case), linear and click in the bottom arrow to get the values of your parameters. You will see you can save to workspace.

Michael sch
Michael sch 2014 年 6 月 17 日
but it fits to one of the set of points and not the seperate line !! , also i dont have the process for compute the line

Zikobrelli
Zikobrelli 2014 年 6 月 17 日
Given X and Y, this function will give you your 'w' and 'b'
function [a b] = droite_reg(X,Y)
% Linear fitting
% Y = a + bX
n = length (Y);
sumY = sum(Y);
sumX = sum(X);
X2 = X.^2;sumX2 = sum(X2);
Y2 = Y.^2; sumY2 = sum(Y2);
XY = X.*Y; sumXY = sum(XY);
%slope estimation
a = (sumXY - ((sumX * sumY)/n))/ (sumX2 - ((sumX^2)/n));
% b estimation
b = (sumY/n)-(a.*(sumX/n));
% r^2
r2 = ((sumXY - ((sumX.*sumY)/n)).^2)/((sumX2-((sumX.^2)/n)).*(sumY2 - ((sumY.^2)/n)));
end
  1 件のコメント
Zikobrelli
Zikobrelli 2014 年 6 月 17 日
To get your two sets of data differentiated, you can try something like:
plot(x(label==1,1),x(label==1,2))
[w,b]=droite_reg(x(label==1,1),x(label==1,2))
hold on
plot(x(label==-1,1),x(label==-1,2),'r')
[w1,b1]=droite_reg(x(label==-1,1),x(label==-1,2))

サインインしてコメントする。


Michael sch
Michael sch 2014 年 6 月 17 日
it seems you go in the correct answer but stil the line is not between alsow what is theory (article )
x=[ 1 2 ; 3 3 ; 4 5; 45 17 ; 33 20];
label=[ 1 ;1 ;1;-1 ;-1];
gscatter(x(:,1),x(:,2),label)
%w = label'*x;
[a b] = droite_reg(x(:,1),x(:,2))
xvec=[-1:0.5:30];
yvec = b*xvec+a;
hold on;plot(xvec,yvec,'k-.');
xvec=[-1:0.5:30];
yvec = a*xvec+b;
hold on;plot(xvec,yvec,'k-.');

Michael sch
Michael sch 2014 年 6 月 17 日
i need to seperate plane like in svm and not like in the figure
http://en.wikipedia.org/wiki/Support_vector_machine
  2 件のコメント
Zikobrelli
Zikobrelli 2014 年 6 月 17 日
You might want to look into nnd4pr or perceptron in the matlab help section.
There's a demo abt 2 inout classifiers
Zikobrelli
Zikobrelli 2014 年 6 月 17 日
*input

サインインしてコメントする。


Michael sch
Michael sch 2014 年 6 月 17 日
as you see i need the black line and not the fit line

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by