在numpy向量中找到最频繁的数字 [英] Find the most frequent number in a numpy vector

查看:110
本文介绍了在numpy向量中找到最频繁的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在python中有以下列表:

Suppose I have the following list in python:

a = [1,2,3,1,2,1,1,1,3,2,2,1]

如何以一种简洁的方式在此列表中找到最频繁的电话号码?

How to find the most frequent number in this list in a neat way?

推荐答案

如果您的列表包含所有非负整数,则应查看numpy.bincounts:

If your list contains all non-negative ints, you should take a look at numpy.bincounts:

http://docs.scipy.org/doc/numpy/reference/produced /numpy.bincount.html

然后可能使用np.argmax:

and then probably use np.argmax:

a = np.array([1,2,3,1,2,1,1,1,3,2,2,1])
counts = np.bincount(a)
print np.argmax(counts)

对于更复杂的列表(可能包含负数或非整数值),可以类似的方式使用np.histogram.另外,如果您只想在python中工作而不使用numpy,则collections.Counter是处理此类数据的好方法.

For a more complicated list (that perhaps contains negative numbers or non-integer values), you can use np.histogram in a similar way. Alternatively, if you just want to work in python without using numpy, collections.Counter is a good way of handling this sort of data.

from collections import Counter
a = [1,2,3,1,2,1,1,1,3,2,2,1]
b = Counter(a)
print b.most_common(1)

这篇关于在numpy向量中找到最频繁的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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