v2.0.0
Loading...
Searching...
No Matches
ml_scaler.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
39#include "ml_scaler.h"
40
41//=============================================================================================================
42// STL INCLUDES
43//=============================================================================================================
44
45#include <stdexcept>
46#include <cmath>
47
48//=============================================================================================================
49// USED NAMESPACES
50//=============================================================================================================
51
52using namespace MLLIB;
53using namespace Eigen;
54
55//=============================================================================================================
56// DEFINE MEMBER METHODS
57//=============================================================================================================
58
60: m_type(type)
61{
62}
63
64//=============================================================================================================
65
66void MlScaler::fit(const MlTensor& data)
67{
68 auto X = data.matrix();
69
70 if (m_type == StandardScaler) {
71 m_mean = X.colwise().mean();
72 VectorXf variance = ((X.rowwise() - m_mean.transpose()).array().square().colwise().sum() /
73 static_cast<float>(X.rows())).matrix().transpose();
74 m_std = variance.array().sqrt().max(1e-10f).matrix();
75 } else {
76 m_min = X.colwise().minCoeff();
77 VectorXf maxVals = X.colwise().maxCoeff();
78 m_range = (maxVals - m_min).array().max(1e-10f).matrix();
79 }
80
81 m_fitted = true;
82}
83
84//=============================================================================================================
85
87{
88 if (!m_fitted) {
89 throw std::runtime_error("MlScaler::transform – scaler has not been fitted.");
90 }
91
92 auto X = data.matrix();
93
94 if (m_type == StandardScaler) {
95 MatrixXf result = ((X.rowwise() - m_mean.transpose()).array().rowwise() / m_std.transpose().array()).matrix();
96 return MlTensor(result);
97 } else {
98 MatrixXf result = ((X.rowwise() - m_min.transpose()).array().rowwise() / m_range.transpose().array()).matrix();
99 return MlTensor(result);
100 }
101}
102
103//=============================================================================================================
104
106{
107 fit(data);
108 return transform(data);
109}
110
111//=============================================================================================================
112
114{
115 if (!m_fitted) {
116 throw std::runtime_error("MlScaler::inverseTransform – scaler has not been fitted.");
117 }
118
119 auto X = data.matrix();
120
121 if (m_type == StandardScaler) {
122 MatrixXf result = ((X.array().rowwise() * m_std.transpose().array()).rowwise() + m_mean.transpose().array()).matrix();
123 return MlTensor(result);
124 } else {
125 MatrixXf result = ((X.array().rowwise() * m_range.transpose().array()).rowwise() + m_min.transpose().array()).matrix();
126 return MlTensor(result);
127 }
128}
MlScaler class declaration.
constexpr int X
Machine learning (models, pipelines, ONNX Runtime integration).
MlTensor transform(const MlTensor &data) const
Definition ml_scaler.cpp:86
MlScaler(ScalerType type=StandardScaler)
Definition ml_scaler.cpp:59
void fit(const MlTensor &data)
Definition ml_scaler.cpp:66
MlTensor fitTransform(const MlTensor &data)
MlTensor inverseTransform(const MlTensor &data) const
N-dimensional tensor with contiguous row-major (C-order) float32 storage.
Definition ml_tensor.h:79
RowMajorMatrixMap matrix()