在Python中合并频率分布 [英] Binning frequency distribution in Python

查看:68
本文介绍了在Python中合并频率分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个列表 value freq 中都有数据,如下所示:

I have data in the two lists value and freq like this:


value freq
1      2
2      1
3      3
6      2
7      3
8      3
....

我希望输出为


bin freq
1-3   6
4-6   2
7-9   6
...

我可以写几行代码来做到这一点.但是,我在寻找标准python还是Numpy中是否有builitin函数?当您重复获得数组/列表中的数据(即它们尚未分组到频率表中)时,我找到了解决方案(例如 d= [1,1,2,3,3,3,6,6,7,7,7,8,8,8,...].)但是,在这种情况下,我找不到答案.我不想转换我的数据首先放入d之类的单个扩展列表中,然后使用直方图功能.

I can write few lines of code to do this. However, I am looking if there are builitin functions in standard python or Numpy? I found the solution when you are given data in array/list with repetition i.e. they are not already grouped into frequency table(eg. d= [1,1,2,3,3,3,6,6,7,7,7,8,8,8,...]. However, in this case I could not find the answers. I do not want to convert my data into single expanded list like d first and use histogram function.

推荐答案

import numpy as np
values = [1,2,3,6,7,8]
freqs = [2,1,3,2,3,3]

hist, _ = np.histogram(values, bins=[1, 4, 7, 10], weights=freqs)
print hist

输出:

[6 2 6]

这篇关于在Python中合并频率分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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