Ignore:
Timestamp:
05/03/06 14:13:48 (19 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mranforest
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc

    r7425 r7687  
    6363    : fData(0), fRFOut(0), fTestMatrix(0),
    6464    fNumTrees(-1), fNumTry(-1), fNdSize(-1), fNumObsoleteVariables(1),
     65    fLastDataColumnHasWeights(kFALSE),
    6566    fNameOutput(gsNameOutput), fDebug(kFALSE), fEstimationMode(kMean)
    6667{
     
    116117    }
    117118
    118     const Int_t nobs = fNumObsoleteVariables; // Number of obsolete columns
     119    // The number of columns which have to be removed for the training
     120    // The last data column may contain weight which also have to be removed
     121    const Int_t nobs = fNumObsoleteVariables + (fLastDataColumnHasWeights?1:0); // Number of obsolete columns
    119122
    120123    const MDataArray &dcol = *matrixtrain.GetColumns();
    121124
     125    // Make a copy of the rules for accessing the train-data
    122126    MDataArray usedrules;
    123127    for (Int_t i=0; i<ncols; i++)
     
    127131            *fLog << inf << "Skipping " << dcol[i].GetRule() << " for training" << endl;
    128132
     133    // In the case of regression store the rule to be regessed in the
     134    // last entry of your rules
    129135    MDataArray rules(usedrules);
    130136    rules.AddEntry(ver<3?"Classification":dcol[ncols-1].GetRule());
    131137
    132     // prepare matrix for current energy bin
     138    // prepare train-matrix finally used
    133139    TMatrix mat(matrixtrain.GetM());
    134140
    135     // last column must be removed (true energy col.)
     141    // Resize it such that the obsolete columns are removed
    136142    mat.ResizeTo(nrows, ncols-nobs+1);
    137143
     
    139145        gLog.SetNullOutput(kTRUE);
    140146
     147    // In the case one independant RF is trained for each bin (e.g.
     148    // energy-bin) train all of them
    141149    const Int_t nbins = ver>0 ? 1 : grid.GetSize()-1;
    142150    for (Int_t ie=0; ie<nbins; ie++)
    143151    {
     152        // In the case weights should be used initialize the
     153        // corresponding array
     154        TArrayF weights(nrows);
     155        if (fLastDataColumnHasWeights)
     156            for (Int_t j=0; j<nrows; j++)
     157            {
     158                weights[j] = matrixtrain.GetM()(j, ncols-nobs);
     159                if (j%100==0)
     160                    cout << weights[j] << " ";
     161            }
     162
     163        // Setup the matrix such that the last comlumn contains
     164        // the classifier or the regeression target value
    144165        switch (ver)
    145166        {
    146         case 0: // Replace last column by a classification
     167        case 0: // Replace last column by a classification which is 1 in
     168                // the case the event belongs to this bin, 0 otherwise
    147169            {
    148170                Int_t irows=0;
    149171                for (Int_t j=0; j<nrows; j++)
    150172                {
    151                     const Double_t energy = matrixtrain.GetM()(j,ncols-1);
    152                     const Bool_t   inside = energy>grid[ie] && energy<=grid[ie+1];
     173                    const Double_t value = matrixtrain.GetM()(j,ncols-1);
     174                    const Bool_t   inside = value>grid[ie] && value<=grid[ie+1];
    153175
    154176                    mat(j, ncols-nobs) = inside ? 1 : 0;
     
    162184                    *fLog << inf << "Training RF for";
    163185
    164                 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl;
     186                *fLog << " bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl;
    165187
    166188                if (irows==0)
     
    191213        if (ver==1)
    192214            rf.SetGrid(grid);
     215        if (fLastDataColumnHasWeights)
     216            rf.SetWeights(weights);
    193217
    194218        plist.AddToList(&rf);
  • trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h

    r7425 r7687  
    4444
    4545    Int_t        fNumObsoleteVariables; //! Training parameters
     46    Bool_t       fLastDataColumnHasWeights; //! Training parameters
    4647
    4748    TString      fFileName;             // File name to forest
     
    8485    void SetDebug(Bool_t b=kTRUE)    { fDebug    = b; }
    8586
    86     void SetNumObsoleteVariables(Int_t n=1) { fNumObsoleteVariables = n; }
     87    void SetNumObsoleteVariables(Int_t n=1)          { fNumObsoleteVariables = n; }
     88    void SetLastDataColumnHasWeights(Bool_t b=kTRUE) { fLastDataColumnHasWeights = b; }
    8789
    8890    // Train Interface
Note: See TracChangeset for help on using the changeset viewer.