Tmva 0 0 4_ R Standard Scaler

This tutorial illustrates the usage of the standard scaler as preprocessing method.

Author: Stefan Wunsch
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Saturday, September 18, 2021 at 09:31 AM.

In [1]:
using namespace TMVA::Experimental;

Load data used to fit the parameters

In [2]:
ROOT::RDataFrame df("TreeS", "http://root.cern/files/tmva_class_example.root");
auto x = AsTensor<float>(df);

Create standard scaler and fit to data

In [3]:
RStandardScaler<float> scaler;
scaler.Fit(x);

Compute transformation

In [4]:
auto y = scaler.Compute(x);

Plot first variable scaled and unscaled

In [5]:
TH1F h1("h1", ";x_{4};N_{Events}", 20, -4, 4);
TH1F h2("h2", ";x_{4};N_{Events}", 20, -4, 4);
for (std::size_t i = 0; i < x.GetShape()[0]; i++) {
   h1.Fill(x(i, 3));
   h2.Fill(y(i, 3));
}
h1.SetLineWidth(2);
h1.SetLineColor(kRed);
h2.SetLineWidth(2);
h2.SetLineColor(kBlue);

gStyle->SetOptStat(0);
auto c = new TCanvas("", "", 800, 800);
h2.Draw("HIST");
h1.Draw("HIST SAME");

TLegend legend(0.7, 0.7, 0.89, 0.89);
legend.SetBorderSize(0);
legend.AddEntry("h1", "Unscaled", "l");
legend.AddEntry("h2", "Scaled", "l");
legend.Draw();

c->DrawClone();