如何使argsort结果在相等的值之间是随机的? [英] how to make argsort result to be random between equal values?

查看:83
本文介绍了如何使argsort结果在相等的值之间是随机的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个numpy向量[0,3,1,1,1],然后运行argsort 您会得到[0,2,3,4,1],但是所有的都是一样的! 我想要的是一种有效的方法,可以对相同值的索引进行混洗. 知道如何在不排序的向量上有两个索引的while循环的情况下做到这一点吗?

Say you have a numpy vector [0,3,1,1,1] and you run argsort you will get [0,2,3,4,1] but all the ones are the same! What I want is an efficient way to shuffle indices of identical values. Any idea how to do that without a while loop with two indices on the sorted vector?

numpy.array([0,3,1,1,1]).argsort()

推荐答案

使用lexsort: np.lexsort((b,a))表示按a,然后按b

Use lexsort: np.lexsort((b,a)) means Sort by a, then by b

>>> a
array([0, 3, 1, 1, 1])
>>> b=np.random.random(a.size)
>>> b
array([ 0.00673736,  0.90089115,  0.31407214,  0.24299867,  0.7223546 ])
>>> np.lexsort((b,a))
array([0, 3, 2, 4, 1])
>>> a.argsort()
array([0, 2, 3, 4, 1])
>>> a[[0, 3, 2, 4, 1]]
array([0, 1, 1, 1, 3])
>>> a[[0, 2, 3, 4, 1]]
array([0, 1, 1, 1, 3])

这篇关于如何使argsort结果在相等的值之间是随机的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