Reputation: 547
I have a 2d numpy array: data.shape==(n,8), and another ind.shape=(n,4). The ind array is the same length as data, and contains indices like [4,3,0,6]. How can I create another array with shape==(n,4) containing the elements from data specified by the indices from ind? My actual arrays are pretty long (shape[0]), so the loop is slow. There must be a better way than loops?
import numpy as np
# Example data
data = np.array([[ 0.44180102, -0.05941365, 2.1482739 , -0.56875081, -1.45400572,
-1.44391254, -0.33710766, -0.44214518],
[ 0.79506417, -2.46156966, -0.09929341, -1.07347179, 1.03986533,
-0.45745476, 0.58853107, -1.08565425],
[ 1.40348682, -1.43396403, 0.8267174 , -1.54812358, -1.05854445,
0.15789466, -0.0666025 , 0.29058816]])
ind = np.array([[3, 4, 1, 5],
[4, 7, 0, 1],
[5, 1, 3, 6]])
# This is the part I want to vectorize:
out = np.zeros(ind.shape)
for i in range(ind.shape[0]):
out[i,:] = data[i,ind[i,:]]
# This should be good
assert np.all(out == np.array([[-0.56875081, -1.45400572, -0.05941365, -1.44391254],
[ 1.03986533, -1.08565425, 0.79506417, -2.46156966],
[ 0.15789466, -1.43396403, -1.54812358, -0.0666025 ]]))
Upvotes: 3
Views: 1802
Reputation: 25813
What you want is something like this:
import numpy as np
data = np.array([[ 0.4, -0.1, 2.1, -0.6, -1.5, -1.4, -0.3, -0.4],
[ 0.8, -2.5, -0.1, -1.1, 1. , -0.5, 0.6, -1.1],
[ 1.4, -1.4, 0.8, -1.5, -1.1, 0.2, -0.1, 0.3]])
expected = np.array([[-0.6, -1.5, -0.1, -1.4],
[ 1. , -1.1, 0.8, -2.5],
[ 0.2, -1.4, -1.5, -0.1]])
indI = np.array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2]])
indJ = np.array([[3, 4, 1, 5],
[4, 7, 0, 1],
[5, 1, 3, 6]])
out = data[indI, indJ]
assert np.all(out == expected)
Notice that indI
and indJ
are the same shape and that
out[i, j] == data[indI[i, j], indJ[i, j]]
for all i
and j
.
You might have noticed that that indI
is very repetitive . Because of numpy's broadcasting magic you can simply indI
to:
indI = np.array([[0],
[1],
[2]])
You can build this kind of indI
array a few different ways, here is my favorite:
a, b = indJ.shape
indI, _ = np.ogrid[:a, :0]
out = data[indI, indJ]
Upvotes: 3
Reputation: 113844
This can be easily done if we index into the raveled data
array:
out = data.ravel()[ind.ravel() + np.repeat(range(0, 8*ind.shape[0], 8), ind.shape[1])].reshape(ind.shape)
It might be easier to understand if it is broken down into three steps:
indices = ind.ravel() + np.repeat(range(0, 8*ind.shape[0], 8), ind.shape[1])
out = data.ravel()[indices]
out = out.reshape(ind.shape)
ind
has the information on the elements from data
that we want. Unfortunately, it is expressed in 2-D indices. The first line above converts these into indices
of the 1-D raveled data
. The second line above selects those elements out of the raveled array data
. The third line restores the 2-D shape to out
.
The 2-D indices represented by ind
is converted to indindices
has the indices
Upvotes: 5