Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
tmva004_RStandardScaler.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ml
3/// \notebook -nodraw
4/// This tutorial illustrates the usage of the standard scaler as preprocessing
5/// method.
6///
7/// \macro_code
8/// \macro_output
9///
10/// \date July 2019
11/// \author Stefan Wunsch
12
13using namespace TMVA::Experimental;
14
15void tmva004_RStandardScaler()
16{
17 // Load data used to fit the parameters
18
19
20 std::string inputFile = std::string(gROOT->GetTutorialDir()) + "/machine_learning/data/tmva_class_example.root";
21 ROOT::RDataFrame df("TreeS", inputFile);
22 auto x = AsTensor<float>(df);
23
24 // Create standard scaler and fit to data
26 scaler.Fit(x);
27
28 // Compute transformation
29 auto y = scaler.Compute(x);
30
31 // Plot first variable scaled and unscaled
32 TH1F h1("h1", ";x_{4};N_{Events}", 20, -4, 4);
33 TH1F h2("h2", ";x_{4};N_{Events}", 20, -4, 4);
34 for (std::size_t i = 0; i < x.GetShape()[0]; i++) {
35 h1.Fill(x(i, 3));
36 h2.Fill(y(i, 3));
37 }
38 h1.SetLineWidth(2);
39 h1.SetLineColor(kRed);
40 h2.SetLineWidth(2);
41 h2.SetLineColor(kBlue);
42
43 gStyle->SetOptStat(0);
44 auto c = new TCanvas("", "", 800, 800);
45 h2.Draw("HIST");
46 h1.Draw("HIST SAME");
47
48 TLegend legend(0.7, 0.7, 0.89, 0.89);
49 legend.SetBorderSize(0);
50 legend.AddEntry("h1", "Unscaled", "l");
51 legend.AddEntry("h2", "Scaled", "l");
52 legend.Draw();
53
54 c->DrawClone();
55}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
#define gROOT
Definition TROOT.h:417
externTStyle * gStyle
Definition TStyle.h:442
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
void Fit(const RTensor< T > &x)
std::vector< T > Compute(const std::vector< T > &x)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5
RTensor< T > AsTensor(U &dataframe, std::vector< std::string > columns={}, MemoryLayout layout=MemoryLayout::RowMajor)
Convert the content of an RDataFrame to an RTensor.