本文介绍了使用argsort后如何取回订单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个数组,例如[-0.7, -3.7, -2.1, -5.8, -1.2 ]这些特定的数字对应于按顺序排列的标签:比如0.7对应于标签201,3.7对应于标签202,依此类推.

So I have an array for example [-0.7, -3.7, -2.1, -5.8, -1.2 ]and these particular numbers correspond to labels which are in order: say 0.7 corresponds to label 201, 3.7 to label 202 and so on.

在对它们进行正常排序时,我收到了[-5.8,-3.7,-2.1,-1.2,-0.7].我有兴趣从中挑选出前三个值,但在排序时,我会失去对标签的跟踪.现在要对它们进行排序,请使用 np.argsort .这给了我[1,2,0].这告诉我4的值具有较低的概率,而0的值具有较高的概率.

On sorting them normally, I receive [-5.8, -3.7, -2.1, -1.2, -0.7]. I am interested in picking out the top 3 values out of these but on sorting, I would lose track of the labels.Now to sort them in order I use np.argsort. This gives me [1,2,0]. This tells me the value with 4 has a low probability while the one with 0 has a high probability.

我的问题是关于argsort的,我该如何找回映射?我怎么知道我的标签现在在哪里?使用argsort时是否可以跟踪它们?

My question is with argsort, how can I get my mappings back? How can I tell where my labels are now? Is there a way I can keep a track of them while using argsort?

推荐答案

这会复制并使用内置的排序方法,但我认为它可以实现您想要的.

This makes a copy and uses the built-in sorted method, but I think it achieves what you want.

vals = [-0.7, -3.7, -2.1, -5.8, -1.2 ]
label_inds_vals = sorted([thing for thing in enumerate(vals)], key=lambda x: x[1])

排序后的值还带有索引,可用于在标签数组中为其对应的标签建立索引.

The sorted values also come with indices that you can use to index their corresponding label in the label array.

如果列表列表:

value_lists = [[-0.7,-3.2,-2.1,-5.8,-1.2],[-1.2,-3.2,-3.4,-5.4,-6.4]]

value_lists = [[-0.7, -3.2, -2.1, -5.8, -1.2], [-1.2, -3.2, -3.4, -5.4, -6.4]]

for vals in value_lists:
    #reverse depending if you want top 3 or bottom
    label_inds_vals = sorted([thing for thing in enumerate(vals)], key=lambda x: x[1], reverse = True)
    print label_inds_vals[:3]

这篇关于使用argsort后如何取回订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 17:55