Here are my initial 2-D and 1-D arrays:
a = numpy.array([
[ 100, 2, 3, 4 ],
[ 200, 5, 6, 7 ],
[ 100, 8, 9, 10 ],
[ 100, 11, 12, 13 ],
[ 200, 14, 15, 16 ]
]
b = numpy.array([100, 200])
I would like to create a new 3-D array that looks like so:
[
[ [ 100, 2, 3, 4 ] , [ 100, 8, 9, 10 ] , [ 100, 11, 12, 13 ] ],
[ [ 200, 5, 6, 7 ] , [ 200, 14, 15, 16 ]
]
i.e. I want to generate a new 3-D array where each "row" (first dimension) corresponds to an element (bn
) in b
, and consists of an array of all elements from a
where a[:, 0 == bn]
.
I have managed to achieve this like so:
c = np.array([
a[a[:, 0] == bn]
for bn in b
])
But this is quite slow (working with large datasets), and I'm wondering if there's any way to achieve this using numpy
functionality instead of the for
loop.