⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gentleboost_model.c

📁 Gentleboost
💻 C
📖 第 1 页 / 共 2 页
字号:

/*

  Gentle AdaBoost Classifier with two different weak-learners : Decision Stump and Perceptron.
  Multi-class problem is performed with the one-against-all strategy.

  Usage
  ------

  model = gentleboost_model(X , y , [T] , [options]);

  
  Inputs
  -------

  X                                     Features matrix (d x N) 
  y                                     Labels (1 x N). If y represent binary labels vector then y_i={-1,1}, i=1,...,N
  T                                     Number of weak learners (default T = 100)
  options
              weaklearner               Choice of the weak learner used in the training phase
			                            weaklearner = 0 <=> minimizing the weighted error : sum(w * |z - h(x;(th,a,b))|^2) / sum(w), where h(x;(th,a,b)) = (a*(x>th) + b) in R
			                            weaklearner = 1 <=> minimizing the weighted error : sum(w * |z - h(x;(a,b))|^2), where h(x;(a,b)) = sigmoid(x ; a,b) in R

			  epsi                      Epsilon constant in the sigmoid function used in the perceptron (default epsi = 1)
              lambda                    Regularization parameter for the perceptron's weights update (default lambda = 1e-3)
			  max_ite                   Maximum number of iterations of the perceptron algorithm (default max_ite = 100)

  Outputs
  -------
  
  model                                 Structure of model ouput
  
	          featureIdx                Features index of the T best weaklearners (T x m) where m is the number of class. 
			                            For binary classification m is force to 1.
			  th                        Optimal Threshold parameters (1 x T)
			  a                         Affine parameter(1 x T)
			  b                         Bias parameter (1 x T)




  To compile
  ----------


  mex  -output gentleboost_model.dll gentleboost_model.c

  mex  -f mexopts_intel10amd.bat -output gentleboost_model.dll gentleboost_model.c



  Example 1
  ---------


  load iris %wine
  %load heart
  %load wbc

  T                       = 10;

  options.weaklearner     = 0;
  options.epsi            = 0.5;
  options.lambda          = 1e-3;
  options.max_ite         = 3000;
  
  model                   = gentleboost_model(X , y , T , options);
  [yest , fx]             = gentleboost_predict(X , model , options);
  Perf                    = sum(y == yest)/length(y)


 Author : S閎astien PARIS : sebastien.paris@lsis.org
 -------  Date : 01/27/2009

 Ref    : Friedman, J. H., Hastie, T. and Tibshirani, R. "Additive Logistic Regression: a Statistical View of Boosting." (Aug. 1998) 
 ------- 



*/


#include <time.h>
#include <math.h>
#include <mex.h>


#define huge 1e300


#define znew   (z = 36969*(z&65535) + (z>>16) )
#define wnew   (w = 18000*(w&65535) + (w>>16) )
#define MWC    ((znew<<16) + wnew )
#define SHR3   ( jsr ^= (jsr<<17), jsr ^= (jsr>>13), jsr ^= (jsr<<5) )



#define randint SHR3
#define rand() (0.5 + (signed)randint*2.328306e-10)
#define sign(a) ((a) >= (0) ? (1.0) : (-1.0))


typedef unsigned long UL;

static UL jsrseed = 31340134 , jsr;




struct opts
{

  int    weaklearner;

  double epsi;

  double lambda;

  int    max_ite;

};


struct weak_learner
{

	double *featureIdx;

	double *th;

	double *a;

	double *b;

};



/* Function prototypes */

void randini(void);

void qs( double * , int , int  ); 

void qsindex( double * , int * , int , int  );

void transpose(double *, double * , int , int);


void  gentelboost_decision_stump(double * , double * , int , struct opts , double * , int , 
								 double *, double *, double * , double *,
		                         int  , int );



void  gentelboost_perceptron(double * , double * , int , struct opts , double * , int ,
							 double *, double *, double * ,
		                     int  , int );


/*-------------------------------------------------------------------------------------------------------------- */



void mexFunction( int nlhs, mxArray *plhs[] , int nrhs, const mxArray *prhs[] )

