### How to normalize data to 0-1 range?

• I am lost in normalizing, could anyone guide me please.

I have a minimum and maximum values, say -23.89 and 7.54990767, respectively.

If I get a value of 5.6878 how can I scale this value on a scale of 0 to 1.

is this the way =(value-min)/(max-min)

Explanation of protection: This question is attracting extra answers containing code solutions only. While these may be interesting or useful to some readers, it's not an aim of CV to provide repositories of code solutions.

the solutions provided consider a *linear* contrast value - would you like a different normalization, for instance one that achieve an uniform probability for the output?

7 years ago

If you want to normalize your data, you can do so as you suggest and simply calculate the following:

$$z_i=\frac{x_i-\min(x)}{\max(x)-\min(x)}$$

where $$x=(x_1,...,x_n)$$ and $$z_i$$ is now your $$i^{th}$$ normalized data. As a proof of concept (although you did not ask for it) here is some R code and accompanying graph to illustrate this point:

# Example Data
x = sample(-100:100, 50)

#Normalized Data
normalized = (x-min(x))/(max(x)-min(x))

# Histogram of example data and normalized data
par(mfrow=c(1,2))
hist(x,          breaks=10, xlab="Data",            col="lightblue", main="")
hist(normalized, breaks=10, xlab="Normalized Data", col="lightblue", main="")


I only wonder how the two quite different-looking histograms do illustrate the point of your (correct) answer?

@ttnphns They look only different due to the binning of the histograms. My point however was to show that the original values lived between -100 to 100 and now after normalization they live between 0 and 1. I could have used a different graph to show this I suppose or just summary statistics.

The gentle nudge by @ttnphns was meant to encourage you not only to use a less complicated means of illustrating a (simple) idea, but also (I suspect) as a hint that a more directly relevant illustration might be beneficial here. You could do both by finding a more straightforward way to graph the transformation when it is applied to the min and max actually supplied by the O.P.

Is there any other function that can help normalize / scale values without the use of max & min?

Is there a way to "normalize" to custom range instead of 0-1?

@JohnDemetriou May not be the cleanest solution, but you can scale the normalized values to do that. If you want for example range of 0-100, you just multiply each number by 100. If you want range that is not beginning with 0, like 10-100, you would do it by scaling by the MAX-MIN and then to the values you get from that just adding the MIN. So scale by 90, then add 10. That should be enough for most of the custom ranges you may want.

to use on multi-dimensional data in numpy, use normalized = (x-x.min())/(x.max()-x.min())