11
$\begingroup$

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?

$\endgroup$
21
$\begingroup$

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

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

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.

$\endgroup$
  • $\begingroup$ @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. $\endgroup$ – RemcoGerlich May 24 '15 at 21:00
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.