{
    
    double *X , *y;
    
    int d , N , T=100;
    
    mxArray *mxtemp;
    
    struct weak_learner model;
    
    struct opts options = {0 , 1 , 1e-3 , 100};
    
    const char *fieldnames_model[4] = {"featureIdx" , "th" , "a" , "b"};
    
    double *tmp , *ysorted , *labels;
    
    int i , tempint , m=0 , currentlabel;
    
    
    if ((nrhs < 2) )

	{

		mexErrMsgTxt("At least two arguments are requiered, i.e. model = gentleboost_model(X , y , [T] , [options]);");

	}


    
    /* Input 1  */
    
    if( (mxGetNumberOfDimensions(prhs[0]) !=2) || !mxIsDouble(prhs[0]) )
    {
        
        mexErrMsgTxt("X must be (d x N) in double format");
        
    }
    
    
    X           = mxGetPr(prhs[0]);
    
    d           = mxGetM(prhs[0]);
    
    N           = mxGetN(prhs[0]);
    
    
    
    /* Input 2  */
    
    if ((nrhs > 1) && !mxIsEmpty(prhs[1]) )
        
    {
        
        y        = mxGetPr(prhs[1]);
        
    }
    
    ysorted      = (double *)mxMalloc(N*sizeof(double));
    
    for ( i = 0 ; i < N ; i++ )
    {
        
        ysorted[i] = y[i];
        
    }
    
    
    qs( ysorted , 0 , N - 1 );
    
    
    labels       = (double *)mxMalloc(sizeof(double));
    
    labels[m]    = ysorted[0];
    
    currentlabel = labels[0];
    
    for (i = 0 ; i < N ; i++)
    {
        if (currentlabel != ysorted[i])
        {
            labels       = (double *)mxRealloc(labels , (m+2)*sizeof(double));
            
            labels[++m]  = ysorted[i];
            
            currentlabel = ysorted[i];
            
        }
    }
    
    m++;
    
    if( m == 2) /* Binary case */
    {
        
        m         = 1;

        labels[0] = 1.0; /*Force positive label in the first position*/
           
    }
    
    /* Input 3  */
    
    
    if ((nrhs > 2) && !mxIsEmpty(prhs[2]) )
        
    {
        
        T          = (int) mxGetScalar(prhs[2]);
        
    }
    
    
    
    /* Input 4  */
    
    
    if ((nrhs > 3) && !mxIsEmpty(prhs[3]) )
        
    {
        
        mxtemp                            = mxGetField(prhs[3] , 0 , "weaklearner");
        
        if(mxtemp != NULL)
        {
            
            tmp                           = mxGetPr(mxtemp);
            
            tempint                       = (int) tmp[0];
            
            if((tempint < 0) || (tempint > 1))
            {
                
                mexErrMsgTxt("weaklearner = {0,1}, force to 0");
                
                options.weaklearner        = 0;
                
            }
            else
            {
                
                options.weaklearner        = tempint;
                
            }
            
        }


        mxtemp                            = mxGetField(prhs[3] , 0 , "epsi");
        
        if(mxtemp != NULL)
        {
            
            tmp                           = mxGetPr(mxtemp);
            
            options.epsi                  = tmp[0];
            
            
        }
        
        
        mxtemp                            = mxGetField(prhs[3] , 0 , "lambda");
        
        if(mxtemp != NULL)
        {
            
            tmp                           = mxGetPr(mxtemp);
            
            options.lambda                = tmp[0];
            
            
        }
        
        
        mxtemp                            = mxGetField(prhs[3] , 0 , "max_ite");
        
        if(mxtemp != NULL)
        {
            
            tmp                           = mxGetPr(mxtemp);
            
            tempint                       = (int) tmp[0];
            
            if(tempint < 1)
            {
                
                mexErrMsgTxt("max_ite > 0, force to default value");
                
                options.max_ite           = 10;
                
            }
            
            else
                
            {
                options.max_ite           =  tempint;
                
            }
            
        }
    }
    
        /*------------------------ Main Call ----------------------------*/
    
    if(options.weaklearner == 0)
    {
        
        
        plhs[0]              =  mxCreateStructMatrix(1 , 1 , 4 , fieldnames_model);
        
        
        for(i = 0 ; i < 4 ; i++)
        {
            
            
            mxSetFieldByNumber(plhs[0] ,0 , i , mxCreateNumericMatrix(T , m , mxDOUBLE_CLASS,mxREAL));
            
        }
        
        model.featureIdx = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[0] ) );
        model.th         = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[1] ) );
        model.a          = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[2] ) );
        model.b          = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[3] ) );
        
        
        
        gentelboost_decision_stump(X , y , T , options , labels , m ,
                                   model.featureIdx , model.th , model.a , model.b,
                                   d , N);
        
    }
    
    if(options.weaklearner == 1)
    {
        
        
        plhs[0]              =  mxCreateStructMatrix(1 , 1 , 4 , fieldnames_model);

       
        for(i = 0 ; i < 4 ; i++)
        {
            
            
            mxSetFieldByNumber(plhs[0] ,0 , i , mxCreateNumericMatrix(T , m , mxDOUBLE_CLASS,mxREAL));
            
        }
        
        model.featureIdx = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[0] ) );
        model.th         = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[1] ) );
        model.a          = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[2] ) );
        model.b          = mxGetPr( mxGetField( plhs[0], 0, fieldnames_model[3] ) );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -