Changeset 7693


Ignore:
Timestamp:
05/05/06 07:46:50 (18 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/Changelog

    r7690 r7693  
    1818
    1919                                                 -*-*- END OF LINE -*-*-
     20
     21 2006/05/05 Thomas Bretz
     22
     23   * mhflux/MHEnergyEst.cc:
     24     - print the result to the all-logstream
     25     - changed the fit range not to take the overflow into account
     26
     27   * mranforest/MRanForest.[h,cc]:
     28     - Use the default Reset() instead of Reset(0)
     29     - changed output while training
     30     - replaced a lot of TArrays by MArrays
     31     - at some points replaced gRandom->Rndm by gRandom->Integer
     32     - removed some obsolete arguments from ModifyDataSort
     33     - In CreateDataSort isort need not to be initialized, it is
     34       done by TMath::Sort anyhow
     35     - a small simplification to ModifyDataSort
     36     - added some const-qualifiers in funciton calls
     37
     38   * mranforest/MRanTree.[h,cc]:
     39     - replaced a lot of TArrays by MArrays
     40     - removed some obsolete calls to Reset(0) after the instatization
     41       of the array
     42     - small acceleration of the averaging when calculating fBestSplit[k]
     43     - at some points replaced gRandom->Rndm by gRandom->Integer
     44     - directly give mean[kbuild] and square[kbuild] as an argument
     45       to FindBestSplit
     46     - removed the obsolste dereferencing from the call to FindBestSplit
     47     - added some const-qualifiers in funciton calls
     48
     49
    2050
    2151 2006/05/04 Thomas Bretz
  • trunk/MagicSoft/Mars/mranforest/MRanForest.cc

    r7685 r7693  
    5151#include "MParList.h"
    5252
     53#include "MArrayI.h"
     54#include "MArrayF.h"
     55
    5356#include "MLog.h"
    5457#include "MLogManip.h"
     
    301304
    302305    fHadTrue.Set(numdata);
    303     fHadTrue.Reset(0);
     306    fHadTrue.Reset();
    304307
    305308    for (Int_t j=0;j<numdata;j++)
     
    312315    // setup labels for classification/regression
    313316    fClass.Set(numdata);
    314     fClass.Reset(0);
     317    fClass.Reset();
    315318
    316319    if (fClassify)
     
    320323    // allocating and initializing arrays
    321324    fHadEst.Set(numdata);
    322     fHadEst.Reset(0);
     325    fHadEst.Reset();
    323326
    324327    fNTimesOutBag.Set(numdata);
    325     fNTimesOutBag.Reset(0);
     328    fNTimesOutBag.Reset();
    326329
    327330    fDataSort.Set(dim*numdata);
    328     fDataSort.Reset(0);
     331    fDataSort.Reset();
    329332
    330333    fDataRang.Set(dim*numdata);
    331     fDataRang.Reset(0);
     334    fDataRang.Reset();
    332335
    333336    Bool_t useweights = fWeight.GetSize()==numdata;
     
    438441
    439442        if(calcResolution)
    440             *fLog << "no. of tree  no. of nodes  resolution in % (from oob-data -> overest. of error)" << endl;
     443            *fLog << "TreeNum BagSize NumNodes TestSize res/% (from oob-data -> overest. of error)" << endl;
    441444        else
    442             *fLog << "no. of tree  no. of nodes  rms in % (from oob-data -> overest. of error)" << endl;
     445            *fLog << "TreeNum BagSize NumNodes TestSize rms/% (from oob-data -> overest. of error)" << endl;
    443446                     //        12345678901234567890123456789012345678901234567890
    444447    }
     
    450453    // bootstrap aggregating (bagging) -> sampling with replacement:
    451454
    452     TArrayF classpopw(nclass);
    453     TArrayI jinbag(numdata); // Initialization includes filling with 0
    454     TArrayF winbag(numdata); // Initialization includes filling with 0
     455    MArrayF classpopw(nclass);
     456    MArrayI jinbag(numdata); // Initialization includes filling with 0
     457    MArrayF winbag(numdata); // Initialization includes filling with 0
    455458
    456459    float square=0;
     
    463466        // all events in the training sample
    464467 
    465         const Int_t k = Int_t(gRandom->Rndm()*numdata);
     468        const Int_t k = gRandom->Integer(numdata);
    466469
    467470        if(fClassify)
     
    472475        mean  +=fHadTrue[k]*fWeight[k];
    473476        square+=fHadTrue[k]*fHadTrue[k]*fWeight[k];
    474  
    475         winbag[k]+=fWeight[k];
     477
     478        winbag[k]+=fWeight[k]; // Increase weight if chosen more than once
    476479        jinbag[k]=1;
    477 
    478480    }
    479481
     
    483485    // In bagging procedure ca. 2/3 of all elements in the original
    484486    // training sample are used to build the in-bag data
    485     TArrayI datsortinbag=fDataSort;
    486     Int_t ninbag=0;
    487 
    488     ModifyDataSort(datsortinbag, ninbag, jinbag);
    489 
    490     fRanTree->GrowTree(fMatrix,fHadTrue,fClass,datsortinbag,fDataRang,classpopw,mean,square,
     487    const MArrayF hadtrue(fHadTrue.GetSize(), fHadTrue.GetArray());
     488    const MArrayI fclass(fClass.GetSize(), fClass.GetArray());
     489    const MArrayI datarang(fDataRang.GetSize(), fDataRang.GetArray());
     490
     491    MArrayI datsortinbag(fDataSort.GetSize(), fDataSort.GetArray());
     492
     493    ModifyDataSort(datsortinbag, jinbag);
     494
     495    fRanTree->GrowTree(fMatrix,hadtrue,fclass,datsortinbag,datarang,classpopw,mean,square,
    491496                       jinbag,winbag,nclass);
    492497
     
    501506    // determined from oob-data is underestimated, but can still be taken as upper limit.
    502507
     508    Int_t ninbag = 0;
    503509    for (Int_t ievt=0;ievt<numdata;ievt++)
    504510    {
    505511        if (jinbag[ievt]>0)
     512        {
     513            ninbag++;
    506514            continue;
     515        }
    507516
    508517        fHadEst[ievt] +=fRanTree->TreeHad((*fMatrix), ievt);
    509518        fNTimesOutBag[ievt]++;
    510 
    511519    }
    512520
     
    529537    //-------------------------------------------------------------------
    530538    // give running output
    531     *fLog << setw(5)  << fTreeNo;
    532     *fLog << setw(18) << fRanTree->GetNumEndNodes();
     539    *fLog << setw(4) << fTreeNo;
     540    *fLog << Form(" %8.1f", 100.*ninbag/numdata);
     541    *fLog << setw(9) << fRanTree->GetNumEndNodes();
     542    *fLog << Form("  %9.1f", 100.*n/numdata);
    533543    *fLog << Form("%18.2f", ferr*100.);
    534544    *fLog << endl;
     
    563573        {
    564574            v[n]=(*fMatrix)(n,mvar);
    565             isort[n]=n;
     575            //isort[n]=n;
    566576
    567577            if(TMath::IsNaN(v[n]))
     
    607617}
    608618
    609 void MRanForest::ModifyDataSort(TArrayI &datsortinbag, Int_t ninbag, const TArrayI &jinbag)
     619// Reoves all indices which are not in the bag from the datsortinbag
     620void MRanForest::ModifyDataSort(MArrayI &datsortinbag, const MArrayI &jinbag)
    610621{
    611622    const Int_t numdim=GetNumDim();
    612623    const Int_t numdata=GetNumData();
    613624
    614     ninbag=0;
     625    Int_t ninbag=0;
    615626    for (Int_t n=0;n<numdata;n++)
    616627        if(jinbag[n]==1) ninbag++;
     
    618629    for(Int_t m=0;m<numdim;m++)
    619630    {
     631        Int_t *subsort = &datsortinbag[m*numdata];
     632
    620633        Int_t k=0;
    621         Int_t nt=0;
    622         for(Int_t n=0;n<numdata;n++)
     634        for(Int_t n=0;n<ninbag;n++)
    623635        {
    624             if(jinbag[datsortinbag[m*numdata+k]]==1)
     636            if(jinbag[subsort[k]]==1)
    625637            {
    626                 datsortinbag[m*numdata+nt]=datsortinbag[m*numdata+k];
     638                subsort[n] = subsort[k];
    627639                k++;
    628640            }else{
    629                 for(Int_t j=1;j<numdata-k;j++)
     641                for(Int_t j=k+1;j<numdata;j++)
    630642                {
    631                     if(jinbag[datsortinbag[m*numdata+k+j]]==1)
     643                    if(jinbag[subsort[j]]==1)
    632644                    {
    633                         datsortinbag[m*numdata+nt]=datsortinbag[m*numdata+k+j];
    634                         k+=j+1;
     645                        subsort[n] = subsort[j];
     646                        k = j+1;
    635647                        break;
    636648                    }
    637649                }
    638650            }
    639             nt++;
    640             if(nt>=ninbag) break;
    641651        }
    642652    }
  • trunk/MagicSoft/Mars/mranforest/MRanForest.h

    r7535 r7693  
    2121class TVector;
    2222class TObjArray;
     23
     24class MArrayI;
     25class MArrayF;
    2326
    2427class MRanTree;
     
    6871    // create and modify (->due to bagging) fDataSort
    6972    Bool_t CreateDataSort();
    70     void ModifyDataSort(TArrayI &datsortinbag, Int_t ninbag, const TArrayI &jinbag);
     73    void ModifyDataSort(MArrayI &datsortinbag, const MArrayI &jinbag);
    7174
    7275public:
  • trunk/MagicSoft/Mars/mranforest/MRanTree.cc

    r7420 r7693  
    3838#include <TRandom.h>
    3939
     40#include "MArrayI.h"
     41#include "MArrayF.h"
     42
    4043#include "MLog.h"
    4144#include "MLogManip.h"
     
    9598}
    9699
    97 void MRanTree::GrowTree(TMatrix *mat, const TArrayF &hadtrue, const TArrayI &idclass,
    98                         TArrayI &datasort, const TArrayI &datarang, TArrayF &tclasspop,
    99                         float &mean, float &square, TArrayI &jinbag, const TArrayF &winbag,
     100void MRanTree::GrowTree(TMatrix *mat, const MArrayF &hadtrue, const MArrayI &idclass,
     101                        MArrayI &datasort, const MArrayI &datarang, MArrayF &tclasspop,
     102                        const Float_t &mean, const Float_t &square, const MArrayI &jinbag, const MArrayF &winbag,
    100103                        const int nclass)
    101104{
     
    110113    for (Int_t n=0;n<numdata;n++) if(jinbag[n]==1) ninbag++;
    111114
    112     TArrayI bestsplit(nrnodes);      bestsplit.Reset(0);
    113     TArrayI bestsplitnext(nrnodes);  bestsplitnext.Reset(0);
    114 
    115     fBestVar.Set(nrnodes);    fBestVar.Reset(0);
    116     fTreeMap1.Set(nrnodes);   fTreeMap1.Reset(0);
    117     fTreeMap2.Set(nrnodes);   fTreeMap2.Reset(0);
    118     fBestSplit.Set(nrnodes);  fBestSplit.Reset(0);
    119     fGiniDec.Set(numdim);     fGiniDec.Reset(0);
     115    MArrayI bestsplit(nrnodes);
     116    MArrayI bestsplitnext(nrnodes);
     117
     118    fBestVar.Set(nrnodes);    fBestVar.Reset();
     119    fTreeMap1.Set(nrnodes);   fTreeMap1.Reset();
     120    fTreeMap2.Set(nrnodes);   fTreeMap2.Reset();
     121    fBestSplit.Set(nrnodes);  fBestSplit.Reset();
     122    fGiniDec.Set(numdim);     fGiniDec.Reset();
    120123
    121124
     
    139142        const Int_t &msp =fBestVar[k];
    140143
    141         fBestSplit[k]  = (*mat)(bsp, msp);
    142         fBestSplit[k] += (*mat)(bspn,msp);
    143         fBestSplit[k] /= 2.;
     144        fBestSplit[k] = ((*mat)(bsp, msp)+(*mat)(bspn,msp))/2;
    144145    }
    145146
     
    151152}
    152153
    153 int MRanTree::FindBestSplitGini(const TArrayI &datasort,const TArrayI &datarang,
    154                                 const TArrayF &hadtrue,const TArrayI &idclass,
    155                                 Int_t ndstart,Int_t ndend, TArrayF &tclasspop,
    156                                 float &mean, float &square, Int_t &msplit,
    157                                 Float_t &decsplit,Int_t &nbest, const TArrayF &winbag,
     154int MRanTree::FindBestSplitGini(const MArrayI &datasort,const MArrayI &datarang,
     155                                const MArrayF &hadtrue,const MArrayI &idclass,
     156                                Int_t ndstart,Int_t ndend, const MArrayF &tclasspop,
     157                                const Float_t &mean, const Float_t &square, Int_t &msplit,
     158                                Float_t &decsplit,Int_t &nbest, const MArrayF &winbag,
    158159                                const int nclass)
    159160{
     
    161162    const Int_t numdata = (nrnodes-1)/2;
    162163    const Int_t mdim = fGiniDec.GetSize();
    163 
    164     TArrayF wr(nclass); wr.Reset(0);// right node
    165164
    166165    // For the best split, msplit is the index of the variable (e.g Hillas par.,
     
    202201        Double_t rld=0;
    203202
    204         TArrayF wl(nclass); wl.Reset(0.);// left node //nclass
    205         wr = tclasspop;
     203        MArrayF wl(nclass); // left node //nclass
     204        MArrayF wr(tclasspop);  // right node//nclass
    206205
    207206        Double_t critvar=-1.0e20;
     
    230229            const Double_t crit=(rln/rld)+(rrn/rrd);
    231230
    232 
    233231            if (crit<=critvar) continue;
    234232
     
    249247}
    250248
    251 int MRanTree::FindBestSplitSigma(const TArrayI &datasort,const TArrayI &datarang,
    252                                  const TArrayF &hadtrue, const TArrayI &idclass,
    253                                  Int_t ndstart,Int_t ndend, TArrayF &tclasspop,
    254                                  float &mean, float &square, Int_t &msplit,
    255                                  Float_t &decsplit,Int_t &nbest, const TArrayF &winbag,
     249int MRanTree::FindBestSplitSigma(const MArrayI &datasort,const MArrayI &datarang,
     250                                 const MArrayF &hadtrue, const MArrayI &idclass,
     251                                 Int_t ndstart,Int_t ndend, const MArrayF &tclasspop,
     252                                 const Float_t &mean, const Float_t &square, Int_t &msplit,
     253                                 Float_t &decsplit,Int_t &nbest, const MArrayF &winbag,
    256254                                 const int nclass)
    257255{
     
    259257    const Int_t numdata = (nrnodes-1)/2;
    260258    const Int_t mdim = fGiniDec.GetSize();
    261 
    262     float wr=0;// right node
    263259
    264260    // For the best split, msplit is the index of the variable (e.g Hillas par., zenith angle ,...)
     
    300296    for (Int_t mt=0; mt<fNumTry; mt++)
    301297    {
    302         const Int_t mvar=Int_t(gRandom->Rndm()*mdim);
     298        const Int_t mvar= gRandom->Integer(mdim);
    303299        const Int_t mn  = mvar*numdata;
    304300
    305301        Double_t rrn=0, rrd=0, rln=0, rld=0;
    306         Double_t esumr=0, esuml=0, e2sumr=0,e2suml=0;
    307 
    308         esumr =mean;
    309         e2sumr=square;
    310         esuml =0;
    311         e2suml=0;
     302
     303        Double_t esumr =mean;
     304        Double_t e2sumr=square;
     305        Double_t esuml =0;
     306        Double_t e2suml=0;
    312307
    313308        float wl=0.;// left node
    314         wr = tclasspop[0];
     309        float wr=tclasspop[0]; // right node
    315310
    316311        Double_t critvar=critmin;
     
    400395}
    401396
    402 void MRanTree::MoveData(TArrayI &datasort,Int_t ndstart, Int_t ndend,
    403                         TArrayI &idmove,TArrayI &ncase,Int_t msplit,
     397void MRanTree::MoveData(MArrayI &datasort,Int_t ndstart, Int_t ndend,
     398                        MArrayI &idmove,MArrayI &ncase,Int_t msplit,
    404399                        Int_t nbest,Int_t &ndendl)
    405400{
     
    410405    const Int_t mdim    = fGiniDec.GetSize();
    411406
    412     TArrayI tdatasort(numdata); tdatasort.Reset(0);
     407    MArrayI tdatasort(numdata);
    413408
    414409    // compute idmove = indicator of case nos. going left
     
    448443}
    449444
    450 void MRanTree::BuildTree(TArrayI &datasort,const TArrayI &datarang, const TArrayF &hadtrue,
    451                          const TArrayI &idclass, TArrayI &bestsplit, TArrayI &bestsplitnext,
    452                          TArrayF &tclasspop, float &tmean, float &tsquare, const TArrayF &winbag,
     445void MRanTree::BuildTree(MArrayI &datasort,const MArrayI &datarang, const MArrayF &hadtrue,
     446                         const MArrayI &idclass, MArrayI &bestsplit, MArrayI &bestsplitnext,
     447                         MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
    453448                         Int_t ninbag, const int nclass)
    454449{
     
    470465    const Int_t numdata = (nrnodes-1)/2;
    471466
    472     TArrayI nodepop(nrnodes);     nodepop.Reset(0);
    473     TArrayI nodestart(nrnodes);   nodestart.Reset(0);
    474     TArrayI parent(nrnodes);      parent.Reset(0);
    475 
    476     TArrayI ncase(numdata);       ncase.Reset(0);
    477     TArrayI idmove(numdata);      idmove.Reset(0);
    478     TArrayI iv(mdim);             iv.Reset(0);
    479 
    480     TArrayF classpop(nrnodes*nclass);  classpop.Reset(0.);//nclass
    481     TArrayI nodestatus(nrnodes);       nodestatus.Reset(0);
     467    MArrayI nodepop(nrnodes);
     468    MArrayI nodestart(nrnodes);
     469    MArrayI parent(nrnodes);
     470
     471    MArrayI ncase(numdata);
     472    MArrayI idmove(numdata);
     473    MArrayI iv(mdim);
     474
     475    MArrayF classpop(nrnodes*nclass);//nclass
     476    MArrayI nodestatus(nrnodes);
    482477
    483478    for (Int_t j=0;j<nclass;j++)
    484479        classpop[j*nrnodes+0]=tclasspop[j];
    485480
    486     TArrayF mean(nrnodes);   mean.Reset(0.);
    487     TArrayF square(nrnodes); square.Reset(0.);
     481    MArrayF mean(nrnodes);
     482    MArrayF square(nrnodes);
    488483
    489484    mean[0]=tmean;
     
    509504              tclasspop[j]=classpop[j*nrnodes+kbuild];
    510505
    511           tmean=mean[kbuild];
    512           tsquare=square[kbuild];
    513 
    514506          Int_t msplit, nbest;
    515507          Float_t decsplit=0;
    516508
    517           if ((*this.*FindBestSplit)(datasort,datarang,hadtrue,idclass,ndstart,
    518                                      ndend, tclasspop,tmean, tsquare,msplit,decsplit,
     509          if ((this->*FindBestSplit)(datasort,datarang,hadtrue,idclass,ndstart,
     510                                     ndend, tclasspop,mean[kbuild],square[kbuild],msplit,decsplit,
    519511                                     nbest,winbag,nclass))
    520512          {
  • trunk/MagicSoft/Mars/mranforest/MRanTree.h

    r7424 r7693  
    1919class TRandom;
    2020
     21class MArrayI;
     22class MArrayF;
     23
    2124class MRanTree : public MParContainer
    2225{
     
    3841
    3942    int (MRanTree::*FindBestSplit)
    40         (const TArrayI &, const TArrayI &, const TArrayF &, const TArrayI &,
    41          Int_t, Int_t , TArrayF &, float &, float &, Int_t &, Float_t &,
    42          Int_t &, const TArrayF &, const int); //!
     43        (const MArrayI &, const MArrayI &, const MArrayF &, const MArrayI &,
     44         Int_t, Int_t , const MArrayF &, const Float_t &, const Float_t &, Int_t &, Float_t &,
     45         Int_t &, const MArrayF &, const int); //!
    4346
    4447
    45     int FindBestSplitGini(const TArrayI &datasort, const TArrayI &datarang,
    46                           const TArrayF &hadtrue, const TArrayI &idclass,
    47                           Int_t ndstart, Int_t ndend, TArrayF &tclasspop,
    48                           float &mean, float &square, Int_t &msplit,
    49                           Float_t &decsplit, Int_t &nbest, const TArrayF &winbag,
     48    int FindBestSplitGini(const MArrayI &datasort, const MArrayI &datarang,
     49                          const MArrayF &hadtrue, const MArrayI &idclass,
     50                          Int_t ndstart, Int_t ndend, const MArrayF &tclasspop,
     51                          const Float_t &mean, const Float_t &square, Int_t &msplit,
     52                          Float_t &decsplit, Int_t &nbest, const MArrayF &winbag,
    5053                          const int nclass);
    5154
    52     int FindBestSplitSigma(const TArrayI &datasort, const TArrayI &datarang,
    53                            const TArrayF &hadtrue, const TArrayI &idclass,
    54                            Int_t ndstart, Int_t ndend, TArrayF &tclasspop,
    55                            float &mean, float &square, Int_t &msplit,
    56                            Float_t &decsplit, Int_t &nbest, const TArrayF &winbag,
     55    int FindBestSplitSigma(const MArrayI &datasort, const MArrayI &datarang,
     56                           const MArrayF &hadtrue, const MArrayI &idclass,
     57                           Int_t ndstart, Int_t ndend, const MArrayF &tclasspop,
     58                           const Float_t &mean, const Float_t &square, Int_t &msplit,
     59                           Float_t &decsplit, Int_t &nbest, const MArrayF &winbag,
    5760                           const int nclass);
    5861
    59     void MoveData(TArrayI &datasort, Int_t ndstart, Int_t ndend,
    60                   TArrayI &idmove, TArrayI &ncase, Int_t msplit,
     62    void MoveData(MArrayI &datasort, Int_t ndstart, Int_t ndend,
     63                  MArrayI &idmove, MArrayI &ncase, Int_t msplit,
    6164                  Int_t nbest, Int_t &ndendl);
    6265
    63     void BuildTree(TArrayI &datasort, const TArrayI &datarang, const TArrayF &hadtrue,
    64                    const TArrayI &idclass,TArrayI &bestsplit,TArrayI &bestsplitnext,
    65                    TArrayF &tclasspop, float &tmean, float &tsquare, const TArrayF &winbag,
     66    void BuildTree(MArrayI &datasort, const MArrayI &datarang, const MArrayF &hadtrue,
     67                   const MArrayI &idclass,MArrayI &bestsplit,MArrayI &bestsplitnext,
     68                   MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
    6669                   Int_t ninbag, const int nclass);
    6770
     
    9396
    9497    // functions used in tree growing process
    95     void GrowTree(TMatrix *mat, const TArrayF &hadtrue, const TArrayI &idclass,
    96                   TArrayI &datasort, const TArrayI &datarang,TArrayF &tclasspop,
    97                   float &mean, float &square, TArrayI &jinbag, const TArrayF &winbag,
     98    void GrowTree(TMatrix *mat, const MArrayF &hadtrue, const MArrayI &idclass,
     99                  MArrayI &datasort, const MArrayI &datarang,MArrayF &tclasspop,
     100                  const Float_t &mean, const Float_t &square, const MArrayI &jinbag, const MArrayF &winbag,
    98101                  const int nclass);
    99102
Note: See TracChangeset for help on using the changeset viewer.