-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.h
47 lines (38 loc) · 1.08 KB
/
preprocessing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef __PREPROCESSING_H__
#define __PREPROCESSING_H__
#include <vector>
#include <numeric>
#include <cmath>
#include <iterator>
#include <algorithm>
#include "data_types.h"
void CalcMeanStdev( const std::vector<double> &data, double& mean, double& stdev )
{
const size_t n_data = data.size();
mean = 0.;
stdev = 0;
mean = std::accumulate( data.begin(), data.end(), 0. ) / double(n_data);
stdev = 0.;
std::for_each( data.begin(), data.end(), [&](const double x) {
stdev += ( x - mean ) * ( x - mean );
});
stdev = sqrt( stdev / double(n_data-1) );
}
void Standardize( std::vector<double> &data ) {
const size_t n_data = data.size();
double mean = 0.;
double stdev = 0;
CalcMeanStdev( data, mean, stdev );
for( int i = 0 ; i < n_data ; i++ ) {
data[i] -= mean;
data[i] /= stdev;
}
}
void Standardize( IrisDataCollection& data )
{
Standardize( data.sepal_length );
Standardize( data.sepal_width );
Standardize( data.petal_length );
Standardize( data.petal_width );
}
#endif /* __PREPROCESSING_H__ */