Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I split numpy array with mask?

I want to split array into array with mask and index
like below

a = array([ 0,  1,  2,  3,  4, 5]))  
b = [0,2,3]  

into

c = array([[0, 2, 3], [1, 3, 4], [2, 4, 5]])  

can I do this without loop?

Edit:

More examples...

Say, we have an array a with shape [10, 10, 10]
where a[x, y, :] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Now given the mask b = [0, 3, 7]

I want the output to be an array c with shape [10, 10, 3, 3]
where c[x, y, :, :] = [[0, 3, 7], [1, 4, 8], [2, 5, 9]]

like image 547
JY Won Avatar asked Mar 27 '20 07:03

JY Won


1 Answers

You can generate the b as a broadcasted sum between the indices you want, and a shift-vector. Then you can broadcast again into a bigger size. Since the output in your examples are not depending on the a array, I'm disregarding that.

from numpy import array, broadcast_to, arange
from numpy.random import random

a = random((10,10,10)) # not used on the code at all.... don't understand what it is for...

b = [0,2,3]
b_array = array(b)
b_shifts = arange(3).reshape(-1,1)
c_cell= b+b_shifts # here, they are broadcasted toegether. one is a row-vector and one is a column-vector...
c = broadcast_to(c_cell,(10,10,3,3))

you might want to create b_shifts with some other method depending on step size and so on...


EDIT based on your comments, it seems a more accurate answer is:

from numpy import array, arange
a = arange(2*2*10).reshape((2,2,10)) # some example input 
b = array([0,2,3])                   # the 'template' to extract
shifts = arange(3).reshape(-1,1)     # 3 is the number of repeats
indexer = b+shifts                   # broadcasted sum makes a matrix
c = a[:,:,indexer]                   # extract

This will take the b array as a kind of template, and repeat it with a certain shift. Finally, it will extract those entries from every array a[i,j,:] into c[i,j,:,:]. Otput from above is:

print(a)

[[[ 0  1  2  3  4  5  6  7  8  9]
  [10 11 12 13 14 15 16 17 18 19]]
 [[20 21 22 23 24 25 26 27 28 29]
  [30 31 32 33 34 35 36 37 38 39]]]

print(c)

[[[[ 0  2  3]
   [ 1  3  4]
   [ 2  4  5]]
  [[10 12 13]
   [11 13 14]
   [12 14 15]]]
 [[[20 22 23]
   [21 23 24]
   [22 24 25]]
  [[30 32 33]
   [31 33 34]
   [32 34 35]]]]
like image 194
LudvigH Avatar answered Sep 30 '22 03:09

LudvigH