Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy.基于计数的唯一排序_Python_Python 3.x_Sorting_Numpy_Unique - Fatal编程技术网

Python numpy.基于计数的唯一排序

Python numpy.基于计数的唯一排序,python,python-3.x,sorting,numpy,unique,Python,Python 3.x,Sorting,Numpy,Unique,如果return\u counts为True,则numpy.unique函数允许返回唯一元素的计数。现在,返回的元组由两个数组组成:一个包含唯一元素,另一个包含计数数组,这两个数组都按唯一元素排序。现在有没有一种方法可以根据计数数组而不是唯一的元素对这两个元素进行排序?我的意思是,我知道如何以艰难的方式做到这一点,但对于这种情况,是否有一些简洁的单行程序或lambda功能 当前结果: my_chr_list = ["a","a","a", "b", "c", "b","d", "d"] uniq

如果
return\u counts
True
,则
numpy.unique
函数允许返回唯一元素的计数。现在,返回的元组由两个数组组成:一个包含唯一元素,另一个包含计数数组,这两个数组都按唯一元素排序。现在有没有一种方法可以根据计数数组而不是唯一的元素对这两个元素进行排序?我的意思是,我知道如何以艰难的方式做到这一点,但对于这种情况,是否有一些简洁的单行程序或lambda功能

当前结果:

my_chr_list = ["a","a","a", "b", "c", "b","d", "d"]
unique_els, counts = np.unique(my_chr_list, return_counts=True)
print(unique_els, counts)
返回的内容如下所示:

>>> (array(['a', 'b', 'c', 'd'], 
     dtype='<U1'), array([3, 2, 1, 2], dtype=int64))
>(数组(['a','b','c','d']),

dtype='您不能直接使用
unique
函数来实现这一点。相反,作为一种Numpythonic方法,您可以使用
return\u index
关键字来获取唯一项的索引,然后使用
np.argsort
来获取排序后的
count
项的索引,并根据其频率使用结果来查找项。

In [33]: arr = np.array(my_chr_list)

In [34]: u, count = np.unique(my_chr_list, return_counts=True)

In [35]: count_sort_ind = np.argsort(-count)

In [36]: u[count_sort_ind]
Out[36]: 
array(['a', 'b', 'd', 'c'], 
      dtype='<U1')

In [37]: count[count_sort_ind]
Out[37]: array([3, 2, 2, 1])
[33]中的
:arr=np.array(my\u chr\u列表)
在[34]中:u,count=np.unique(my\u chr\u list,return\u counts=True)
在[35]中:count\u sort\u ind=np.argsort(-count)
In[36]:u[count\u sort\u ind]
出[36]:
数组(['a','b','d','c'],

dtype='I reliese
-count
np.argsort(-count)中
是返回反向排序数组的索引。我在numpy文档中没有找到这条指令;用负数索引类似列表的数据结构倾向于反转返回的顺序,这是一条普遍的经验法则吗?提前谢谢。@SumanthLazarus不,文档中没有明确提到这一点。这就是全部取决于排序函数的工作方式。也就是说,它们对给定iterable(降序或升序)进行排序的方式,使用否定可以颠倒排序顺序。
In [33]: arr = np.array(my_chr_list)

In [34]: u, count = np.unique(my_chr_list, return_counts=True)

In [35]: count_sort_ind = np.argsort(-count)

In [36]: u[count_sort_ind]
Out[36]: 
array(['a', 'b', 'd', 'c'], 
      dtype='<U1')

In [37]: count[count_sort_ind]
Out[37]: array([3, 2, 2, 1])