Normalize any value in range (-inf…+inf) to (0…1). Is it possible?

前端 未结 3 723
半阙折子戏
半阙折子戏 2021-02-06 14:23

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

3条回答
  •  死守一世寂寞
    2021-02-06 15:04

    There are many ways to do this. I'll leave out mapping -inf and +inf, which can be done with conditional statements.

    1. exp(x) / (1 + exp(x)) or the equivalent 1 / (1 + exp(-x)) where exp is the exponential function. This is a logistic function.
    2. atan(x) / pi + 1 / 2
    3. (tanh(x) + 1) / 2
    4. (1 + x / sqrt(1 + x*x)) / 2
    5. (1 + x / (1 + abs(x)) / 2
    6. (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.

提交回复
热议问题