-2

I have code that checks if the list is empty

end_reg = np.argmin(vals_reg)
print(end_reg)
print("vals_reg[:end_reg]")
print(vals_reg[:end_reg])
if not vals_reg[:end_reg]:
    start_reg = np.argmax(vals_reg)
    end_reg = np.argmin(vals_reg[start_reg:]) + start_reg
else:
    start_reg = np.argmax(vals_reg[:end_reg])

I get such prints, so seems that the arrays and everything is ok. The check for the emptiness fails

5
vals_reg[:end_reg]
[ 24844.  34973.  33538.  31136.  28258.]

And I get following error:

    if not vals_reg[:end_reg]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
Alina
  • 2,191
  • 3
  • 33
  • 68

1 Answers1

0

The error seems pretty self-explanatory. Numpy is refusing to make a judgement about whether the array vals_reg[:end_reg] or indeed any array should be interpreted as True or False, because the creators of Numpy thought that it was ambiguous (which is true; it may be more natural for an array of all Falses to be classified as False, rather than True by virtue of being non-empty).

Python has made the decision to classify lists as False if and only if the list is empty, because the creators of Python thought was sufficiently obvious and useful, but this is not a Python list, it is a Numpy array.

What you're actually asking, in non-ambiguous terms, is whether the array is empty. You can do that with array methods/attributes:

How can I check whether the numpy array is empty or not?

i.e.

vals_reg[:end_reg].size == 0

is the test you want.

Community
  • 1
  • 1
Denziloe
  • 7,473
  • 3
  • 24
  • 34