How to scale an array of signed integers to range from 0 to 1?

15

12

I'm using Brain to train a neural network on a feature set that includes both positive and negative values. But Brain requires input values between 0 and 1. What's the best way to normalize my data?

Jonathan Shobrook

Posted 2015-05-24T02:11:36.640

Reputation: 303

Answers

25

This is called unity-based normalization. If you have a vector $X$, you can obtain a normalized version of it, say $Z$, by doing:

$$Z = \frac{X - \min(X)}{\max(X) - \min(X)}$$

stochazesthai

Posted 2015-05-24T02:11:36.640

Reputation: 533

Is it Ok to use this method to normalize a value that represents percentage and can be negative but always higher than -2% and lower than 30%? Won't it be harder for a neural network to get the clue of the value meaning if I normalize it this way? – Ivan – 2018-10-02T12:32:24.663

1This approach is also known as min-max normalization (as we are using min and max values) – Shagun Sodhani – 2015-05-25T14:50:32.343

3

Find the largest positive number and the smallest (most negative) number in the array. Add the absolute value of the smallest (most negative) number to every value in the array. Divide each result by the difference between the largest and the smallest number.

mcduffee

Posted 2015-05-24T02:11:36.640

Reputation: 131

@Jonathan: it doesn't matter as long as both values are from the same array, original or updated. Since the same number is added to every value, the difference between the minimum and maximum stays the same. – RemcoGerlich – 2015-05-24T21:00:55.447

1

Before you do that, you may want to check for outliers. Say 99% of the data lie in range (-5, 5), but one little guy takes a value of 25.0. Your normalized array would cluster around (0, 0.3), and that would cause problem for the neural net to learn.

horaceT

Posted 2015-05-24T02:11:36.640

Reputation: 1 230

1

say you have a vector/array of values v = [1, -2, 3]

minV = Math.min.apply(Math, v);;
for(var i=0; i<v.length; i++) {v[i] -= minV;}
maxV = Math.max.apply(Math, v);;
for(var i=0; i<v.length; i++) {v[i] /= ( maxV - minV );}

Output at the end will be v = [0.6, 0, 1]. Explanation:

  1. Pushing the entire range of values to start from 0, so that we have no negatives

  2. Dividing the values by ( max - min ) of range, so that max will be 1

device_exec

Posted 2015-05-24T02:11:36.640

Reputation: 179