Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- import sys, time
- from ROOT import TFile, TTree, TCut, TMVA
- ## Files
- inputFile = TFile("tmva_reg_example.root")
- outputFile = TFile("out.root", 'RECREATE')
- ## Create instance of TMVA factory
- factory = TMVA.Factory("TMVARegression", outputFile, "!V:!Silent:Color:DrawProgressBar")
- factory.SetVerbose(True)
- ## Define the input variables that shall be used for the MVA training
- ## note that you may also use variable expressions, such as: "3*var1/var2*abs(var3)"
- ## [all types of expressions that can also be parsed by TTree::Draw( "expression" )]
- factory.AddVariable("var1", "Variable 1", "units", 'F')
- factory.AddVariable("var2", "Variable 2", "units", 'F')
- ## You can add so-called "Spectator variables", which are not used in the MVA training,
- ## but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the
- ## input variables, the response values of all trained MVAs, and the spectator variables
- #factory.AddSpectator("spec1:=var1*2", "Spectator 1", "units", 'F')
- #factory.AddSpectator("spec2:=var1*3", "Spectator 2", "units", 'F')
- ## Add the variable carrying the regression target
- factory.AddTarget("fvalue")
- ## For MLP, can declare additional targets for multi-dimensional regression, e.g.
- # factory.AddTarget("fvalue2")
- ## Set up regression tree
- # for k in inputFile.GetListOfKeys():
- # print k
- regTree = inputFile.Get("TreeR")
- regWeight = 1.0
- factory.AddRegressionTree(regTree, regWeight);
- ## This would set individual event weights (the variables defined in the
- ## expression need to exist in the original TTree)
- factory.SetWeightExpression("var1", "Regression")
- ## Prepare for regression with no cut
- ## Cut *could* be e.g. TCut("abs(var1)<0.5 && abs(var2-0.5)<1")
- ## Tell the factory to use all remaining events in the trees after training for testing:
- factory.PrepareTrainingAndTestTree(TCut(""), "nTrain_Regression=0:nTest_Regression=0:SplitMode=Random:NormMode=NumEvents:!V")
- ## If no numbers of events are given, half are used for training, and the other half for testing:
- # factory.PrepareTrainingAndTestTree(mycut, "SplitMode=random:!V")
- ## Set up machine learning method
- #config = "!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=20000:HiddenLayers=N+20"
- config = "!H:!V:VarTransform=Norm:NeuronType=tanh:NCycles=20000:HiddenLayers=N+20,N+20"
- config += ":TestRate=6:TrainingMethod=BFGS:Sampling=0.3:SamplingEpoch=0.8"
- config += ":ConvergenceImprove=1e-6:ConvergenceTests=15:!UseRegulator"
- factory.BookMethod(TMVA.Types.kMLP, "MLP", config)
- # TODO: add CalculateError
- #factory.BookMethod( TMVA.Types.kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" )
- #factory.BookMethod( TMVA.Types.kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" ) # BFGS training with bayesian regulators
- #
- #factory.BookMethod(TMVA.Types.kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm")
- #factory.BookMethod(TMVA.Types.kBDT, "BDT", "!H:!V:NTrees=100:nEventsMin=5:BoostType=AdaBoostR2:SeparationType=RegressionVariance:nCuts=20:PruneMethod=CostComplexity:PruneStrength=30")
- #factory.BookMethod( TMVA.Types.kBDT, "BDTG", "!H:!V:NTrees=2000::BoostType=Grad:Shrinkage=0.1:UseBaggedGrad:GradBaggingFraction=0.5:nCuts=20:MaxDepth=3:NNodesMax=15")
- ## Train MVAs using the set of training events
- factory.TrainAllMethods()
- ## Evaluate all MVAs using the set of test events
- factory.TestAllMethods()
- ## Evaluate and compare performance of all configured MVAs
- factory.EvaluateAllMethods()
- ## Save the output
- outputFile.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement