# 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 Oct 26, 2015 at 1:19
• You can find reference at Wikipedia as follows: en.wikipedia.org/wiki/Normalization_(statistics) Feb 26, 2018 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 Mar 8, 2018 at 16:46
• @covfefe if you are still around you might want to accept one of the answers Nov 4, 2018 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. Oct 19, 2017 at 4:40
• Do you have better methods to do this? Feb 29, 2020 at 12:15
• Appreciate the final general formula for any interval $[a,b]$ Apr 18, 2020 at 19:32
• @ThePredator this is a linear transformation of a random variable, so it is invertible. But you need to know the original $\max{x}$ and $\min{x}$. If you have $x''$ (as in the formula above) in $[-1,1]$ you can get back to $x$ with $(\max{x} - \min{x})\frac{x''+1}{2} + \min{x}$. Jun 28, 2020 at 9:49
• or in general: $x=\frac{(x'''-a)(\max{x}-\min{x})}{b-a}+\min{x}$. I advise keeping your original and normalised datasets, you can then find $\max{x}$ and $\min{x}$ easily by just looking at your original dataset again.
– GMSL
Oct 11, 2021 at 11:22

I tested on randomly generated data, and

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

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:

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

where

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

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

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

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

$$$$Z_{out} = Z_{in}$$$$

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

• 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). Jul 17, 2019 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, 2019 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, 2019 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, 2019 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, 2019 at 19:57