Quantcast
Channel: Numpy where function multiple conditions - Stack Overflow
Browsing latest articles
Browse All 10 View Live

Answer by eric for Numpy where function multiple conditions

To get np.where() working with multiple conditions, just do the following:np.where((condition 1) & (condition 2)) # for andnp.where((condition 1) | (condition 2)) # for orI know this repeats some...

View Article



Image may be NSFW.
Clik here to view.

Answer by Amit Amola for Numpy where function multiple conditions

One interesting thing to point here; the usual way of using OR and AND too will work in this case, but with a small change. Instead of "and" and instead of "or", rather use Ampersand(&) and Pipe...

View Article

Answer by Xiong-Hui Chen for Numpy where function multiple conditions

Try:import numpy as npdist = np.array([1,2,3,4,5])r = 2dr = 3np.where(np.logical_and(dist> r, dist<=r+dr))# Output: (array([2, 3, 4]),)You can see Logic functions for more details.

View Article

Answer by Qhan for Numpy where function multiple conditions

This should work:dists[((dists >= r) & (dists <= r+dr))]

View Article

Answer by user4340135 for Numpy where function multiple conditions

I like to use np.vectorize for such tasks. Consider the following:>>> # function which returns True when constraints are satisfied.>>> func = lambda d: d >= r and d<= (r+dr)...

View Article


Answer by Mazdak for Numpy where function multiple conditions

The accepted answer explained the problem well enough. However, the more Numpythonic approach for applying multiple conditions is to use numpy logical functions. In this case, you can use...

View Article

Answer by Xin Wang for Numpy where function multiple conditions

Try:np.intersect1d(np.where(dists >= r)[0],np.where(dists <= r + dr)[0])

View Article

Answer by kiriloff for Numpy where function multiple conditions

I have worked out this simple example import numpy as npar = np.array([3,4,5,14,2,4,3,7])print [X for X in list(ar) if (X >= 3 and X <= 6)]>>> [3, 4, 5, 4, 3]

View Article


Answer by askewchan for Numpy where function multiple conditions

The best way in your particular case would just be to change your two criteria to one criterion:dists[abs(dists - r - dr/2.) <= dr/2.]It only creates one boolean array, and in my opinion is easier...

View Article


Numpy where function multiple conditions

I have an array of distances called dists. I want to select dists which are within a range. dists[(np.where(dists >= r)) and (np.where(dists <= r + dr))]However, this selects only for the...

View Article
Browsing latest articles
Browse All 10 View Live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>