Pick Txk Numpy Array From Txn Numpy Array Using Txk Column Index Array
This is an indirect indexing problem. It can be solved with a list comprehension. The question is whether, or, how to solve it within numpy, When data.shape is (T,N) and c.s
Solution 1:
You can avoid loops with np.choose:
In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
data = np.array([\
[ 0, 1, 2],\
[ 3, 4, 5],\
[ 6, 7, 8],\
[ 9, 10, 11],\
[12, 13, 14]])
c = np.array([
[0, 2],\
[1, 2],\
[0, 0],\
[1, 1],\
[2, 2]])
--
In [2]: np.choose(c, data.T[:,:,np.newaxis])
Out[2]:
array([[ 0, 2],
[ 4, 5],
[ 6, 6],
[10, 10],
[14, 14]])
Solution 2:
Here's one possible route to a general solution...
Create masks for data
to select the values for each column of out
. For example, the first mask could be achieved by writing:
>>> np.arange(3) == np.vstack(c[:,0])
array([[ True, False, False],
[False, True, False],
[ True, False, False],
[False, True, False],
[False, False, True]], dtype=bool)
>>> data[_]
array([ 2, 5, 6, 10, 14])
The mask to get the values for the second column of out
: np.arange(3) == np.vstack(c[:,1])
.
So, to get the out
array...
>>>mask0 = np.arange(3) == np.vstack(c[:,0])>>>mask1 = np.arange(3) == np.vstack(c[:,1])>>>np.vstack((data[mask0], data[mask1])).T
array([[ 0, 2],
[ 4, 5],
[ 6, 6],
[10, 10],
[14, 14]])
Edit: Given arbitrary array widths K
and N
you could use a loop to create the masks, so the general construction of the out
array might simply look like this:
np.vstack([data[np.arange(N) == np.vstack(c[:,i])] for i in range(K)]).T
Edit 2: A slightly neater solution (though still relying on a loop) is:
np.vstack([data[i][c[i]] for i in range(T)])
Post a Comment for "Pick Txk Numpy Array From Txn Numpy Array Using Txk Column Index Array"