www.pudn.com > adaboost_matlab.rar > try_adaboost2.m


function Try_Adaboost(train_data) 
  max_x=100,max_y=100; 
   num=20; 
 train_data=[ 15,21,1;37,30,1;49,50,1;23,67,1;38,79,1;57,99,1;64,120,1;34,125,1;82,110,1;107,108,1;134,97,1; 
      155,96,1;158,59,1;137,68,1;135,49,1;117,48,1;103,52,1;105,22,1;62,22,1;161,18,1;64,35,0;79,37,0;60,60,0; 
      76,60,0;66,71,0;89,71,0;116,69,0;116,82,0;101,82,0;82,87,0;70,86,0;76,77,0;110,89,0;108,75,0;90,82,0; 
      96,72,0;69,43,0;75,44,0;68,64,0;74,68,0]/2;  
  [total_num,dimension]=size(train_data);  
 axis([0,max_x,0,max_y]); 
 hold on  
     plot(train_data(1:num,1),train_data(1:num,2),'ro'); 
     plot(train_data(num+1:2*num,1),train_data(num+1:2*num,2),'g+'); 
  hold off   
   %对180度进行划分 
 theta_num=19; 
 theta=linspace(0,pi,theta_num); 
 value_theta=zeros(total_num,theta_num-1); 
 w_orthogonal=zeros(theta_num-1,2); 
 for i=1:theta_num-1 
     w=[cos(theta(i)),sin(theta(i))]; 
     theta_orthogonal=mod((theta(i)+pi/2),pi); 
     w_orthogonal(i,:)=[cos(theta_orthogonal),sin(theta_orthogonal)]; 
     value_theta(:,i)=train_data(:,1:2)*w_orthogonal(i,:)'; 
 end 
 Iter=20; 
 weight=ones(1,2*num)/(2*num); 
 target=ones(1,2*num); 
 target(num+1:2*num)=zeros(1,num); 
 Threshold=zeros(1,Iter);  
 Flag=zeros(1,Iter); 
 Polarity=zeros(1,Iter); 
 Error =zeros(1,Iter); 
 Beta=zeros(1,Iter); 
 new_target=zeros(1,total_num); 
  
for ii=1:Iter 
    error=0.5; 
    for k=1:theta_num-1 
      [threshold,p,e,ans_target]=weaklearner(value_theta(:,k)',weight,target,total_num); 
      if e<=error&e~=0 
          error=e; 
          Error(ii)=e; 
          Threshold(ii)=threshold; 
          Flag(ii)=k; 
          Polarity(ii)=p; 
          Beta(ii)= e / (1 - e); 
          new_target=ans_target; 
      end 
    end 
 
      delta = xor(target,new_target) 
      weight= weight .* (Beta(ii) .^ (1-delta)) 
      weight=weight./sum(weight); 
      idx=Flag(ii);    
      Print_Line(w_orthogonal(idx,:),Threshold(ii) ,max_x,max_y); 
      out=TestStrong(value_theta,train_data,target,total_num,Threshold, Flag,Polarity,Beta,ii); 
      if out==1 
        break; 
      end 
end 
           
 function out=TestStrong(value_theta,data,target,num,Threshold, Flag,Polarity,Beta,T) 
        w_ans=zeros(1,num); 
        newtarget=zeros(1,num);      
        for k=1:num 
           for i=1:T 
              idx=Flag(i); 
              value=value_theta(k,idx); 
              if Polarity(i)*value< Polarity(i)*Threshold(i)                       
                    w_ans(k)=w_ans(k)+log(1/Beta(i))/2; 
              else 
                    w_ans(k)=w_ans(k)-log(1/Beta(i))/2; 
              end 
           end 
                if w_ans(k)>0   newtarget(k)=1; 
                else newtarget(k)=0; 
                end 
        end 
        delta=xor(target,newtarget); 
        out=0; 
        if sum(delta)<1|T==20 
            %disp('sum(delta)<4'); 
            disp(['sum(delta) : ',num2str(sum(delta))]); 
            T 
            out=1; 
            hold on 
          for i=1:num 
            if delta(i)==1 
                if target(i)==1 plot(data(i,1),data(i,2),'bx'); 
                else plot(data(i,1),data(i,2),'bx'); 
                end 
            end 
          end 
          hold off  
        end        
    
 function [threshold,p,e,ans_target]=weaklearner(data,weight,target,num) %特征即是x轴 或者 y轴 投影        
        posWeight=0; 
        negWeight=0; 
        for i=1:num 
            if target(i)==1 
                posWeight=weight(i)+posWeight; 
            elseif target(i)==0 
                negWeight=weight(i)+negWeight; 
            else  
                disp('weaklearner wrong \n'); 
            end 
        end     
         
        [data_sort,Idx]=sort(data);        
        lposweight=0; 
        lnegweight=0; 
        bestErr=0.5; 
        bestParity=0; 
        bestThresh=-1;  
        for i=2:num  
            if target(Idx(i-1))==1 
                lposweight=weight(Idx(i-1))+lposweight; 
            else 
                lnegweight=weight(Idx(i-1))+lnegweight; 
            end 
             
            if data_sort(i)~=data_sort(i-1) 
                if ( lposweight+negWeight-lnegweight ) < bestErr  
				     bestErr = lposweight + negWeight - lnegweight; 
				     bestParity = -1; 
				    bestThresh = (data_sort(i) + data_sort(i-1)) / 2; 
                elseif lnegweight+posWeight-lposweight  < bestErr  
				bestErr = lnegweight + posWeight - lposweight; 
				bestParity = 1; 
				bestThresh = (data_sort(i) + data_sort(i-1)) / 2; 
                end     
            end 
        end 
        ans_target=zeros(1,num);      
    threshold = bestThresh; 
	p = bestParity; 
	e = bestErr;     
     for i=1:num 
            if p*data(i)