Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
tmva004_RStandardScaler.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tmva
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 
13 using namespace TMVA::Experimental;
14 
15 void tmva004_RStandardScaler()
16 {
17  // Load data used to fit the parameters
18  ROOT::RDataFrame df("TreeS", "http://root.cern/files/tmva_class_example.root");
19  auto x = AsTensor<float>(df);
20 
21  // Create standard scaler and fit to data
22  RStandardScaler<float> scaler;
23  scaler.Fit(x);
24 
25  // Compute transformation
26  auto y = scaler.Compute(x);
27 
28  // Plot first variable scaled and unscaled
29  TH1F h1("h1", ";x_{4};N_{Events}", 20, -4, 4);
30  TH1F h2("h2", ";x_{4};N_{Events}", 20, -4, 4);
31  for (std::size_t i = 0; i < x.GetShape()[0]; i++) {
32  h1.Fill(x(i, 3));
33  h2.Fill(y(i, 3));
34  }
35  h1.SetLineWidth(2);
36  h1.SetLineColor(kRed);
37  h2.SetLineWidth(2);
38  h2.SetLineColor(kBlue);
39 
40  gStyle->SetOptStat(0);
41  auto c = new TCanvas("", "", 800, 800);
42  h2.Draw("HIST");
43  h1.Draw("HIST SAME");
44 
45  TLegend legend(0.7, 0.7, 0.89, 0.89);
46  legend.SetBorderSize(0);
47  legend.AddEntry("h1", "Unscaled", "l");
48  legend.AddEntry("h2", "Scaled", "l");
49  legend.Draw();
50 
51  c->DrawClone();
52 }