-
Notifications
You must be signed in to change notification settings - Fork 0
/
minMaxScaler.js
36 lines (25 loc) · 1.24 KB
/
minMaxScaler.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
// Function minMaxScaler()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------
// Description:
// Re-scales an array to fall between zero and one based on the minimum and
// maxium values in the array (so-called "min-max rescaling"). The minimum value
// will equal zero; the maximum value will equal one. Null values are ignored.
//------------------------------------------------------------------------------
// Parameters:
// yourArray : Array of numbers; may include null values.
// Returns
// A re-scaled Array.
//------------------------------------------------------------------------------
// START
function minMaxScaler(yourArray){
let yourArrayX = yourArray.filter(x => {if (x != null) {return x}})
let min = Math.min.apply(Math, yourArrayX);
let max = Math.max.apply(Math, yourArrayX);
let numerator = yourArray.map(x => {if (x != null) {return x - min} else {return null}});
let z = numerator.map(x => {if (x != null) {return x / (max - min)} else {return null}});
return z;
}
// END
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------