296*_*502 6 python numpy python-3.x
他们是否可以在numpy ndarray中找到最频繁的字符串元素?
A= numpy.array(['a','b','c']['d','d','e']])
result should be 'd'
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 11
如果你想要一个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)
Run Code Online (Sandbox Code Playgroud)
虽然如果有两个具有相同计数的元素,这将只是从unique数组中取第一个.
有了这个,您还可以轻松按元素排序,如下所示:
>>> maxsort = counts.argsort()[::-1]
>>> (unique[maxsort],counts[maxsort])
(array(['d', 'e', 'c', 'b', 'a'],
dtype='|S1'), array([2, 1, 1, 1, 1]))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6361 次 |
| 最近记录: |