加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

numpy.bincount正确理解

发布时间:2020-12-14 04:31:35 所属栏目:大数据 来源:网络整理
导读:? 今天看了个方法,numpy.bincount首先官网文档: numpy.bincount numpy. bincount ( x ,? weights=None ,? minlength=0 ) Count number of occurrences of each value in array of non-negative ints. The number of bins (of size 1) is one larger than t

? 今天看了个方法,numpy.bincount首先官网文档:

numpy.bincount

numpy. bincount (x,?weights=None,?minlength=0)

Count number of occurrences of each value in array of non-negative ints.

The number of bins (of size 1) is one larger than the largest value in?x. If?minlength?is specified,there will be at least this number of bins in the output array (though it will be longer if necessary,depending on the contents of?x). Each bin gives the number of occurrences of its index value in?x. If?weights?is specified the input array is weighted by it,i.e. if a value?n?is found at position?i,?out[n]?+=?weight[i]?instead of?out[n]?+=?1.

Parameters:
x? :?array_like,1 dimension,nonnegative ints

Input array.

weights? :?array_like,optional

Weights,array of the same shape as?x.

minlength? :?int,optional

A minimum number of bins for the output array.

New in version 1.6.0.

Returns:
out? :?ndarray of ints

The result of binning the input array. The length of?out?is equal to?np.amax(x)+1.

Raises:
ValueError

If the input is not 1-dimensional,or contains elements with negative values,or if?minlength?is negative.

TypeError

If the type of the input is float or complex.

?

大意思是:? 一脸懵逼好吧,统计bin在x出现的次数,what is bin?长度是x中最大值的+1,也看了一些博客,有些是这样写的:

?

仔细看了看这个例子,还是没看懂,难道是我智商有问题?索引值0->7? 哪里看出来的?官网文档也没说呀

索引0出现1次?x的索引0上数字是0,缺失出现了1次,1出现3次对的,后面索引2也是1应该也是3呀 什么鬼?怎么结果是1???

最终,我发现了到底是怎么统计的了,这个函数要求x里面的数字是非负正数是有道理的,

函数返回的是固定的array[0,1,2,3,...,N]这个数据对应数字在该数组x中出现的次数。

那么我们来验证一下经典的例子:

x = np.array([0,1,3,2,7])

第一个是看0在x中出现几次,1次;

1出现几次,3次;

2出现几次,1次;

3出现几次,1次;

4出现几次,0次;

5出现几次,0次;

6出现几次,0次;

7出现几次,1次

结果:

np.bincount(x)

array([0,1])

完美,再试一个?

也是对的,为啥要长度的最大值加一呢,显然是这里从0开始计算出现次数的,所以最后一个是最大值,数组长度要加1了。

?另外一个验证方式,z的数字求和肯定和np.bincount(z).dot(np.arange(max(z)+1))相等

至于其它参数就不介绍了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读