0

Edit: Question updated - I should have asked:

Why is this:

new_x = x[(0<x<10) & (20<x<40)]

giving me "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"?

The original question I posted did not reflect the problem I was having (apologies - my actual conditions are more complicated and I had oversimplified it). Original question below:

I have a numpy array x and I want to create a new array of x elements satisfying x>0 and x<10. I have tried all the following:

new_x = x[(x>0) and (x<10)]

new_x = x[(x>0) & (x<10)]

new_x = x[np.logical_and(x>0, x<10)]

new_x = x[np.where(x>0, x<10)]

new_x = x[np.all(x>0, x<10)]

new_x = x[np.all((x>0) and (x<10))]

new_x = x[np.all(x>0) and np.all(x<10)]

new_x = x[np.any(x>0, x<10)]

and they all give the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have ran out of ideas. I realise this may be a duplicate question but I have consulted this and this and many others which did not help as I keep getting the same value error.

Edit: Fixed typo (x<0 and x>10) --> (x>0 and x<10)

ru111
  • 813
  • 3
  • 13
  • 27
  • Why not simply using the `filter` function? – rll Dec 07 '17 at 20:18
  • Use the last version, but add additional brackets: `selection = np.any((x<0, x>10))`. The first argument of np.any need to be an iterable, the second would be the axis. – YSelf Dec 07 '17 at 20:18
  • 3
    The second one is correct. You are probably doing something else wrong. – ayhan Dec 07 '17 at 20:20
  • @rll because that would give you a `filter` object.... clearly, the OP is looking to work with `numpy.ndarray` objects. – juanpa.arrivillaga Dec 07 '17 at 20:22
  • 2
    @ayhan yea, the second and third one should definitely work. I suspect that `x` may be a `dtype=object` array that contains *other array objects*. That would potentially produce these errors. – juanpa.arrivillaga Dec 07 '17 at 20:24
  • @ru111 what does `x.dtype, x.shape` give you? – juanpa.arrivillaga Dec 07 '17 at 20:24
  • @juanpa.arrivillaga int64 and (200000,) - but I think my conditions just need to be revised (they are a bit complicated). If it was the problem with my condition statements the error message is very misleading though. – ru111 Dec 07 '17 at 20:27
  • 2
    @ru111 you are going to need to provide a [mcve] or else one can only guess. – juanpa.arrivillaga Dec 07 '17 at 20:28

3 Answers3

2

I'm confused with the logical statements. How can a value be both less than 0 and greater than 10?

In [1]: import numpy as np

In [2]: x = np.arange(-10,20)

In [3]: x
Out[3]:
array([-10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1,   0,   1,   2,
     3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,

In [5]: x[(x<0) & (x>10)]
Out[5]: array([], dtype=int64)

In [6]: x[(x<0) | (x>10)]
Out[6]:
array([-10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1,  11,  12,  13,
    14,  15,  16,  17,  18,  19])
SciGuyMcQ
  • 993
  • 6
  • 21
0

Replacing and with & in the first one will do the trick.

0

The issue was that I was using conditions like x[(0<x<10) & (20<x<40)] (I should have guessed from the error message "more than one element") and it is solved by using separating statements i.e. x[(0<x) & (x<10) & (20<x) & (x<40)].

ru111
  • 813
  • 3
  • 13
  • 27