[SciPy-User] Selecting elements in a numpy array by their indices contained in two arrays

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[SciPy-User] Selecting elements in a numpy array by their indices contained in two arrays

Éric Depagne-2

Hi list.

 

Suppose I have a numpy array A (4,4) and two 1D array, one (X1) that would contain (2,2,1,1) and the other one X2 that would be (3,3,3,3).

Is there a way to select all the elements of A that have indices between X1 and X2?

In the current case, that would mean selecting A[2:3,0], A[2:3,1], A[1:3,2] and A[1:3,3]

 

Thanks.

Éric.

 

--

Un clavier azerty en vaut deux

----------------------------------------------------------

Éric Depagne [hidden email]

 


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Selecting elements in a numpy array by their indices contained in two arrays

Éric Depagne-2

Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit :

> Hi list.

Hi list,

 

Some more details on my problem.

I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop.

Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like

A[X1:X2, 1]

 

Since in my problem, A is (4000x2000), X1 and X2 (2000,1), avoiding unnecessary loops will make a significant difference.

 

Thanks.

Éric.

>

> Suppose I have a numpy array A (4,4) and two 1D array, one (X1) that would

> contain (2,2,1,1) and the other one X2 that would be (3,3,3,3).

> Is there a way to select all the elements of A that have indices between X1

> and X2? In the current case, that would mean selecting A[2:3,0], A[2:3,1],

> A[1:3,2] and A[1:3,3]

>

> Thanks.

> Éric.

 

 

--

Un clavier azerty en vaut deux

----------------------------------------------------------

Éric Depagne [hidden email]

 


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Selecting elements in a numpy array by their indices contained in two arrays

Robert Kern-2
On Mon, Jun 26, 2017 at 4:40 PM, Éric Depagne <[hidden email]> wrote:

>
> Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit :
>
> > Hi list.
>
> Hi list,
>
> Some more details on my problem.
>
> I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop.
>
> Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like
> A[X1:X2, 1]

There isn't one since for each `i`, there will be a different number of items in the slice, so the result would not be able to be fit into an array with a uniform shape. The loop is the right way to do this.

--
Robert Kern

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Selecting elements in a numpy array by their indices contained in two arrays

Juan Nunez-Iglesias
Well, the resulting array size is known ahead of time. I haven’t come up with a nice vectorised NumPy way to do it, but I would recommend preallocating the result array and then using Numba or Cython to loop. You’ll get some pretty nice speed.

Also, since you are grabbing column-wise chunks, if possible, I would suggest making sure your A matrix has order=‘F’.

(Incidentally, if you want to keep track of the origin columns, I think scipy.sparse.csc_matrix is a nice format for the result.)

Juan.

On 27 Jun 2017, 9:48 AM +1000, Robert Kern <[hidden email]>, wrote:
On Mon, Jun 26, 2017 at 4:40 PM, Éric Depagne <[hidden email]> wrote:
>
> Le lundi 26 juin 2017, 04:27:47 SAST Éric Depagne a écrit :
>
> > Hi list.
>
> Hi list,
>
> Some more details on my problem.
>
> I know that I can select the elements individually through a for loop, and that will provide me with the extraction I need, but I was wondering if there was a way to do that without a loop.
>
> Repeating 4 times A[X1[i]:X2[i],1] will work fine, but I was looking for something like
> A[X1:X2, 1]

There isn't one since for each `i`, there will be a different number of items in the slice, so the result would not be able to be fit into an array with a uniform shape. The loop is the right way to do this.

--
Robert Kern
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Selecting elements in a numpy array by their indices contained in two arrays

Alexander Barth
Dear Eric,
I think you can use numpy.ravel_multi_index for this.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel_multi_index.html

Here is an example:

In [10]: A = np.random.randn(4,4)

In [11]: A
Out[11]:
array([[ 1.9158152 , -0.69065018,  0.71567087, -1.31872821],
       [ 1.00126163,  0.34461176,  0.30298258, -1.19507848],
       [-0.14355935,  0.18827163,  0.78383867, -1.01362843],
       [ 0.33303015, -1.49289243, -1.02713378,  0.24379566]])

In [12]: i = np.array([2,2,1,1])

In [13]: j = np.array([3,3,3,3])

In [16]: ij = np.ravel_multi_index((i,j),A.shape)

In [17]: ij
Out[17]: array([11, 11,  7,  7])

In [21]: A.flatten()[ij]
Out[21]: array([-1.01362843, -1.01362843, -1.19507848, -1.19507848])

In [23]: [A[i[l],j[l]] for l in range(len(i))]
Out[23]:
[-1.0136284267170597,
 -1.0136284267170597,
 -1.195078482610719,
 -1.195078482610719]

I hope this helps,
Cheers,
Alex
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Loading...