47 #ifndef ROOT_TMVA_Tools
50 #ifndef ROOT_TMVA_Timer
54 #ifndef ROOT_TMVA_SVWorkingSet
58 #ifndef ROOT_TMVA_SVEvent
62 #ifndef ROOT_TMVA_SVKernelFunction
90 using std::stringstream;
112 , fSVKernelFunction(0)
115 , fDoubleSigmaSquared(0)
127 fNumVars = theData.GetVariableInfos().size();
128 for(
int i=0; i<fNumVars; i++){
129 fVarNames.push_back(theData.GetVariableInfos().at(i).GetTitle());
147 , fSVKernelFunction(0)
150 , fDoubleSigmaSquared(0)
173 fSupportVectors->clear();
174 for (
UInt_t i=0; i<fInputData->size(); i++) {
175 delete fInputData->at(i);
177 if (fWgSet !=0) {
delete fWgSet; fWgSet=0; }
178 if (fSVKernelFunction !=0 ) {
delete fSVKernelFunction; fSVKernelFunction = 0; }
187 fSupportVectors->clear();
188 for (
UInt_t i=0; i<fInputData->size(); i++){
189 delete fInputData->at(i);
193 if (fWgSet !=0) { fWgSet=0; }
194 if (fSVKernelFunction !=0 ) { fSVKernelFunction = 0; }
199 Log() <<
kDEBUG <<
" successfully(?) reset the method " <<
Endl;
218 SetNormalised(
kTRUE );
223 fInputData =
new std::vector<TMVA::SVEvent*>(0);
224 fSupportVectors =
new std::vector<TMVA::SVEvent*>(0);
232 DeclareOptionRef( fTheKernel =
"RBF",
"Kernel",
"Pick which kernel ( RBF or MultiGauss )");
234 DeclareOptionRef( fGamma = 1.,
"Gamma",
"RBF kernel parameter: Gamma (size of the Kernel)");
236 DeclareOptionRef( fOrder = 3,
"Order",
"Polynomial Kernel parameter: polynomial order");
237 DeclareOptionRef( fTheta = 1.,
"Theta",
"Polynomial Kernel parameter: polynomial theta");
239 DeclareOptionRef( fGammas =
"",
"GammaList",
"MultiGauss parameters" );
242 DeclareOptionRef( fTune =
"All",
"Tune",
"Tune Parameters");
244 DeclareOptionRef( fMultiKernels =
"None",
"KernelList",
"Sum or product of kernels");
245 DeclareOptionRef( fLoss =
"hinge",
"Loss",
"Loss function");
247 DeclareOptionRef( fCost,
"C",
"Cost parameter" );
248 if (DoRegression()) {
253 DeclareOptionRef( fTolerance = 0.01,
"Tol",
"Tolerance parameter" );
254 DeclareOptionRef( fMaxIter = 1000,
"MaxIter",
"Maximum number of training loops" );
264 DeclareOptionRef( fNSubSets = 1,
"NSubSets",
"Number of training subsets" );
265 DeclareOptionRef( fTheKernel =
"Gauss",
"Kernel",
"Uses kernel function");
267 DeclareOptionRef( fDoubleSigmaSquared = 2.,
"Sigma",
"Kernel parameter: sigma");
269 DeclareOptionRef( fOrder = 3,
"Order",
"Polynomial Kernel parameter: polynomial order");
271 DeclareOptionRef( fTheta = 1.,
"Theta",
"Sigmoid Kernel parameter: theta");
272 DeclareOptionRef( fKappa = 1.,
"Kappa",
"Sigmoid Kernel parameter: kappa");
280 if (IgnoreEventsWithNegWeightsInTraining()) {
281 Log() <<
kFATAL <<
"Mechanism to ignore events with negative weights in training not yet available for method: "
282 << GetMethodTypeName()
283 <<
" --> please remove \"IgnoreNegWeightsInTraining\" option from booking string."
293 fIPyMaxIter = fMaxIter;
298 fDataSize =
Data()->GetNEvents();
299 Int_t nSignal =
Data()->GetNEvtSigTrain();
300 Int_t nBackground =
Data()->GetNEvtBkgdTrain();
307 if(nSignal < nBackground){
309 CBkg = CSig*((double)nSignal/nBackground);
313 CSig = CBkg*((double)nSignal/nBackground);
317 for (
Int_t ievnt=0; ievnt<
Data()->GetNEvents(); ievnt++){
318 if (GetEvent(ievnt)->GetWeight() != 0){
319 if(DataInfo().IsSignal(GetEvent(ievnt))){
320 fInputData->push_back(
new SVEvent(GetEvent(ievnt), CSig, DataInfo().IsSignal\
324 fInputData->push_back(
new SVEvent(GetEvent(ievnt), CBkg, DataInfo().IsSignal\
333 if( fTheKernel ==
"RBF"){
336 else if( fTheKernel ==
"MultiGauss" ){
342 if(fmGamma.size()!=0){ GetMGamma(fmGamma); }
344 for(
Int_t ngammas=0; ngammas<fNumVars; ++ngammas){
345 fmGamma.push_back(1.0);
352 else if( fTheKernel ==
"Polynomial" ){
355 else if( fTheKernel ==
"Prod" ){
361 if(fmGamma.size()!=0){ GetMGamma(fmGamma); }
365 else if( fTheKernel ==
"Sum" ){
371 if(fmGamma.size()!=0){ GetMGamma(fmGamma); }
376 Log() <<
kWARNING << fTheKernel <<
" is not a recognised kernel function." <<
Endl;
380 Log()<<
kINFO <<
"Building SVM Working Set...with "<<fInputData->size()<<
" event instances"<<
Endl;
382 fWgSet =
new SVWorkingSet( fInputData, fSVKernelFunction,fTolerance, DoRegression() );
387 Log() <<
kINFO <<
"Sorry, no computing time forecast available for SVM, please wait ..." <<
Endl;
389 if (fInteractive) fWgSet->SetIPythonInteractive(&fExitFromTraining, &fIPyCurrentIter);
391 fWgSet->Train(fMaxIter);
396 fBparm = fWgSet->GetBpar();
397 fSupportVectors = fWgSet->GetSupportVectors();
401 if (!fExitFromTraining) fIPyMaxIter = fIPyCurrentIter;
418 for (std::vector<TMVA::SVEvent*>::iterator veciter=fSupportVectors->begin();
419 veciter!=fSupportVectors->end() ; ++veciter ) {
421 temp[0] = (*veciter)->GetNs();
422 temp[1] = (*veciter)->GetTypeFlag();
423 temp[2] = (*veciter)->GetAlpha();
424 temp[3] = (*veciter)->GetAlpha_p();
425 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++)
426 temp[ivar+4] = (*(*veciter)->GetDataVector())[ivar];
431 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++)
434 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++)
455 std::vector<Float_t>* svector =
new std::vector<Float_t>(GetNvar());
457 if (fMaxVars!=0)
delete fMaxVars;
458 fMaxVars =
new TVectorD( GetNvar() );
459 if (fMinVars!=0)
delete fMinVars;
460 fMinVars =
new TVectorD( GetNvar() );
461 if (fSupportVectors!=0) {
462 for (vector< SVEvent* >::iterator it = fSupportVectors->begin(); it!=fSupportVectors->end(); ++it)
464 delete fSupportVectors;
466 fSupportVectors =
new std::vector<TMVA::SVEvent*>(0);
468 for (
UInt_t ievt = 0; ievt < fNsupv; ievt++) {
472 typeFlag=(int)temp[1];
475 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) (*svector)[ivar]=temp[ivar+4];
477 fSupportVectors->push_back(
new SVEvent(svector,alpha,alpha_p,typeFlag));
481 void* maxminnode = supportvectornode;
482 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++)
485 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++)
487 if (fSVKernelFunction!=0)
delete fSVKernelFunction;
488 if( fTheKernel ==
"RBF" ){
491 else if( fTheKernel ==
"MultiGauss" ){
492 SetMGamma(fGammaList);
495 else if( fTheKernel ==
"Polynomial" ){
498 else if( fTheKernel ==
"Prod" ){
499 SetMGamma(fGammaList);
502 else if( fTheKernel ==
"Sum" ){
503 SetMGamma(fGammaList);
507 Log() <<
kWARNING << fTheKernel <<
" is not a recognised kernel function." <<
Endl;
525 if (fSupportVectors !=0) {
delete fSupportVectors; fSupportVectors = 0;}
526 fSupportVectors =
new std::vector<TMVA::SVEvent*>(0);
534 fSupportVectors->reserve(fNsupv);
540 std::vector<Float_t>* svector =
new std::vector<Float_t>(GetNvar());
542 fMaxVars =
new TVectorD( GetNvar() );
543 fMinVars =
new TVectorD( GetNvar() );
545 for (
UInt_t ievt = 0; ievt < fNsupv; ievt++) {
548 typeFlag = typeTalpha<0?-1:1;
549 alpha = typeTalpha<0?-typeTalpha:typeTalpha;
550 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) istr >> svector->at(ivar);
552 fSupportVectors->push_back(
new SVEvent(svector,alpha,typeFlag,ns));
555 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) istr >> (*fMaxVars)[ivar];
557 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) istr >> (*fMinVars)[ivar];
559 delete fSVKernelFunction;
560 if (fTheKernel ==
"Gauss" ) {
569 Log() <<
kFATAL <<
"Unknown kernel function found in weight file!" <<
Endl;
572 fSVKernelFunction->setCompatibilityParams(k, fOrder, fTheta, fKappa);
594 for (
UInt_t ievt = 0; ievt < fSupportVectors->size() ; ievt++) {
595 myMVA += ( fSupportVectors->at(ievt)->GetAlpha()
596 * fSupportVectors->at(ievt)->GetTypeFlag()
597 * fSVKernelFunction->Evaluate( fSupportVectors->at(ievt), ev ) );
605 NoErrorCalc(err, errUpper);
614 if( fRegressionReturnVal ==
NULL )
615 fRegressionReturnVal =
new std::vector<Float_t>();
616 fRegressionReturnVal->clear();
620 const Event *baseev = GetEvent();
623 for (
UInt_t ievt = 0; ievt < fSupportVectors->size() ; ievt++) {
624 myMVA += ( fSupportVectors->at(ievt)->GetDeltaAlpha()
625 *fSVKernelFunction->Evaluate( fSupportVectors->at(ievt), ev ) );
631 const Event* evT2 = GetTransformationHandler().InverseTransform( evT );
633 fRegressionReturnVal->push_back(evT2->
GetTarget(0));
639 return *fRegressionReturnVal;
647 const int fNsupv = fSupportVectors->size();
648 fout <<
" // not implemented for class: \"" << className <<
"\"" << std::endl;
649 fout <<
" float fBparameter;" << std::endl;
650 fout <<
" int fNOfSuppVec;" << std::endl;
651 fout <<
" static float fAllSuppVectors[][" << fNsupv <<
"];" << std::endl;
652 fout <<
" static float fAlphaTypeCoef[" << fNsupv <<
"];" << std::endl;
654 fout <<
" // Kernel parameter(s) " << std::endl;
655 fout <<
" float fGamma;" << std::endl;
656 fout <<
"};" << std::endl;
657 fout <<
"" << std::endl;
660 fout <<
"inline void " << className <<
"::Initialize() " << std::endl;
661 fout <<
"{" << std::endl;
662 fout <<
" fBparameter = " << fBparm <<
";" << std::endl;
663 fout <<
" fNOfSuppVec = " << fNsupv <<
";" << std::endl;
664 fout <<
" fGamma = " << fGamma <<
";" <<std::endl;
665 fout <<
"}" << std::endl;
669 fout <<
"inline double " << className <<
"::GetMvaValue__(const std::vector<double>& inputValues ) const" << std::endl;
670 fout <<
"{" << std::endl;
671 fout <<
" double mvaval = 0; " << std::endl;
672 fout <<
" double temp = 0; " << std::endl;
674 fout <<
" for (int ievt = 0; ievt < fNOfSuppVec; ievt++ ){" << std::endl;
675 fout <<
" temp = 0;" << std::endl;
676 fout <<
" for ( unsigned int ivar = 0; ivar < GetNvar(); ivar++ ) {" << std::endl;
678 fout <<
" temp += (fAllSuppVectors[ivar][ievt] - inputValues[ivar]) " << std::endl;
679 fout <<
" * (fAllSuppVectors[ivar][ievt] - inputValues[ivar]); " << std::endl;
680 fout <<
" }" << std::endl;
681 fout <<
" mvaval += fAlphaTypeCoef[ievt] * exp( -fGamma * temp ); " << std::endl;
683 fout <<
" }" << std::endl;
684 fout <<
" mvaval -= fBparameter;" << std::endl;
685 fout <<
" return 1./(1. + exp(mvaval));" << std::endl;
686 fout <<
"}" << std::endl;
687 fout <<
"// Clean up" << std::endl;
688 fout <<
"inline void " << className <<
"::Clear() " << std::endl;
689 fout <<
"{" << std::endl;
690 fout <<
" // nothing to clear " << std::endl;
691 fout <<
"}" << std::endl;
692 fout <<
"" << std::endl;
695 fout <<
"float " << className <<
"::fAlphaTypeCoef[] =" << std::endl;
697 for (
Int_t isv = 0; isv < fNsupv; isv++) {
698 fout << fSupportVectors->at(isv)->GetDeltaAlpha() * fSupportVectors->at(isv)->GetTypeFlag();
699 if (isv < fNsupv-1) fout <<
", ";
701 fout <<
" };" << std::endl << std::endl;
703 fout <<
"float " << className <<
"::fAllSuppVectors[][" << fNsupv <<
"] =" << std::endl;
705 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) {
708 for (
Int_t isv = 0; isv < fNsupv; isv++){
709 fout << fSupportVectors->at(isv)->GetDataVector()->at(ivar);
710 if (isv < fNsupv-1) fout <<
", ";
713 if (ivar < GetNvar()-1) fout <<
", " << std::endl;
714 else fout << std::endl;
716 fout <<
"};" << std::endl<< std::endl;
730 Log() <<
"The Support Vector Machine (SVM) builds a hyperplance separating" <<
Endl;
731 Log() <<
"signal and background events (vectors) using the minimal subset of " <<
Endl;
732 Log() <<
"all vectors used for training (support vectors). The extension to" <<
Endl;
733 Log() <<
"the non-linear case is performed by mapping input vectors into a " <<
Endl;
734 Log() <<
"higher-dimensional feature space in which linear separation is " <<
Endl;
735 Log() <<
"possible. The use of the kernel functions thereby eliminates the " <<
Endl;
736 Log() <<
"explicit transformation to the feature space. The implemented SVM " <<
Endl;
737 Log() <<
"algorithm performs the classification tasks using linear, polynomial, " <<
Endl;
738 Log() <<
"Gaussian and sigmoidal kernel functions. The Gaussian kernel allows " <<
Endl;
739 Log() <<
"to apply any discriminant shape in the input space." <<
Endl;
743 Log() <<
"SVM is a general purpose non-linear classification method, which " <<
Endl;
744 Log() <<
"does not require data preprocessing like decorrelation or Principal " <<
Endl;
745 Log() <<
"Component Analysis. It generalises quite well and can handle analyses " <<
Endl;
746 Log() <<
"with large numbers of input variables." <<
Endl;
750 Log() <<
"Optimal performance requires primarily a proper choice of the kernel " <<
Endl;
751 Log() <<
"parameters (the width \"Sigma\" in case of Gaussian kernel) and the" <<
Endl;
752 Log() <<
"cost parameter \"C\". The user must optimise them empirically by running" <<
Endl;
753 Log() <<
"SVM several times with different parameter sets. The time needed for " <<
Endl;
754 Log() <<
"each evaluation scales like the square of the number of training " <<
Endl;
755 Log() <<
"events so that a coarse preliminary tuning should be performed on " <<
Endl;
756 Log() <<
"reduced data sets." <<
Endl;
769 std::map< TString,std::vector<Double_t> > optVars;
772 optVars= GetTuningOptions();
774 std::map< TString,std::vector<Double_t> >::iterator iter;
776 std::map<TString,TMVA::Interval*> tuneParameters;
777 std::map<TString,Double_t> tunedParameters;
780 Log() <<
kINFO <<
"Using the " << fTheKernel <<
" kernel." <<
Endl;
782 if( fTheKernel ==
"RBF" ){
784 tuneParameters.insert(std::pair<TString,Interval*>(
"Gamma",
new Interval(0.01,1.,100)));
785 tuneParameters.insert(std::pair<TString,Interval*>(
"C",
new Interval(0.01,1.,100)));
788 for(iter=optVars.begin(); iter!=optVars.end(); iter++){
789 if( iter->first ==
"Gamma" || iter->first ==
"C"){
790 tuneParameters.insert(std::pair<TString,Interval*>(iter->first,
new Interval(iter->second.at(0),iter->second.at(1),iter->second.at(2))));
793 Log() <<
kWARNING << iter->first <<
" is not a recognised tuneable parameter." <<
Endl;
799 else if( fTheKernel ==
"Polynomial" ){
801 tuneParameters.insert(std::pair<TString,Interval*>(
"Order",
new Interval(1,10,10)));
802 tuneParameters.insert(std::pair<TString,Interval*>(
"Theta",
new Interval(0.01,1.,100)));
803 tuneParameters.insert(std::pair<TString,Interval*>(
"C",
new Interval(0.01,1.,100)));
806 for(iter=optVars.begin(); iter!=optVars.end(); iter++){
807 if( iter->first ==
"Theta" || iter->first ==
"C"){
808 tuneParameters.insert(std::pair<TString,Interval*>(iter->first,
new Interval(iter->second.at(0),iter->second.at(1),iter->second.at(2))));
810 else if( iter->first ==
"Order"){
811 tuneParameters.insert(std::pair<TString,Interval*>(iter->first,
new Interval(iter->second.at(0),iter->second.at(1),iter->second.at(2))));
814 Log() <<
kWARNING << iter->first <<
" is not a recognised tuneable parameter." <<
Endl;
820 else if( fTheKernel ==
"MultiGauss" ){
822 for(
int i=0; i<fNumVars; i++){
824 s << fVarNames.at(i);
825 string str =
"Gamma_" + s.str();
826 tuneParameters.insert(std::pair<TString,Interval*>(str,
new Interval(0.01,1.,100)));
828 tuneParameters.insert(std::pair<TString,Interval*>(
"C",
new Interval(0.01,1.,100)));
830 for(iter=optVars.begin(); iter!=optVars.end(); iter++){
831 if( iter->first ==
"GammaList"){
832 for(
int j=0; j<fNumVars; j++){
834 s << fVarNames.at(j);
835 string str =
"Gamma_" + s.str();
836 tuneParameters.insert(std::pair<TString,Interval*>(str,
new Interval(iter->second.at(0),iter->second.at(1),iter->second.at(2))));
839 else if( iter->first ==
"C"){
840 tuneParameters.insert(std::pair<TString,Interval*>(iter->first,
new Interval(iter->second.at(0),iter->second.at(1),iter->second.at(2))));
843 Log() <<
kWARNING << iter->first <<
" is not a recognised tuneable parameter." <<
Endl;
849 else if( fTheKernel ==
"Prod" ){
850 std::stringstream tempstring(fMultiKernels);
852 while (std::getline(tempstring,value,
'*')){
854 tuneParameters.insert(std::pair<TString,Interval*>(
"Gamma",
new Interval(0.01,1.,100)));
856 else if(value ==
"MultiGauss"){
857 for(
int i=0; i<fNumVars; i++){
859 s << fVarNames.at(i);
860 string str =
"Gamma_" + s.str();
861 tuneParameters.insert(std::pair<TString,Interval*>(str,
new Interval(0.01,1.,100)));
864 else if(value ==
"Polynomial"){
865 tuneParameters.insert(std::pair<TString,Interval*>(
"Order",
new Interval(1,10,10)));
866 tuneParameters.insert(std::pair<TString,Interval*>(
"Theta",
new Interval(0.0,1.0,101)));
869 Log() <<
kWARNING << value <<
" is not a recognised kernel function." <<
Endl;
873 tuneParameters.insert(std::pair<TString,Interval*>(
"C",
new Interval(0.01,1.,100)));
875 else if( fTheKernel ==
"Sum" ){
876 std::stringstream tempstring(fMultiKernels);
878 while (std::getline(tempstring,value,
'+')){
880 tuneParameters.insert(std::pair<TString,Interval*>(
"Gamma",
new Interval(0.01,1.,100)));
882 else if(value ==
"MultiGauss"){
883 for(
int i=0; i<fNumVars; i++){
885 s << fVarNames.at(i);
886 string str =
"Gamma_" + s.str();
887 tuneParameters.insert(std::pair<TString,Interval*>(str,
new Interval(0.01,1.,100)));
890 else if(value ==
"Polynomial"){
891 tuneParameters.insert(std::pair<TString,Interval*>(
"Order",
new Interval(1,10,10)));
892 tuneParameters.insert(std::pair<TString,Interval*>(
"Theta",
new Interval(0.0,1.0,101)));
895 Log() <<
kWARNING << value <<
" is not a recognised kernel function." <<
Endl;
899 tuneParameters.insert(std::pair<TString,Interval*>(
"C",
new Interval(0.01,1.,100)));
902 Log() <<
kWARNING << fTheKernel <<
" is not a recognised kernel function." <<
Endl;
905 Log() <<
kINFO <<
" the following SVM parameters will be tuned on the respective *grid*\n" <<
Endl;
906 std::map<TString,TMVA::Interval*>::iterator it;
907 for(it=tuneParameters.begin(); it!=tuneParameters.end(); it++){
909 std::ostringstream oss;
910 (it->second)->
Print(oss);
915 tunedParameters=optimize.
optimize();
917 return tunedParameters;
925 std::map<TString,Double_t>::iterator it;
926 if( fTheKernel ==
"RBF" ){
927 for(it=tuneParameters.begin(); it!=tuneParameters.end(); it++){
929 if (it->first ==
"Gamma"){
930 SetGamma (it->second);
932 else if(it->first ==
"C"){
933 SetCost (it->second);
936 Log() <<
kFATAL <<
" SetParameter for " << it->first <<
" not implemented " <<
Endl;
940 else if( fTheKernel ==
"MultiGauss" ){
942 for(
int i=0; i<fNumVars; i++){
944 s << fVarNames.at(i);
945 string str =
"Gamma_" + s.str();
946 Log() <<
kWARNING << tuneParameters.find(str)->first <<
" = " << tuneParameters.find(str)->second <<
Endl;
947 fmGamma.push_back(tuneParameters.find(str)->second);
949 for(it=tuneParameters.begin(); it!=tuneParameters.end(); it++){
950 if (it->first ==
"C"){
957 else if( fTheKernel ==
"Polynomial" ){
958 for(it=tuneParameters.begin(); it!=tuneParameters.end(); it++){
960 if (it->first ==
"Order"){
961 SetOrder(it->second);
963 else if (it->first ==
"Theta"){
964 SetTheta(it->second);
966 else if(it->first ==
"C"){ SetCost (it->second);
968 else if(it->first ==
"Mult"){
972 Log() <<
kFATAL <<
" SetParameter for " << it->first <<
" not implemented " <<
Endl;
976 else if( fTheKernel ==
"Prod" || fTheKernel ==
"Sum"){
978 for(it=tuneParameters.begin(); it!=tuneParameters.end(); it++){
979 bool foundParam =
false;
981 for(
int i=0; i<fNumVars; i++){
983 s << fVarNames.at(i);
984 string str =
"Gamma_" + s.str();
985 if(it->first == str){
986 fmGamma.push_back(it->second);
990 if (it->first ==
"Gamma"){
991 SetGamma (it->second);
994 else if (it->first ==
"Order"){
995 SetOrder (it->second);
998 else if (it->first ==
"Theta"){
999 SetTheta (it->second);
1002 else if (it->first ==
"C"){ SetCost (it->second);
1003 SetCost (it->second);
1008 Log() <<
kFATAL <<
" SetParameter for " << it->first <<
" not implemented " <<
Endl;
1014 Log() <<
kWARNING << fTheKernel <<
" is not a recognised kernel function." <<
Endl;
1025 std::stringstream tempstring(mg);
1027 while (tempstring >> value){
1028 fmGamma.push_back(value);
1030 if (tempstring.peek() ==
','){
1031 tempstring.ignore();
1039 std::ostringstream tempstring;
1040 for(
UInt_t i = 0; i<gammas.size(); ++i){
1041 tempstring << gammas.at(i);
1042 if(i!=(gammas.size()-1)){
1046 fGammaList= tempstring.str();
1058 std::vector<TMVA::SVKernelFunction::EKernelType> kernelsList;
1059 std::stringstream tempstring(multiKernels);
1062 while (std::getline(tempstring,value,
'*')){
1064 else if(value ==
"MultiGauss"){
1072 Log() <<
kWARNING << value <<
" is not a recognised kernel function." <<
Endl;
1077 else if(kernel==
"Sum"){
1078 while (std::getline(tempstring,value,
'+')){
1080 else if(value ==
"MultiGauss"){
1088 Log() <<
kWARNING << value <<
" is not a recognised kernel function." <<
Endl;
1094 Log() <<
kWARNING <<
"Unable to split MultiKernels. Delimiters */+ required." <<
Endl;
1110 std::map< TString,std::vector<Double_t> > optVars;
1111 std::stringstream tempstring(fTune);
1113 while (std::getline(tempstring,value,
',')){
1114 unsigned first = value.find(
'[')+1;
1115 unsigned last = value.find_last_of(
']');
1116 std::string optParam = value.substr(0,first-1);
1117 std::stringstream strNew (value.substr(first,last-first));
1119 std::vector<Double_t> tempVec;
1121 while (strNew >> optInterval){
1122 tempVec.push_back(optInterval);
1123 if (strNew.peek() ==
';'){
1128 if(i != 3 && i == tempVec.size()){
1129 if(optParam ==
"C" || optParam ==
"Gamma" || optParam ==
"GammaList" || optParam ==
"Theta"){
1132 tempVec.push_back(0.01);
1134 tempVec.push_back(1.);
1136 tempVec.push_back(100);
1139 else if(optParam ==
"Order"){
1142 tempVec.push_back(1);
1144 tempVec.push_back(10);
1146 tempVec.push_back(10);
1150 Log() <<
kWARNING << optParam <<
" is not a recognised tuneable parameter." <<
Endl;
1154 optVars.insert(std::pair<
TString,std::vector<Double_t> >(optParam,tempVec));
1170 for (
Long64_t ievt=0; ievt<GetNEvents(); ievt++) {
1171 const Event* ev = GetEvent(ievt);
1174 if(DataInfo().IsSignal(ev)){
1175 if(lossFunction ==
"hinge"){
1178 else if(lossFunction ==
"exp"){
1181 else if(lossFunction ==
"binomial"){
1185 Log() <<
kWARNING << lossFunction <<
" is not a recognised loss function." <<
Endl;
1190 if(lossFunction ==
"hinge"){
1193 else if(lossFunction ==
"exp"){
1196 else if(lossFunction ==
"binomial"){
1200 Log() <<
kWARNING << lossFunction <<
" is not a recognised loss function." <<
Endl;
void Train(void)
Train SVM.
std::vector< TMVA::SVKernelFunction::EKernelType > MakeKernelList(std::string multiKernels, TString kernel)
MakeKernelList Function providing string manipulation for product or sum of kernels functions to take...
std::string GetName(const std::string &scope_name)
void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility
MsgLogger & Endl(MsgLogger &ml)
std::map< TString, std::vector< Double_t > > GetTuningOptions()
GetTuningOptions Function to allow for ranges and number of steps (for scan) when optimising kernel f...
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
void MakeClassSpecific(std::ostream &, const TString &) const
write specific classifier response
void ProcessOptions()
option post processing (if necessary)
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
TString GetElapsedTime(Bool_t Scientific=kTRUE)
std::vector< TString > fVarNames
virtual void SetTuneParameters(std::map< TString, Double_t > tuneParameters)
Set the tuning parameters according to the arguement.
void GetMGamma(const std::vector< float > &gammas)
Produces GammaList string for multigaussian kernel to be written to xml file.
std::vector< std::vector< double > > Data
void DeclareOptions()
declare options available for this method
TVectorT< Double_t > TVectorD
void AddWeightsXMLTo(void *parent) const
write configuration to xml file
std::string GetMethodName(TCppMethod_t)
void ReadWeightsFromXML(void *wghtnode)
void ReadWeightsFromStream(std::istream &istr)
MethodSVM(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="")
standard constructor
std::map< TString, Double_t > optimize()
void Init(void)
default initialisation
void SetTarget(UInt_t itgt, Float_t value)
set the target value (dimension itgt) to value
virtual std::map< TString, Double_t > OptimizeTuningParameters(TString fomType="ROCIntegral", TString fitType="Minuit")
Optimize Tuning Parameters This is used to optimise the kernel function parameters and cost...
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
SVM can handle classification with 2 classes and regression with one regression-target.
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
returns MVA value for given event
void Print(std::ostream &os, const OptionType &opt)
Double_t getLoss(TString lossFunction)
getLoss Calculates loss for testing dataset.
Float_t GetTarget(UInt_t itgt) const
#define REGISTER_METHOD(CLASS)
for example
virtual ~MethodSVM(void)
destructor
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
void WriteWeightsToStream(TFile &fout) const
TODO write IT write training sample (TTree) to file.
void SetMGamma(std::string &mg)
Takes as input a string of values for multigaussian gammas and splits it, filling the gamma vector re...
std::vector< VariableInfo > & GetVariableInfos()
void GetHelpMessage() const
get help message text
const std::vector< Float_t > & GetRegressionValues()