-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep.cpp
103 lines (93 loc) · 3.09 KB
/
prep.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//------------------------------------------------------------------------
// PREPROCESS
//------------------------------------------------------------------------
class Preprocessor{
public:
virtual ~Preprocessor(){}
virtual void calc_scale(int nvec, int n, double* x, int ldx, double* scl_vecs) = 0;
virtual void scale(int nvec, int n, double* x, int ldx, double* scl_vecs);
virtual void unscale(int nvec, int n, double* x, int ldx, double* scl_vecs);
virtual void print(){}
private:
protected:
};
void Preprocessor::scale(
int nvec, int n, double* x, int ldx, double* scl_vecs)
{
double* down_vec = scl_vecs, *up_vec = scl_vecs + n;
for (int i = 0; i < nvec; ++i){
double* xi = x + i*ldx;
for (int j = 0; j < n; ++j)
xi[j] = (xi[j] - up_vec[j])/down_vec[j];
}
}
void Preprocessor::unscale(
int nvec, int n, double* x, int ldx, double* scl_vecs)
{
double* down_vec = scl_vecs, *up_vec = scl_vecs + n;
for (int i = 0; i < nvec; ++i){
double* xi = x + i*ldx;
for (int j = 0; j < n; ++j)
xi[j] = xi[j]*down_vec[j] + up_vec[j];
}
}
class NoScaling : public Preprocessor{
public:
void calc_scale(int nvec, int n, double* x, int ldx, double* scl_vecs){};
void scale(int nvec, int n, double* x, int ldx, double* scl_vecs){};
void unscale(int nvec, int n, double* x, int ldx, double* scl_vecs){};
void print(){sprint("No scaling\n");}
private:
protected:
};
class MaxMinScaling : public Preprocessor{
public:
void calc_scale(int nvec, int n, double* x, int ldx, double* scl_vecs);
void print(){sprint("MaxMin Scaling\n");}
private:
protected:
};
void MaxMinScaling::calc_scale(
int nvec, int n, double* x, int ldx, double* scl_vecs)
{
double* max_vec = scl_vecs, *min_vec = scl_vecs + n;
memcpy(max_vec, x, sizeof(double)*n);
memcpy(min_vec, x, sizeof(double)*n);
for (int i = 1; i < nvec; ++i){
double* xi = x + i*ldx;
for (int j = 0; j < n; ++j) {
min_vec[j] = std::min(min_vec[j], xi[j]);
max_vec[j] = std::max(max_vec[j], xi[j]);
}
}
for (int j = 0; j < n; ++j){
max_vec[j] -= min_vec[j];
if (max_vec[j] == 0.0) max_vec[j] = 1;
}
}
class ZscoreScaling : public Preprocessor{
public:
void calc_scale(int nvec, int n, double* x, int ldx, double* scl_vecs);
void print(){sprint("Z-score Scaling\n");}
private:
protected:
};
void ZscoreScaling::calc_scale(
int nvec, int n, double* x, int ldx, double* scl_vecs)
{
double* std = scl_vecs, *mean = scl_vecs + n;
memset(scl_vecs, 0, sizeof(double)*2*n);
for (int i = 0; i < nvec; ++i){
double* xi = x + i*ldx;
for (int j = 0; j < n; ++j) { mean[j] += xi[j]; }
}
for (int j = 0; j < n; ++j) { mean[j] /= nvec; }
for (int i = 0; i < nvec; ++i){
double* xi = x + i*ldx;
for (int j = 0; j < n; ++j) { std[j] += pow((xi[j]-mean[j]),2); }
}
for (int j = 0; j < n; ++j){
std[j] = sqrt(std[j]/nvec);
if (std[j] == 0.0) std[j] = 1;
}
}