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},
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,
55 1.079515125000000080e+04, 2.652374999999999972e+01,
56 2.904959681383768952e+02, 5.040460461552937659e+02,
57 1.784837500024586854e+00, 9.787104166666666742e+02,
67 6.728206620826987091e+03, 2.899059219708179924e+01,
68 1.302756303387655201e+01, 2.544038827736384079e+01,
69 1.753588622119440776e+00, 1.909781553371280793e+01,
78 nc::NdArray<float>
scale(
const nc::NdArray<float>& sample) {
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;
107 nc::NdArray<float>
transform(
const nc::NdArray<float>& sample) {
108 auto transformed = sample -
mean;
109 transformed = nc::dot<float>(transformed,
componentsT);
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);
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);
147 nc::NdArray<float>
PCA(
const nc::NdArray<float>& sample) {
148 nc::NdArray<float> scaled =
scale(sample);
158 nc::NdArray<float>
PCABatch(
const nc::NdArray<float>& samples) {
159 auto toInsert = samples.numRows() -
scaler_mean.numRows();
162 auto extPCAMean =
mean.copy();
164 for (
unsigned int i = 0; i < toInsert; i++) {
165 extScalerMean = nc::append(extScalerMean,
scaler_mean, nc::Axis::ROW);
167 nc::append(extScalerScale,
scaler_scale, nc::Axis::ROW);
168 extPCAMean = nc::append(extPCAMean,
mean, nc::Axis::ROW);
170 nc::NdArray<float> scaled =
scale(samples, extScalerMean, extScalerScale);
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