# How to normalize data between -1 and 1?

I have seen the min-max normalization formula but that normalizes values between 0 and 1. How would I normalize my data between -1 and 1? I have both negative and positive values in my data matrix.

• If you're working in R, see this thread for a few options. In particular, a comment on the accepted answer has this function where you set the 'newMax' to 1 and 'newMin' to -1 and run the function on your data – mtreg Oct 26 '15 at 1:19
• You can find reference at Wikipedia as follows: en.wikipedia.org/wiki/Normalization_(statistics) – salem Feb 26 '18 at 5:38
• Javascript example, taken from here. function convertRange( value, r1, r2 ) { return ( value - r1[ 0 ] ) * ( r2[ 1 ] - r2[ 0 ] ) / ( r1[ 1 ] - r1[ 0 ] ) + r2[ 0 ]; } convertRange( 328.17, [ 300.77, 559.22 ], [ 1, 10 ] ); >>> 1.9541497388276272 – Giuseppe Canale Mar 8 '18 at 16:46
• @covfefe if you are still around you might want to accept one of the answers – Simone Nov 4 '18 at 9:06

With: $$x' = \frac{x - \min{x}}{\max{x} - \min{x}}$$ you normalize your feature $x$ in $[0,1]$.

To normalize in $[-1,1]$ you can use:

$$x'' = 2\frac{x - \min{x}}{\max{x} - \min{x}} - 1$$

In general, you can always get a new variable $x'''$ in $[a,b]$:

$$x''' = (b-a)\frac{x - \min{x}}{\max{x} - \min{x}} + a$$

• Honestly I don't have citations for this. It is just a linear transformation of a random variable. Have a look at the effect of linear transformations on the support of a random variable. – Simone Oct 19 '17 at 4:40
• Do you have better methods to do this? – GoingMyWay Feb 29 at 12:15
• @GoingMyWay better from what point of view? – Simone Feb 29 at 21:03
• Appreciate the final general formula for any interval $[a,b]$ – Alex Trueman Apr 18 at 19:32

I tested on randomly generated data, and

$$\begin{equation} X_{out} = (b-a)\frac{X_{in} - \min{X_{in}}}{\max{X_{in}} - \min{X_{in}}} + a \end{equation}$$

does not preserve the shape of the distribution. Would really like to see the proper derivation of this using functions of random variables.

The approach that did preserve the shape for me was using:

$$\begin{equation} X_{out} = \frac{X_{in} - \mu_{in}}{\sigma_{in}} \cdot \sigma_{out} + \mu_{out} \end{equation}$$

where

$$\begin{equation} \sigma_{out} = \frac{b-a}{6} \end{equation}$$

(I admit that using 6 is a bit dirty) and

$$\begin{equation} \mu_{out} = \frac{b+a}{2} \end{equation}$$

and

$$a$$ and $$b$$ is the desired range; so as per original question would be $$a=-1$$ and $$b=1$$.

I arrived at the result from this reasoning

$$\begin{equation} Z_{out} = Z_{in} \end{equation}$$

$$\begin{equation} \frac{X_{out} - \mu_{out}}{\sigma_{out}} = \frac{X_{in} - \mu_{in}}{\sigma_{in}} \end{equation}$$

• Are you sure that this guarantees the transformed data will lie within the bounds? In R, try: set.seed(1); scale(rnorm(1000))*.333. I get a max of 1.230871. Your method seems to be just a tweak on standardizing data, rather than normalizing them as requested. Note that the question does not ask for a method that preserves the shape of the distribution (which would be a strange requirement for normalization). – gung - Reinstate Monica Jul 17 '19 at 17:01
• I'm not sure how the original transformation could fail to preserve the shape of the data. It's equivalent to subtracting a constant and then dividing by a constant, which is what your proposal does, and which doesn't change the shape of the data. Your proposal assumes all the data falls within three standard deviations of the mean, which may be somewhat reasonable with small, approximately normally distributed samples, but not with big or non-normal samples. – Noah Jul 17 '19 at 17:01
• @Noah It's not equivalent to subtracting and dividing by constants, because the min and max of the data are random variables. Indeed, for most underlying distributions they are pretty variable--more variable than the rest of the data--whence using them for any form of standardization is usually not a good idea. In this answer it's unclear what $a$ and $b$ mean or how they might be related to the data. – whuber Jul 17 '19 at 17:15
• @whuber true, but I meant that in a given dataset (i.e., treating the data as fixed), they are constants, in the same way the sample mean and sample standard deviation function as constants when standardizing a dataset. My impression was that OP wanted to normalize a dataset, not a distribution. – Noah Jul 17 '19 at 17:57
• @Noah I had the same impression, but I believe the present post may be responding to a different interpretation. – whuber Jul 17 '19 at 19:57