admin管理员组文章数量:1025259
I have an array of indexes:
test_idxs = np.array([4, 2, 7, 5])
I also have an array of values (which is longer):
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
So I want to get an array with the length of the array of indexes, but with values from the array of values in the order of the array of indexes. In other words, I want to get something like this:
array([21, 31, 131, 45])
I know how to do this in a loop, but I don't know how to achieve this using Numpy tools.
I have an array of indexes:
test_idxs = np.array([4, 2, 7, 5])
I also have an array of values (which is longer):
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
So I want to get an array with the length of the array of indexes, but with values from the array of values in the order of the array of indexes. In other words, I want to get something like this:
array([21, 31, 131, 45])
I know how to do this in a loop, but I don't know how to achieve this using Numpy tools.
Share Improve this question edited Nov 18, 2024 at 12:39 simon 5,6551 gold badge16 silver badges29 bronze badges asked Nov 18, 2024 at 12:11 IzaeDAIzaeDA 3976 bronze badges1 Answer
Reset to default 1This is actually extremely simple with numpy, just index your test_vals
array with test_idx
(integer array indexing):
out = test_vals[test_idxs]
Output:
array([ 21, 31, 131, 45])
Note that this requires the indices to be valid. If you have indices that could be too high you would need to handle them explicitly.
Example:
test_idxs = np.array([4, 2, 9, 5])
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
out = np.where(test_idxs < len(test_vals),
test_vals[np.clip(test_idxs, 0, len(test_vals)-1)],
np.nan)
Output:
array([21., 31., nan, 45.])
I have an array of indexes:
test_idxs = np.array([4, 2, 7, 5])
I also have an array of values (which is longer):
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
So I want to get an array with the length of the array of indexes, but with values from the array of values in the order of the array of indexes. In other words, I want to get something like this:
array([21, 31, 131, 45])
I know how to do this in a loop, but I don't know how to achieve this using Numpy tools.
I have an array of indexes:
test_idxs = np.array([4, 2, 7, 5])
I also have an array of values (which is longer):
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
So I want to get an array with the length of the array of indexes, but with values from the array of values in the order of the array of indexes. In other words, I want to get something like this:
array([21, 31, 131, 45])
I know how to do this in a loop, but I don't know how to achieve this using Numpy tools.
Share Improve this question edited Nov 18, 2024 at 12:39 simon 5,6551 gold badge16 silver badges29 bronze badges asked Nov 18, 2024 at 12:11 IzaeDAIzaeDA 3976 bronze badges1 Answer
Reset to default 1This is actually extremely simple with numpy, just index your test_vals
array with test_idx
(integer array indexing):
out = test_vals[test_idxs]
Output:
array([ 21, 31, 131, 45])
Note that this requires the indices to be valid. If you have indices that could be too high you would need to handle them explicitly.
Example:
test_idxs = np.array([4, 2, 9, 5])
test_vals = np.array([13, 19, 31, 6, 21, 45, 98, 131, 11])
out = np.where(test_idxs < len(test_vals),
test_vals[np.clip(test_idxs, 0, len(test_vals)-1)],
np.nan)
Output:
array([21., 31., nan, 45.])
本文标签: pythonReorder Numpy array by given index listStack Overflow
版权声明:本文标题:python - Reorder Numpy array by given index list - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619124a2159469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论