-
Notifications
You must be signed in to change notification settings - Fork 119
/
pre_process.js
121 lines (108 loc) · 3.31 KB
/
pre_process.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Description.
* Preprocess data with MinMaxScalar algorithm.
* X_std = (X - X.min()) / (X.max() - X.min())
* X_scaled = X_std * (max - min) + min
**/
const fs = require('fs');
var X_min = 0;
var X_max = 0;
var min_ = 0;
var max_ = 1;
/**
* Write the minimum and maximum to be used for later scaling to a file.
* Use the values for future transformation of data before model prediction.
**/
function write_util(){
const fs = require('fs')
// Data which will write in a file.
let data = {"X_max":X_max, "X_min":X_min, "max_":max_, "min_":min_};
fs.writeFileSync('utils.json', JSON.stringify(data));
}
/**
* fit data for preprocessing.
* @param {Array} X - input data.
* @param {integer} min - minimum value of the feature range.
* @param {integer} max - maximum value of the feature range.
* @return {Array} X_scaled - Final scaled array fitted within Feature Range.
**/
function fit(X, min=0, max=1){
X_max = Math.max.apply(null,X)
X_min = Math.min.apply(null,X)
min_ = min;
max_ = max;
var X_minArr = X.map(function(values){
return values - X_min
});
// X_std = (X - X.min()) / (X.max() - X.min())
var X_std = X_minArr.map(function(values){
return values / (X_max - X_min)
});
// X_scaled = X_std * (max - min) + min
var X_scaled = X_std.map(function(values){
return values*(max - min) + min
});
return X_scaled
}
/**
* Fit to data, then transform it.
* @param {Array} result - array of objects.
* @param {String} attribute - proprety of the JSON object to be accessed.
* @return {Array} train_scaled - Final scaled array fitted within Feature Range.
**/
function fit_transform(result, attribute){
var data = null;
try{
data = result.map(value => value[attribute]);
} catch (error) {
console.log("attribute undefined.");
}
var train_scaled = fit(data);
write_util();
return train_scaled;
}
/**
* Scale features of X according to feature_range.
* @param {Array} result - array of objects.
* @param {String} attribute - proprety of the JSON object to be accessed.
* @return {Array} X_scaled - Final scaled array fitted within Feature Range.
**/
function transform(result, attribute){
var data = null;
let fit = require('./utils.json');
var data = null;
try{
data = result.map(value => value[attribute]);
} catch (error) {
console.log("attribute undefined.");
}
console.log(fit.X_max,fit.X_min)
var X_minArr = data.map(function(values){
return values - fit.X_min
});
var X_std = X_minArr.map(function(values){
return values / (fit.X_max - fit.X_min)
});
var X_scaled = X_std.map(function(values){
return values*(fit.max_ - fit.min_) + fit.min_
});
return X_scaled
}
/**
* Undo the scaling of X according to feature_range.
* @param {Array} inp - Scaled array according to feature_range.
* @param {integer} min - minimum value of the feature range.
* @param {integer} max - maximum value of the feature range.
* @return {Array} X_ - Inverse Scaled Array.
**/
function inverse_transform(inp, min=0, max=1){
let fit = require('./utils.json');
var X = inp.map(function(values){
return (values - min)/ (max - min)
});
var X_ = X.map(function(values){
return values * (fit.X_max - fit.X_min) + fit.X_min
});
return X_
}
module.exports = { fit_transform, transform, inverse_transform }