Adaboost理解笔记(matlab实现)

目前集成学习有bagging、boosting算法,二者异同能够参考这篇博客 随机森林(RandomForest)是一种bagging的方法; Adaboost、GBDT、XGBoost 都是一种boosting方法。html

本文只介绍AdaBoost的基本原理方便本身复习,并附上两年前写的matlab程序。算法

基本原理

参考:李航的《统计学习方法》 AdaBoost经过加大分类偏差率小的弱分类器的权重,使其在表决中起的做用较大,减少分类偏差率大的弱分类器的权重,使其在表决中起较小的做用。微信

编辑器不支持工具,用图片替换

举例表示

微信图片_20180727171209.jpg

matlab程序实现

adaboost_model.m 模型函数:dom

%%---------- 《提高算法-多维》:建模专用 -------------用于二分类
%说明:      
%       输入X=[n*m],Y=[n*1](-1/1二类值);
%       输出CUT=[p*4],CUT(:,1)为切分点,CUT(:,2:3)为切分值,CUT(:,4)为权重系数,CUT(:,5)为变量标志位;
%       ERR为训练偏差精度,ECHO为训练最大步数;
%       使用C_CART函数进行切分。
%例如:
%       x=[2 4 0 3 1 5 6 7 8 9;2 6 7 22 5 15 4 9 8 1;5 8 12 9 0 11 30 7 6 4;5 4 9 0 0.2 2 7 6 3 1]';
%       y=[1 -1 1 -1 1 -1 1 1 1 -1]';

%做者:zlw 

%时间:2016-07-27

%---------------------------------------------------
%%

function [ CUT ] = adaboost_model( x,y,ERR,ECHO)
%ADABOOST_MODEL Summary of this function goes here
%   Detailed explanation goes here
%%自适应提高算法(adaboosting)实现多维特征进行 二分类(-1,1)问题;

xy=[x,y];

%--------------------------------------------
[n,m]=size(x);

%xy=sortrows(xy,1);%按某列排序;
for i=1:m
    [XY(:,:,i),DI(:,i)]=sortrows(xy,i);%将矩阵按照第i列升序跟着排列
end

D=1/n*ones(n,m);%佯本初始权值;

%-------   -------
y=xy(:,end);
echo=1;p_err=1;Fx=0;CUT=[];
while (p_err>ERR && echo<ECHO)

    G0=zeros(n,1);D0=zeros(n,1);
    for i=1:m
        [cut(i),cut_v(i,:),err(i),G(:,i)] = C_cart(XY(:,i,i) ,XY(:,end,i),D(:,i));%计算各个变量分类的最优切分点、偏差、对应的预测值  
    end

    min_err=min(err); %最小分类偏差,(须要注意err可能为0)
    min_i=find(err==min_err);min_i=min_i(1);%分类偏差最小的变量序号
    op_cut=cut(min_i);  %最优分类阈值
    op_G=G(:,min_i); %分类值
    op_cutv=cut_v(min_i,:);%分类值
    alpha=0.5*log((1-min_err)/min_err); %G的系数

    CUT=[CUT;op_cut,op_cutv,alpha,min_i];
    
    G0(DI(:,min_i))=op_G;%原始序号下的预测值
    
    Fx=alpha*G0+Fx;%决策函数
    %计算分类错误率
    a=Fx>0;
    b=y>0;
    p_err=sum(abs(a-b))/n;


    %---------- 更新权值 ----------  
    sumD=0;
    for i=1:n
       D(i,min_i)=D(i,min_i)*exp(-alpha*XY(i,end,min_i)*op_G(i)); 
       sumD=sumD+D(i,min_i);
    end
    D(:,min_i)=D(:,min_i)/sumD;

    D0(DI(:,min_i))=D(:,min_i);%原始序号下的佯本权值
    for k=1:m
       D(:,k)=D0(DI(:,k)) ;%各列为列排序后的佯本权值
    end

    echo=echo+1;
    
end

end

C_cat.m 切分函数:编辑器

function [ op_cut,op_cutv ,min_err,op_G] = C_cart( x,y,D )
%C_CART Summary of this function goes here
%   Detailed explanation goes here
[n,~]=size(x);
class_y=unique(y);
pre0=ones(n,1);

    for i=1:n-1
       cut(i)= (x(i)+x(i+1))/2;%分类阈值

    %----- %获得分类偏差   
       pre(1:i)=class_y(1)*pre0(1:i);%归为类1
       pre(i+1:n)=class_y(2)*pre0(i+1:n);%归为类2
       G1=[pre(1:i),pre(i+1:n)]';
       err1=0;
       for j=1:n
          if y(j)~=pre(j)
              err1=err1+D(j);
          end
       end

       pre(1:i)=class_y(2)*pre0(1:i);%归为类2
       pre(i+1:n)=class_y(1)*pre0(i+1:n);%归为类1
       G2=[pre(1:i),pre(i+1:n)]';
       err2=0;
       for j=1:n
          if y(j)~=pre(j)
              err2=err2+D(j);
          end
       end

       if err1<=err2
           err(i)=err1;G(:,i)=G1;cut_v(i,:)=[class_y(1),class_y(2)];
       else
           err(i)=err2;G(:,i)=G2;cut_v(i,:)=[class_y(2),class_y(1)];
       end
    %--------------------------

    end
    
    
    min_err=min(err); %最小分类偏差
    min_i=find(err==min_err);min_i=min_i(1);
    op_cut=cut(min_i);  %最优分类阈值
    op_G=G(:,min_i); %分类值
    op_cutv=cut_v(min_i,:);

end

adaboost_pre.m 预测函数:函数

%%---------- 《提高回归树算法》:预测专用 -------------
%说明: 
%       输入:测试数据X=[n*m],生成树CUT=[p*5],CUT(:,1)为切分点,CUT(:,2:3)为切分值,CUT(:,4)为权重系数,CUT(:,5)为变量标志位;
%       输出:预测Y=[n*1];
%      

%做者:zlw 

%时间:2016-07-27
%%
function [ Y  ] = adaboost_pre( x, CUT )
%ADABOOST_PRE Summary of this function goes here
%   Detailed explanation goes here
[n,~]=size(x);

Y=[];
for i=1:n
    y_predict=0;
    
    for j=1:size(CUT,1)
        n_r=CUT(j,end);
        if x(i,n_r)<CUT(j,1)
            y_predict=y_predict+CUT(j,4)*CUT(j,2);
        else
            y_predict=y_predict+CUT(j,4)*CUT(j,3);
        end
        
    end
    

    Y = [Y;y_predict];
end

end

test.m测试文件工具

clc;clear;close all;

x=[2 4 0 3 1 5 6 7 8 9;2 6 7 22 5 15 4 9 8 1;5 8 12 9 0 11 30 7 6 4;5 4 9 0 0.2 2 7 6 3 1]';
% x=[2 4 0 3 1 5 6 7 8 9]';
y=[1 -1 1 -1 1 -1 1 1 1 -1]';

ERR=0.08;%最大训练偏差
ECHO=100;%最大训练循环次数

[ CUT ] = adaboost_model( x,y ,ERR,ECHO);%训练

[ Y  ] = adaboost_pre( x, CUT );%预测


%计算分类错误率
a=Y>0;
b=y>0;
p_err=sum(abs(a-b))/size(x,1);

disp(CUT);
disp(p_err);
相关文章
相关标签/搜索