带有浮点数的Numpy bincount() [英] Numpy bincount() with floats

查看:185
本文介绍了带有浮点数的Numpy bincount()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取浮点类型的numpy数组的bincount:

I am trying to get a bincount of a numpy array which is of the float type:

w = np.array([0.1, 0.2, 0.1, 0.3, 0.5])
print np.bincount(w)

如何将bincount()与浮点值而不是int一起使用?

How can you use bincount() with float values and not int?

推荐答案

您想要这样的东西吗?

>>> from collections import Counter
>>> w = np.array([0.1, 0.2, 0.1, 0.3, 0.5])
>>> c = Counter(w)

Counter({0.10000000000000001: 2, 0.5: 1, 0.29999999999999999: 1, 0.20000000000000001: 1})

或者更好的输出:

Counter({0.1: 2, 0.5: 1, 0.3: 1, 0.2: 1})

然后您可以对其进行排序并获取您的值:

You can then sort it and get your values:

>>> np.array([v for k,v in sorted(c.iteritems())])

array([2, 1, 1, 1])

bincount的输出对于浮点数没有意义:

The output of bincount wouldn't make sense with floats:

>>> np.bincount([10,11])
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

因为没有定义的浮点数序列.

as there is no defined sequence of floats.

这篇关于带有浮点数的Numpy bincount()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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