Changeset 7693 for trunk/MagicSoft/Mars/mranforest/MRanForest.cc
- Timestamp:
- 05/05/06 07:46:50 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MRanForest.cc
r7685 r7693 51 51 #include "MParList.h" 52 52 53 #include "MArrayI.h" 54 #include "MArrayF.h" 55 53 56 #include "MLog.h" 54 57 #include "MLogManip.h" … … 301 304 302 305 fHadTrue.Set(numdata); 303 fHadTrue.Reset( 0);306 fHadTrue.Reset(); 304 307 305 308 for (Int_t j=0;j<numdata;j++) … … 312 315 // setup labels for classification/regression 313 316 fClass.Set(numdata); 314 fClass.Reset( 0);317 fClass.Reset(); 315 318 316 319 if (fClassify) … … 320 323 // allocating and initializing arrays 321 324 fHadEst.Set(numdata); 322 fHadEst.Reset( 0);325 fHadEst.Reset(); 323 326 324 327 fNTimesOutBag.Set(numdata); 325 fNTimesOutBag.Reset( 0);328 fNTimesOutBag.Reset(); 326 329 327 330 fDataSort.Set(dim*numdata); 328 fDataSort.Reset( 0);331 fDataSort.Reset(); 329 332 330 333 fDataRang.Set(dim*numdata); 331 fDataRang.Reset( 0);334 fDataRang.Reset(); 332 335 333 336 Bool_t useweights = fWeight.GetSize()==numdata; … … 438 441 439 442 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; 441 444 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; 443 446 // 12345678901234567890123456789012345678901234567890 444 447 } … … 450 453 // bootstrap aggregating (bagging) -> sampling with replacement: 451 454 452 TArrayF classpopw(nclass);453 TArrayI jinbag(numdata); // Initialization includes filling with 0454 TArrayF winbag(numdata); // Initialization includes filling with 0455 MArrayF classpopw(nclass); 456 MArrayI jinbag(numdata); // Initialization includes filling with 0 457 MArrayF winbag(numdata); // Initialization includes filling with 0 455 458 456 459 float square=0; … … 463 466 // all events in the training sample 464 467 465 const Int_t k = Int_t(gRandom->Rndm()*numdata);468 const Int_t k = gRandom->Integer(numdata); 466 469 467 470 if(fClassify) … … 472 475 mean +=fHadTrue[k]*fWeight[k]; 473 476 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 476 479 jinbag[k]=1; 477 478 480 } 479 481 … … 483 485 // In bagging procedure ca. 2/3 of all elements in the original 484 486 // 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, 491 496 jinbag,winbag,nclass); 492 497 … … 501 506 // determined from oob-data is underestimated, but can still be taken as upper limit. 502 507 508 Int_t ninbag = 0; 503 509 for (Int_t ievt=0;ievt<numdata;ievt++) 504 510 { 505 511 if (jinbag[ievt]>0) 512 { 513 ninbag++; 506 514 continue; 515 } 507 516 508 517 fHadEst[ievt] +=fRanTree->TreeHad((*fMatrix), ievt); 509 518 fNTimesOutBag[ievt]++; 510 511 519 } 512 520 … … 529 537 //------------------------------------------------------------------- 530 538 // 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); 533 543 *fLog << Form("%18.2f", ferr*100.); 534 544 *fLog << endl; … … 563 573 { 564 574 v[n]=(*fMatrix)(n,mvar); 565 isort[n]=n;575 //isort[n]=n; 566 576 567 577 if(TMath::IsNaN(v[n])) … … 607 617 } 608 618 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 620 void MRanForest::ModifyDataSort(MArrayI &datsortinbag, const MArrayI &jinbag) 610 621 { 611 622 const Int_t numdim=GetNumDim(); 612 623 const Int_t numdata=GetNumData(); 613 624 614 ninbag=0;625 Int_t ninbag=0; 615 626 for (Int_t n=0;n<numdata;n++) 616 627 if(jinbag[n]==1) ninbag++; … … 618 629 for(Int_t m=0;m<numdim;m++) 619 630 { 631 Int_t *subsort = &datsortinbag[m*numdata]; 632 620 633 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++) 623 635 { 624 if(jinbag[ datsortinbag[m*numdata+k]]==1)636 if(jinbag[subsort[k]]==1) 625 637 { 626 datsortinbag[m*numdata+nt]=datsortinbag[m*numdata+k];638 subsort[n] = subsort[k]; 627 639 k++; 628 640 }else{ 629 for(Int_t j= 1;j<numdata-k;j++)641 for(Int_t j=k+1;j<numdata;j++) 630 642 { 631 if(jinbag[ datsortinbag[m*numdata+k+j]]==1)643 if(jinbag[subsort[j]]==1) 632 644 { 633 datsortinbag[m*numdata+nt]=datsortinbag[m*numdata+k+j];634 k +=j+1;645 subsort[n] = subsort[j]; 646 k = j+1; 635 647 break; 636 648 } 637 649 } 638 650 } 639 nt++;640 if(nt>=ninbag) break;641 651 } 642 652 }
Note:
See TracChangeset
for help on using the changeset viewer.