他们有什么方法可以在 numpy ndarray 中找到最常见的字符串元素吗?
A= numpy.array(['a','b','c']['d','d','e']])
result should be 'd'
他们有什么方法可以在 numpy ndarray 中找到最常见的字符串元素吗?
A= numpy.array(['a','b','c']['d','d','e']])
result should be 'd'
如果你想要一个 numpy 的答案,你可以使用np.unique
:
>>> unique,pos = np.unique(A,return_inverse=True) #Finds all unique elements and their positions
>>> counts = np.bincount(pos) #Count the number of each unique element
>>> maxpos = counts.argmax() #Finds the positions of the maximum count
>>> (unique[maxpos],counts[maxpos])
('d', 2)
虽然如果有两个相同计数的元素,这将简单地从unique
数组中获取第一个。
有了这个,您还可以轻松地按元素计数排序,如下所示:
>>> maxsort = counts.argsort()[::-1]
>>> (unique[maxsort],counts[maxsort])
(array(['d', 'e', 'c', 'b', 'a'],
dtype='|S1'), array([2, 1, 1, 1, 1]))
这是一种方法:
>>> import numpy
>>> from collections import Counter
>>> A = numpy.array([['a','b','c'],['d','d','e']])
>>> Counter(A.flat).most_common(1)
[('d', 2)]
提取的'd'
留给读者作为练习。