1
$\begingroup$

So I have a range of numbers that I need to "squish" into values between 0 and 1 - where lowest value is zero and the highest is 1. I need preserve the ratio.

For instance. If I had these example arrays (all the same ratio between each other):

0, 2000, 8000, 10000

0, 4000, 16000, 20000

0, 200000, 800000, 1000000

I'm hoping to get back a result something like this for each of those individual arrays:

0, 0.2, 0.8, 1

Notice that the ratio between the numbers is preserved.

Is there a built in function somewhere (in numpy?) that does this for me?

$\endgroup$
6
  • $\begingroup$ Do you definitely need the smallest value to be $0$ and largest to be $1?$ Why? // Do you have a mix of positive and negative numbers? // What does it mean to have a ratio in the output space that involves the lowest number, now equal to zero? $\endgroup$
    – Dave
    Aug 5 '21 at 23:58
  • $\begingroup$ > Do you definitely need the smallest value to be 0 and largest to be 1? Why? Yes, since I want all the values "swished" to a value between 0 and 1 since a down stream dependency expects that range. > What does it mean to have a ratio in the output space that involves the lowest number, now equal to zero I don't know what that means, sorry. I'm just looking to squish a range of numbers down into values between 0 and 1. Same ratio. Is your question about handling 0? $\endgroup$
    – will
    Aug 6 '21 at 0:04
  • 1
    $\begingroup$ Is zero always your smallest original value? If so, there is an easy solution. $\endgroup$
    – Dave
    Aug 6 '21 at 0:09
  • $\begingroup$ Oh I see. No, zero won't always be the smallest number. For instance, for the array [500, 750, 1000] I would expect [0, 0.5, 1] $\endgroup$
    – will
    Aug 6 '21 at 13:38
  • $\begingroup$ Then your task cannot be accomplished. Perhaps you can post a new question about the downstream analysis that requires the 0-1 range. $\endgroup$
    – Dave
    Aug 6 '21 at 13:54
3
$\begingroup$

You're asking for a function $f$, defined on some closed interval $\left[m, M\right]$, with values is $\left[0, 1\right]$, with the property that:

$$\frac{f(x)}{f(y)} = \frac{x}{y}$$

for all $x < y$ in said interval.

From this specification, we can derive a property the function must have. Fix $y$ to any non-zero value for which $f(y) \neq 0$, and let $x$ stay variable:

$$\frac{f(x)}{f(y)} = \frac{x}{y} \Rightarrow f(x) = \frac{f(y)}{y} x = \text{constant} \times x$$

So any such function must be of the form $f(x) = c x$. Bringing in the requirement that $f(m) = 0$:

$$ 0 = f(m) = c m $$

So either $c$ or $m$ must be zero. We can't have $c = 0$, since then everything collapses, so it must be the case that $m = 0$.

Therefore, such a function exists only when $m = 0$. In this case, such a function is easy to construct: $f(x) = \frac{x}{M}$.

$\endgroup$
0
$\begingroup$

Your specifications are not entirely clear to me. Maybe starting with x and ending with y or z will do what you want. [Arithmetic and plots using R.]

set.seed(1234)
x = rexp(20, .001)
min(x); max(x)
[1] 6.581957
[1] 3052.458

y = x/max(x)
min(y); max(y)
[1] 0.002156281
[1] 1

z = (x - min(x))/(max(x)- min(x))
min(z); max(z)
[1] 0
[1] 1

par(mfrow=c(1,3))
 hist(x, prob=T, col="skyblue2"); rug(x)
 hist(y, prob=T, col="skyblue2"); rug(y)
 hist(z, prob=T, col="skyblue2"); rug(z)
par(mfrow=c(1,1))

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.