Slice numpy array wth list of wanted rows
Use take()
:
In [87]: m = np.random.random((6, 2))
In [88]: m
Out[88]:
array([[ 0.6641412 , 0.31556053],
[ 0.11480163, 0.00143887],
[ 0.4677745 , 0.43055324],
[ 0.49749099, 0.15678506],
[ 0.48024596, 0.65701218],
[ 0.48952677, 0.97089177]])
In [89]: m.take([0, 2, 5], axis=0)
Out[89]:
array([[ 0.6641412 , 0.31556053],
[ 0.4677745 , 0.43055324],
[ 0.48952677, 0.97089177]])
You can pass a list or an array as indexes to any np array.
>>> r = np.random.randint(0,10,(5,5))
>>> r
array([[3, 8, 9, 8, 4],
[4, 1, 5, 9, 1],
[3, 6, 8, 8, 0],
[5, 1, 7, 6, 1],
[6, 1, 7, 7, 7]])
>>> idx = [0,3,1]
>>> r[idx]
array([[3, 8, 9, 8, 4],
[5, 1, 7, 6, 1],
[4, 1, 5, 9, 1]])