Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use numpy.where in multi-conditions in python #5

Open
LiNinghui-AI opened this issue Mar 21, 2020 · 2 comments
Open

how to use numpy.where in multi-conditions in python #5

LiNinghui-AI opened this issue Mar 21, 2020 · 2 comments

Comments

@LiNinghui-AI
Copy link

LiNinghui-AI commented Mar 21, 2020

I have met a issue. For example: I created the following array, and I want to use np.where for multiple condition judgments
import numpy as np
a = np.array( [1,2,3,4,5,6] )
b = np.array( [4,5,8,9,7,9])
I want to get a subscript that meets the conditions, but an error occurs
index = np.where(a >1 and b != 2 and a < 6)
The error is as follows:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I don't want to use np.logical_and() , what is the solution? Thanks a lot!

@Huzaib
Copy link

Huzaib commented Aug 4, 2020

You cannot directly use and as it returns a boolean array.

Instead try this code:

index = np.where(np.logical_and(a>1, a<6, b!=2))

This will return the desired result.

Thank You

#5

@LiNinghui-AI
Copy link
Author

Thank you for your help, this problem has been perfectly solved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants