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

Post Reply
programmer
Posts: 9
Joined: 27 Mar 2019

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

Post by programmer » 27 Mar 2019

Hello, I am new to numpy and i am learning numpy from the scratch. I am trying to print some random values using numpy array and trying to select values based on some threshold. Please see the below code

Code: Select all

import numpy as np
array = np.random.rand(3,5)
print(array[array>0.5])
It is showing the output and now i try to put two conditions together using or operator like below

Code: Select all

print(array[array<0.5 or array>0.5])
During above statement, following output came

Code: Select all

Traceback (most recent call last):
  File "C:/Users/AI/PycharmProjects/Logics/truth_values.py", line 6, in <module>
    print(array[array<0.5 or array>0.5])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any solution will be highly appreciated..

User avatar
Aisangam
Site Admin
Posts: 27
Joined: 24 Mar 2019
Location: Delhi, India
Contact:

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

Post by Aisangam » 27 Mar 2019

@ programmer as i could see that you are trying to use or operator for two condition. Please execute the below code instead of

Code: Select all

print(array[array<0.5 or array>0.5])
Code to execute

Code: Select all

print(np.logical_or(array[array<0.5,array>0.5)
I hope this will resolve your error.

Post Reply