6

Basically I have an array that may vary between any two numbers, and I want to preserve the distribution while constraining it to the [0,1] space. The function to do this is very very simple. I usually write it as:

def to01(array):
    array -= array.min()
    array /= array.max()
    return array

Of course it can and should be more complex to account for tons of situations, such as all the values being the same (divide by zero) and float vs. integer division (use np.subtract and np.divide instead of operators). But this is the most basic.

The problem is that I do this very frequently across stuff in my project, and it seems like a fairly standard mathematical operation. Is there a built in function that does this in NumPy?

user3557216
  • 259
  • 1
  • 9
  • I'm a bit confused because if you normalize an array of ints between 0 and 1 you'll just have an array of zeros and one. Which means you are going to lose a lot of distributional information. – BKay Sep 11 '14 at 23:09
  • did you look thru the docs? – wwii Sep 11 '14 at 23:16
  • 1
    @user3557216 this looks pretty efficient, but I would change `array` to another name to avoid shadowing the `np.array()` function... – Saullo G. P. Castro Sep 12 '14 at 06:45
  • 1
    @BKay, the array would turn the ints into floats. I normally send floats to begin with, but this is something the augmented function would handle additionally. – user3557216 Sep 12 '14 at 17:23
  • @wwii I did, but I'm not sure what I'm looking for. – user3557216 Sep 12 '14 at 17:23
  • 1
    @Saullo Castro, I never do asterisk imports precisely to give me this freedom. – user3557216 Sep 12 '14 at 17:24
  • I'm curious why you are subtracting the min, in affect losing the offset information. Is the bias not informative? – wwii Sep 12 '14 at 20:39
  • I don't care about the offset, I only care about the relative distance from array minimum to array maximum. Think of this going into a colormap. so whether -20 to 300 or 2 to 3, I want all of the array values to go from 0 to 1 for the color conversion. – user3557216 Sep 12 '14 at 21:28
  • 2
    possible duplicate of [how to normalize array numpy?](http://stackoverflow.com/questions/21030391/how-to-normalize-array-numpy) – fredtantini Sep 19 '14 at 14:18
  • Possible duplicates? http://stackoverflow.com/questions/1735025/how-to-normalize-a-numpy-array-to-within-a-certain-range http://stackoverflow.com/questions/21030391/how-to-normalize-array-numpy – fredtantini Sep 19 '14 at 14:19

1 Answers1

2

Don't know if there's a builtin for that (probably not, it's not really a difficult thing to do as is). You can use vectorize to apply a function to all the elements of the array:

def to01(array):
    a = array.min()
    # ignore the Runtime Warning
    with numpy.errstate(divide='ignore'):
        b = 1. /(array.max() - array.min())
    if not(numpy.isfinite(b)):
        b = 0
    return numpy.vectorize(lambda x: b * (x - a))(array)
greschd
  • 606
  • 8
  • 19
  • Yeah I have a function doing this. It's a bit niche, and maybe more the responsibility of the colormapper than of the data gatherer, so I guess this will do. – user3557216 Oct 15 '14 at 21:16