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

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?

## 4 Answers

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)}$$

• This approach is also known as min-max normalization (as we are using min and max values) – Shagun Sodhani May 25 '15 at 14:50
• 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 Oct 2 '18 at 12:32

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.

• @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 May 24 '15 at 21:00

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

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.