hmnoidk
hmnoidk

Reputation: 565

"The truth value of an array with more than one element is ambiguous" error [Python]

index = np.where(slopes > mean - 2 * sd and slopes < mean + 2 * sd)[0]

returns this error:

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

If I instead write idx = np.where(slopes < mean + 2 * sd)[0] or idx = np.where(slopes > mean - 2 * sd)[0] I get the right indices. Why can't I combine both conditions?

Upvotes: 1

Views: 863

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24322

Instead of using boolean 'and':

index = np.where((slopes > mean - 2 * sd) and (slopes < mean + 2 * sd))[0]

Try your code with bitwise '&':

index = np.where((slopes > mean - 2 * sd) & (slopes < mean + 2 * sd))[0]

Note: For more Information regarding boolean 'and' and bitwise '&' you can refer to this Question

Upvotes: 1

Related Questions