简体   繁体   English

numpy ValueError 尝试相对于整数列表对 np.ndarray 列表进行排序时

[英]numpy ValueError when trying to sort list of np.ndarray with respect to list of ints

I am trying to sort a list of numpy arrays with respect to a list of integers in ascending order, the problem discussed in this post .我正在尝试根据升序排列的整数列表对 numpy arrays 列表进行排序,这个问题在这篇文章中讨论过。 Specifically, I am using the top rated solution from the post.具体来说,我正在使用帖子中评价最高的解决方案。

This first example produces the intended solution:第一个示例产生了预期的解决方案:

>>> x1 = [np.array([1,2,3]),np.array([4,5,6]),np.array([7,8,9])]
>>> y1 = [6, 10 , 4]
>>> y1_sorted, x1_sorted = zip(*sorted(zip(y1, x1)))
>>> y1_sorted, x1_sorted
((4, 6, 10), (array([7, 8, 9]), array([1, 2, 3]), array([4, 5, 6])))

However, this second example, with variables seemingly of the same type, produces this error:然而,这第二个例子,变量看似相同的类型,产生了这个错误:

>>> x2 = [np.array([1, 2, 3]),
...                   np.array([1, 3, 2]),
...                   np.array([2, 1, 3]),
...                   np.array([2, 3, 1]),
...                   np.array([3, 1, 2]),
...                   np.array([3, 2, 1])]
>>> y2 = [6,3,7,1,3,8]
>>> y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Would anyone be able to explain what is happening?谁能解释发生了什么? I am using numpy 1.20.3 with Python 3.8.12.我正在使用 numpy 1.20.3 和 Python 3.8.12。

So the line sorted(zip(y1, x1)) in the first part of the code seems to be sorting according to y1 .因此,代码第一部分中的sorted(zip(y1, x1))行似乎是根据y1进行排序的。

What you can do is use the the key argument of sorted to replicate that behaviour您可以做的是使用sortedkey参数来复制该行为

y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2), key=lambda _: _[0]))
print(y2_sorted)
# (1, 3, 3, 6, 7, 8)
print(x2_sorted)
# (array([2, 3, 1, 4, 5, 6]), array([1, 3, 2, 4, 5, 6]), array([3, 1, 2, 4, 5, 6]), array([1, 2, 3, 4, 5, 6]), array([2, 1, 3, 4, 5, 6]), array([3, 2, 1, 4, 5, 6]))

sorted function by default sorts tuples by the first elements and if there is a tie there, sort by second elements, and if there is still a tie, sort by third elements and so on. sorted function 默认情况下按第一个元素对元组进行排序,如果存在平局,则按第二个元素排序,如果仍然存在平局,则按第三个元素排序,依此类推。

In y2 , 3 appears twice, so sorted will look into the second elements of the tuples to sort but the second elements are arrays, so it's not clear how to sort them, so you get an error.y2中, 3 出现了两次,因此sorted将查看要排序的元组的第二个元素,但第二个元素是 arrays,因此不清楚如何对它们进行排序,因此会出现错误。 In other words, it's as if you ran the following:换句话说,就好像您运行了以下命令:

y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2), key=lambda x: (x[0], x[1])))

One way you can still use sorted function here is to simply sort by the first element as @niko suggested:您仍然可以在这里使用sorted的 function 的一种方法是按照@niko 的建议简单地按第一个元素排序:

y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2), key=lambda x: x[0]))

In this case, you only sort by the first elements (ie sort by y2 ) and leave the sorting of ties in y2 to the order it appears in.在这种情况下,您只需按第一个元素排序(即按y2排序),并将y2中的关系排序保留为它出现的顺序。

Another way is to explicitly state how to use the information from the np.arrays .另一种方法是明确 state 如何使用来自np.arrays的信息。 Maybe you want to sort by the first elements in the arrays in case there are ties in y2 :也许您想按 arrays 中的第一个元素排序,以防y2中存在联系:

y2_sorted, x2_sorted = zip(*sorted(zip(y2, x2), key=lambda x: (x[0], x[1][0])))

Lastly, since you have a list of np.arrays , you can use numpy.argsort instead:最后,由于您有一个np.arrays列表,您可以使用numpy.argsort代替:

x2_sorted = np.array(x2)[np.argsort(y2)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM