'extracting rows with specific values form a nested numpy array

I have a numpy array as follow:

array([[0.603893  , 0.39610702],
   [0.6546377 , 0.34536234],
   [0.6454333 , 0.3545667 ],
   ...,
   [0.5815867 , 0.4184133 ],
   [0.52462864, 0.4753714 ],
   [0.4026965 , 0.5973035 ]], dtype=float32)

I want to extract the rows that the value of the first item is between say 0.2 to 0.5, How can I do this?

Where is the best place that I read about these type of tricks?



Solution 1:[1]

You can do that easily with: a[(a[:, 0] >= 0.2) & (a[:, 0] < 0.5)].

As for the information, the Numpy website is a good start... It contains tutorials and the documentation (with useful examples). Past StackOverflow answers also help to solve many Numpy-related problems (the ones with the highest votes can be useful to learn interesting tricks that can be often applied).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jérôme Richard