1#ifndef TMVA_RSTANDARDSCALER
2#define TMVA_RSTANDARDSCALER
13namespace Experimental {
25 std::vector<T>
Compute(
const std::vector<T>&
x);
29 void Save(std::string_view title, std::string_view filename);
36 file->GetObject(title.data(), obj);
53 const auto shape =
x.GetShape();
54 if (shape.size() != 2)
55 throw std::runtime_error(
"Can only fit to input tensor of rank 2.");
57 fMeans.resize(shape[1]);
59 fStds.resize(shape[1]);
62 for (std::size_t i = 0; i < shape[0]; i++) {
63 for (std::size_t j = 0; j < shape[1]; j++) {
67 for (std::size_t i = 0; i < shape[1]; i++) {
68 fMeans[i] /= shape[0];
72 for (std::size_t i = 0; i < shape[0]; i++) {
73 for (std::size_t j = 0; j < shape[1]; j++) {
74 fStds[j] += (
x(i, j) - fMeans[j]) * (
x(i, j) - fMeans[j]);
77 for (std::size_t i = 0; i < shape[1]; i++) {
78 fStds[i] = std::sqrt(fStds[i] / (shape[0] - 1));
84 const auto size =
x.size();
85 if (
size != fMeans.size())
86 throw std::runtime_error(
"Size of input vector is not equal to number of fitted variables.");
88 std::vector<T>
y(
size);
89 for (std::size_t i = 0; i <
size; i++) {
90 y[i] = (
x[i] - fMeans[i]) / fStds[i];
99 if (shape.size() != 2)
100 throw std::runtime_error(
"Can only compute output for input tensor of rank 2.");
101 if (shape[1] != fMeans.size())
102 throw std::runtime_error(
"Second dimension of input tensor is not equal to number of fitted variables.");
105 for (std::size_t i = 0; i < shape[0]; i++) {
106 for (std::size_t j = 0; j < shape[1]; j++) {
107 y(i, j) = (
x(i, j) - fMeans[j]) / fStds[j];
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
void Save(std::string_view title, std::string_view filename)
std::vector< T > GetMeans() const
void Fit(const RTensor< T > &x)
std::vector< T > GetStds() const
RStandardScaler()=default
std::vector< T > Compute(const std::vector< T > &x)
RTensor is a container with contiguous memory and shape information.
const Shape_t & GetShape() const
create variable transformations