%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% M. Welling %%%%%%% March 2000 %%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% Linear Regression %%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; colordef black; N_0 = input('Number of points you may want to select for 0 '); x_0=zeros(1,N_0); y_0=zeros(1,N_0); figure(1);clf; axis([-10 10 -0.2 1.2]); hold on; %%%%%%%% acquire points %%%%%%%%%%%%%%%%%%% for i=1:N_0, fprintf(1,'%d - Select a point labelled 0 with the mouse:\n ',i); [x,dm] = ginput2(1); x_0(i) = x; plot(x_0(i),y_0(i),'r+'); end; N_1 = input('Number of points you may want to select for 1 '); x_1=zeros(1,N_1); y_1=ones(1,N_1); for i=1:N_1, fprintf(1,'%d - Select a point labelled 1 with the mouse: \n',i); [x,dm] = ginput2(1); x_1(i) = x; plot(x_1(i),y_1(i),'r+'); end; X = [x_0 x_1;ones(1,N_0+N_1)]'; Y = [y_0 y_1]'; eta = 0.03; th = [0 0]'; t = -10:0.01:10; T = [t;ones(1,length(t))]'; for i=1:100 sg = 1./(1+exp(-X*th)); th = th + eta * (X'*Y - X'*sg); sg_t = 1./(1+exp(-T*th)); figure(1);clf plot(x_0,y_0,'r+');hold on; plot(x_1,y_1,'r+'); plot(t,sg_t,'g-','linewidth',2); axis([-10 10 -0.2 1.2]); drawnow; end plot([-th(2)/th(1) -th(2)/th(1)],[-0.2 1.2],'m-','linewidth',2);