[英]How can I retrieve values from a list of numpy arrays with a list of indices?
I have a list of numpy array indices which I created with argsort()
:我有一个使用
argsort()
创建的 numpy 数组索引列表:
i =
[array([0, 1, 3, 2, 4], dtype=int64),
array([1, 3, 0, 2, 4], dtype=int64),
array([2, 4, 0, 1, 3], dtype=int64),
array([3, 1, 0, 2, 4], dtype=int64),
array([4, 2, 0, 3, 1], dtype=int64)]
This is the corresponding list of arrays with values:这是 arrays 的对应列表,其值为:
v =
[array([0. , 0.19648367, 0.24237755, 0.200832 , 0.28600039]),
array([0.19648367, 0. , 0.25492185, 0.15594099, 0.31378135]),
array([0.24237755, 0.25492185, 0. , 0.25685254, 0.2042604 ]),
array([0.200832 , 0.15594099, 0.25685254, 0. , 0.29995309]),
array([0.28600039, 0.31378135, 0.2042604 , 0.29995309, 0. ])]
When I try to loop over the lists like this:当我尝试像这样遍历列表时:
for line in i:
v[line]
I get the error:我得到错误:
TypeError: only integer scalar arrays can be converted to a scalar index
But when I try to access them individually like this:但是当我尝试像这样单独访问它们时:
v[0][i[0]]
It works and outputs the values in v[0] in correct order like this:它以正确的顺序工作并输出 v[0] 中的值,如下所示:
array([0. , 0.19648367, 0.200832 , 0.24237755, 0.28600039])
I want the arrays in v
ordered from the smallest value to biggest.我想要
v
中的 arrays 从最小值到最大值排序。 What am I doing wrong?我究竟做错了什么?
Loop through each line of i, and loop through each line of v at the same time using enumerate:循环遍历 i 的每一行,同时使用 enumerate 循环遍历 v 的每一行:
import numpy as np
i = np.array([[0, 1, 3, 2, 4], [1, 3, 0, 2, 4], [2, 4, 0, 1, 3], [3, 1, 0, 2, 4], [4, 2, 0, 3, 1]])
v = np.array([[0. , 0.19648367, 0.24237755, 0.200832 , 0.28600039],
[0.19648367, 0. , 0.25492185, 0.15594099, 0.31378135],
[0.24237755, 0.25492185, 0. , 0.25685254, 0.2042604 ],
[0.200832 , 0.15594099, 0.25685254, 0. , 0.29995309],
[0.28600039, 0.31378135, 0.2042604 , 0.29995309, 0. ]] )
# you can rearrange each line of v by using indices in each row of i
for index, line in enumerate(i):
print(v[index][line])
Output: Output:
[0. 0.19648367 0.200832 0.24237755 0.28600039]
[0. 0.15594099 0.19648367 0.25492185 0.31378135]
[0. 0.2042604 0.24237755 0.25492185 0.25685254]
[0. 0.15594099 0.200832 0.25685254 0.29995309]
[0. 0.2042604 0.28600039 0.29995309 0.31378135]
This is all easier (and faster) if you don't use a python list of Numpy arrays, but instead use a multi-dimensional numpy array.如果您不使用 Numpy arrays 的 python 列表,而是使用多维 Z29EA9510C357FF627 数组,这一切都更容易(也更快)。 Then you have all the great tool from numpy at you disposal and can avoid slow loops.
然后,您可以使用 numpy 的所有出色工具,并且可以避免慢循环。 For example for you can use
np.take_along_axis
:例如,您可以使用
np.take_along_axis
:
from numpy import array
i = np.array([
[0, 1, 3, 2, 4],
[1, 3, 0, 2, 4],
[2, 4, 0, 1, 3],
[3, 1, 0, 2, 4],
[4, 2, 0, 3, 1]])
v = array([
[0., 0.19648367, 0.24237755, 0.200832 , 0.28600039],
[0.19648367, 0. , 0.25492185, 0.15594099, 0.31378135],
[0.24237755, 0.25492185, 0. , 0.25685254, 0.2042604 ],
[0.200832 , 0.15594099, 0.25685254, 0. , 0.29995309],
[0.28600039, 0.31378135, 0.2042604 , 0.29995309, 0. ]]
)
np.take_along_axis(v,i, 1)
result:结果:
array([[0. , 0.19648367, 0.200832 , 0.24237755, 0.28600039],
[0. , 0.15594099, 0.19648367, 0.25492185, 0.31378135],
[0. , 0.2042604 , 0.24237755, 0.25492185, 0.25685254],
[0. , 0.15594099, 0.200832 , 0.25685254, 0.29995309],
[0. , 0.2042604 , 0.28600039, 0.29995309, 0.31378135]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.