[Numpy-discussion] Extract subset from an array
Robert Kern
robert.kern@gmail....
Tue Feb 16 09:34:43 CST 2010
On Tue, Feb 16, 2010 at 06:42, Nicola Creati <ncreati@inogs.it> wrote:
> Hello,
> I need to extract a subset from a Nx3 array. Each row has x, y, and z
> coordinates.
> The subset is just a portion of the array in which the following
> condition realizes
>
> x_min < x < x_max and y_min < y < y_max
>
> The problem reduce to the extraction of points inside a rectangular box
> defined by
> x_min, x_max, y_min, y_max.
>
> I work with large arrays, the number or rows is always larger than 5x1e7.
> I'm looking for a fast way to extract the subset.
>
> At the moment I found a solution that seems the best. This is a small
> example:
>
> import numpy as np
>
> # Create a large 1e7x3 array of random numbers
> array = np.random.random((10000000, 3))
>
> # Define rectangular box
> x_min = 0.3
> x_max = 0.5
> y_min = 0.4
> y_max = 0.7
>
> # Create bool array that indicates the elemnts of array to extract
> condition = (array[:,0]>x_min) & (array[:,0]<x_max) & (array[:,1]>y_min)
> & (array[:,1]<y_max)
>
> # Extract the subset
> subset = array[condition]
>
> Are there any faster solution?
That's about as good as it gets.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list