dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
PCA.h
Go to the documentation of this file.
1 
11 #ifndef __PCA_H__
12 #define __PCA_H__
13 
14 #include <vector>
15 
16 #include "NumCpp.hpp"
17 
18 namespace PCA {
19 
27 nc::NdArray<float> componentsT = {
28  {7.375311811592301370e-02, -3.178655266058625917e-01},
29  {4.093087483174921992e-01, 4.955981249916805442e-01},
30  {3.346693276473038936e-01, -6.128877541751576175e-01},
31  {5.953059210206299579e-01, 1.328377255939875989e-01},
32  {-5.162302414308733711e-02, -4.926638878583582404e-01},
33  {5.983052657524839946e-01, -1.317157490269994513e-01},
34 };
35 
43 nc::NdArray<float> mean = {
44  -1.302661682226850347e-16, 5.921189464667501929e-18,
45  -2.107943449421630714e-15, 9.577523959099684045e-16,
46  5.329070518200751197e-17, -3.907985046680551042e-16,
47 };
54 nc::NdArray<float> scaler_mean = {
55  1.079515125000000080e+04, 2.652374999999999972e+01,
56  2.904959681383768952e+02, 5.040460461552937659e+02,
57  1.784837500024586854e+00, 9.787104166666666742e+02,
58 };
59 
66 nc::NdArray<float> scaler_scale = {
67  6.728206620826987091e+03, 2.899059219708179924e+01,
68  1.302756303387655201e+01, 2.544038827736384079e+01,
69  1.753588622119440776e+00, 1.909781553371280793e+01,
70 };
71 
78 nc::NdArray<float> scale(const nc::NdArray<float>& sample) {
79  auto result = sample - scaler_mean;
80  result /= scaler_scale;
81  return result;
82 }
83 
93 nc::NdArray<float> scale(const nc::NdArray<float>& samples,
94  nc::NdArray<float>& extMean,
95  nc::NdArray<float>& extScale) {
96  auto result = samples - extMean;
97  result /= extScale;
98  return result;
99 }
100 
107 nc::NdArray<float> transform(const nc::NdArray<float>& sample) {
108  auto transformed = sample - mean;
109  transformed = nc::dot<float>(transformed, componentsT);
110  return transformed;
111 }
112 
121 nc::NdArray<float> transform(const nc::NdArray<float>& samples,
122  const nc::NdArray<float>& extMean) {
123  auto transformed = samples - extMean;
124  transformed = nc::dot<float>(transformed, componentsT);
125  return transformed;
126 }
127 
134 nc::NdArray<float> PCA(const std::vector<float>& sample) {
135  nc::NdArray<float> sampleArray =
136  nc::fromiter<float>(sample.begin(), sample.end());
137  nc::NdArray<float> scaled = scale(sampleArray);
138  return transform(scaled);
139 }
140 
147 nc::NdArray<float> PCA(const nc::NdArray<float>& sample) {
148  nc::NdArray<float> scaled = scale(sample);
149  return transform(scaled);
150 }
151 
158 nc::NdArray<float> PCABatch(const nc::NdArray<float>& samples) {
159  auto toInsert = samples.numRows() - scaler_mean.numRows();
160  auto extScalerMean = scaler_mean.copy();
161  auto extScalerScale = scaler_scale.copy();
162  auto extPCAMean = mean.copy();
163  // Broadcasting the matrices to allow algebraic operations
164  for (unsigned int i = 0; i < toInsert; i++) {
165  extScalerMean = nc::append(extScalerMean, scaler_mean, nc::Axis::ROW);
166  extScalerScale =
167  nc::append(extScalerScale, scaler_scale, nc::Axis::ROW);
168  extPCAMean = nc::append(extPCAMean, mean, nc::Axis::ROW);
169  }
170  nc::NdArray<float> scaled = scale(samples, extScalerMean, extScalerScale);
171  return transform(scaled, extPCAMean);
172 }
173 
174 } // namespace PCA
175 #endif
nc::NdArray< float > scale(const nc::NdArray< float > &sample)
Scales the features using a pre-built StandardScaler.
Definition: PCA.h:78
nc::NdArray< float > scaler_mean
This is the scaler mean we obtained using only 5000 samples and considering only the 8-dimensional fe...
Definition: PCA.h:54
nc::NdArray< float > scaler_scale
This is the scaler scale obtained with only 5000 samples and considering only the 8-dimensional featu...
Definition: PCA.h:66
nc::NdArray< float > transform(const nc::NdArray< float > &sample)
Transforms the sample using a PCA model previously built.
Definition: PCA.h:107
nc::NdArray< float > PCA(const std::vector< float > &sample)
Applies the pipeline Standard Scaler plus PCA to the sample data.
Definition: PCA.h:134
nc::NdArray< float > PCABatch(const nc::NdArray< float > &samples)
Applies the pipeline Standard Scaler plus PCA to the batch of samples.
Definition: PCA.h:158
nc::NdArray< float > componentsT
This is the PCA components.T we obtained when using 5000 samples and considering only the 8-dimension...
Definition: PCA.h:27
nc::NdArray< float > mean
This is the PCA mean we obtained using only 5000 samples and considering only the 8-dimensional featu...
Definition: PCA.h:43