0

Briefly... here is the problem:

import numpy as np
a = np.array([ 0, 1, 2, 3, 4, 5, 6, 100, 8, 9])
np.where(a==100, -1, a[a])

What I expect to get is: 0, 1, 2, 3, 4, 5, 6, -1, 8, 9 Instead I'm getting: index 100 out of bounds 0<=index<10

I admit that the index is invalid but is shouldn't eval a[100] but -1 instead... as far as I understand numpy.where() command structure.

What I'm doing wrong in this example?

Just to clarify what I actually trying to do here is more detailed code: It is a lookup table array remapping procedure:

import numpy as np

# gamma-ed look-up table array
lut = np.power(np.linspace(0, 1, 32), 1/2.44)*255.0

def gamma(x):
    ln = (len(lut)-1)
    idx = np.uint8(x*ln)
    frac = x*ln - idx
    return np.where( frac == 0.0,
                    lut[idx],
                    lut[idx]+(lut[idx+1]-lut[idx])*frac)

# some linear values array to remap
lin = np.linspace(0, 1, 64)

# final look-up remap
gamma_lin = gamma(lin)
2
  • you should avoid using the same a with different meanings, even in different scopes (a[a] is really ugly and bad). also, a==100 is your condition that's why it gets evalued docs.scipy.org/doc/numpy/reference/generated/numpy.where.html Commented Oct 25, 2012 at 8:47
  • Yes, i agree that my quick code was not the best example. In my actual code (check above) I do not have such thing as a[a] but anyway getting index out of bound because python always validates (executes?) both expressions within numpy.where clause call. Commented Oct 25, 2012 at 20:49

3 Answers 3

2

Expressions that you put as function arguments are evaluated before they are passed to the function (Documentation link). Thus you are getting an index error from the expression a[a] even before np.where is called.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks silvado, the doc reference explains this issue. I'm a python beginner so not quite aware of all the function call pequliarities.
1

Use the following:

np.where(a==100, -1, a)

As stated by the documentation:

numpy.where(condition[, x, y])

Return elements, either from x or y, depending on condition.
If only condition is given, return condition.nonzero().

Here, a==100 is your condition, -1 the value that should be taken when the condition is met (True), a the values to fall back on.


The reason why you're getting an IndexError is your a[a]: you're indexing the array a by itself, which is then equivalent to a[[0,1,2,3,4,5,6,100,8,9]]: that fails because a has less than 100 elements...


Another approach is:

a_copy = a.copy()
a_copy[a==100] = -1

(replace a_copy by a if you want to change it in place)

3 Comments

still this works fine: a = [ 0, 1, 2, 3, 4, 5, 6, 100, 8, 9 ] a = [ -1 if k==100 else a[k] for j, k in enumerate(a)] which is handled just fine by python... but I see where my problem is, thanks. Still not quite undertand why it is necessary to evaluate two expressions if only one is used after all.
@decuman: with a = [ -1 if k==100 else a[k] for j, k in enumerate(a)] you never get to evaluate a[k] when k=100, therefore you don't run into the IndexError.
I see. I've just confused python's if conditional execution with numpy.where() functional call specific.
0

When you write a[a] you try to take index 0,1,2...100... from a which is why you get the index out of bounds error. You should instead write np.where(a==100, -1, a) - I think that will produce the result you are looking for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.