philsupertramp/game-math
Loading...
Searching...
No Matches
Scaler.h
Go to the documentation of this file.
1#pragma once
2#include "../../Matrix.h"
3#include "../../statistics/Probability.h"
4
5
33#include "../../Matrix.h"
34#include "../../numerics/utils.h"
35#include "../Predictor.h"
36
44{
45private:
46 bool with_std = true;
47 bool with_means = true;
48
49public:
52
53public:
54 StandardScaler(bool withMeans = true, bool withStd = true)
55 : Transformer()
56 , with_std(withStd)
57 , with_means(withMeans){};
58
65 void fit(const Matrix<double>& X, [[maybe_unused]] const Matrix<double>& y) override {
66 means = with_means ? mean(X, 0) : zeros(1, X.columns());
67 std_deviations = with_std ? sd(X, 0) : ones(1, X.columns());
68 }
69
79 auto diff = in - means;
80 return diff / std_deviations;
81 }
82};
83
85{
86private:
87 double min_val;
88 double max_val;
89
90public:
91public:
92 MinMaxScaler(double range_min_val = 0., double range_max_val = 1.0)
93 : Transformer()
94 , min_val(range_min_val)
95 , max_val(range_max_val) { }
96
97 void fit(const Matrix<double>& X, [[maybe_unused]] const Matrix<double>& y) override { }
98
100 auto localDiff = max(in, 0) - min(in, 0);
101 auto shiftedIn = in - min(in, 0);
102 auto scaledDiff = shiftedIn / localDiff;
103 scaledDiff = scaledDiff * (max_val - min_val);
104 scaledDiff = scaledDiff + Matrix<double>(min_val, scaledDiff.rows(), scaledDiff.columns());
105 return scaledDiff;
106 }
107};
108
Matrix< double > sd(const Matrix< double > &x, int axis=0)
Definition: Matrix.h:42
size_t columns() const
Definition: Matrix.h:198
Definition: Scaler.h:85
double min_val
Definition: Scaler.h:87
MinMaxScaler(double range_min_val=0., double range_max_val=1.0)
Definition: Scaler.h:92
double max_val
Definition: Scaler.h:88
void fit(const Matrix< double > &X, const Matrix< double > &y) override
Definition: Scaler.h:97
Matrix< double > transform(const Matrix< double > &in) override
Definition: Scaler.h:99
Definition: Scaler.h:44
Matrix< double > std_deviations
Definition: Scaler.h:51
Matrix< double > means
Definition: Scaler.h:50
bool with_std
Definition: Scaler.h:46
bool with_means
Definition: Scaler.h:47
StandardScaler(bool withMeans=true, bool withStd=true)
Definition: Scaler.h:54
void fit(const Matrix< double > &X, const Matrix< double > &y) override
Definition: Scaler.h:65
Matrix< double > transform(const Matrix< double > &in) override
Definition: Scaler.h:78
Definition: Predictor.h:30
Matrix< T > mean(const Matrix< T > &mat, int axis=-1)
Definition: matrix_utils.h:401
T min(const Matrix< T > &mat)
Definition: matrix_utils.h:324
T max(const Matrix< T > &mat)
Definition: matrix_utils.h:292
Matrix< double > ones(size_t rows, size_t columns=1, size_t elements=1)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)