我是靠谱客的博主 无情香水,这篇文章主要介绍Numpy:np.bincount()的用法1. np.bincount(x)2. np.bincount(x,weights=w)3.np.bincount(x,weights=w,minlength=7),现在分享给大家,希望可以做个参考。
1. np.bincount(x)
复制代码
1
2
3x = np.array([1, 2, 3, 3, 0, 9, 4]) np.bincount(x)
输出:
array([1, 1, 1, 2, 1, 0, 0, 0, 0, 1], dtype=int64)
统计数值出现次数:数值0出现1次,1出现1次,2出现1次,3出现2次,4出现1次,5-8出现0次,9出现1次。
2. np.bincount(x,weights=w)
复制代码
1
2
3
4x = np.array([1, 2, 3, 3, 0, 9, 4]) w = np.array([0.3,0.5,0.7,0.6,0.1,-0.9,1]) np.bincount(x,weights=w)
输出:
array([ 0.1, 0.3, 0.5, 1.3, 1. , 0. , 0. , 0. , 0. , -0.9])
数字 0 出现在x中index=4位置,那么在w中访问index=4的位置即可,w[4]=0.1。
数字 3 出现在x中index=2与index=3位置,那么在w中访问index=2与index=3的位置即可,然后将两这个加和,计算得:w[2]+w[3]=1.3。其余的按照上面的方法即可。
3.np.bincount(x,weights=w,minlength=7)
复制代码
1
2
3
4x = np.array([1, 2, 3, 3, 0, 9, 4]) w = np.array([0.3,0.5,0.7,0.6,0.1,-0.9,1]) np.bincount(x,weights=w,minlength=13)
输出:
array([ 0.1, 0.3, 0.5, 1.3, 1. , 0. , 0. , 0. , 0. , -0.9, 0. , 0. , 0. ])
上面知道,这个bin范围是0-9,数量为10,index从0到9,那么当minlength为13的时候,也就是总长为13,index从0到9,多了后面3位,直接补位为0即可。
最后
以上就是无情香水最近收集整理的关于Numpy:np.bincount()的用法1. np.bincount(x)2. np.bincount(x,weights=w)3.np.bincount(x,weights=w,minlength=7)的全部内容,更多相关Numpy:np.bincount()的用法1.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复