If we have concrete range of max..min value it is quite easy to normalize it to 0..1 float values, but if we don\'t have concrete limits? Is it possible to build universal funct
There are many ways to do this. I'll leave out mapping -inf
and +inf
, which can be done with conditional statements.
exp(x) / (1 + exp(x))
or the equivalent 1 / (1 + exp(-x))
where exp
is the exponential function. This is a logistic function.atan(x) / pi + 1 / 2
(tanh(x) + 1) / 2
(1 + x / sqrt(1 + x*x)) / 2
(1 + x / (1 + abs(x)) / 2
(erf(x) + 1) / 2
You have probably noticed that most of these take a mapping to (-1, 1) and change it to (0, 1). The former is usually easier. Here is a graph of these functions:
In my Python 3.5.2, the fastest was (1 + x / (1 + abs(x)) * 0.5
.