0

I have to index an x and a y axis with a condition on an x axis. I tried using the np.where function like this:

x = data[:, 0]
y = data[:, 1]
y = y[np.where((x < 111) or (x > 125))]
x = x[np.where((x < 111) or (x > 125))]

But it prints the following error:

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

I tried using np.all then like this:

x = data[:, 0]
y = data[:, 1]
y = y[np.all([(x < 111) or (x > 125)], axis = 0)]
x = x[np.all([(x < 111) or (x > 125)], axis = 0)]

But got the same error, am I doing the 'or' condition wrong? Thanks in advance.

3 Answers3

1

For arrays use the numpy logical methods, standard python conditionals and and or don't work well.

x = data[:, 0]
y = data[:, 1]
y = y[np.logical_or((x < 111), (x > 125))]
x = x[np.logical_or((x < 111), (x > 125))]
Paddy Harrison
  • 1,808
  • 1
  • 8
  • 21
0

Try to use bitwise operator instead of or i.e |

x = data[:, 0]
y = data[:, 1]
y = y[np.where((x < 111) | (x > 125))]
x = x[np.where((x < 111) | (x > 125))]

Let me know if it works for you or not.

The Guy
  • 411
  • 4
  • 11
0

Try this:

import numpy as np

# test data
x = np.arange(0, 200)

# conditions:
# values wanted that are smaller than 111 and bigger than 125
cond1 = 111 > x
cond2 = x > 125

# get data that satisfies both conditions
x *= np.logical_or(cond1, cond2)

# (optional) remove zeros
x = x.nonzero()
code-lukas
  • 1,586
  • 9
  • 19