Binning frequency distribution in Python

女生的网名这么多〃 提交于 2019-12-10 15:48:28

问题


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
....

and I want the output to be

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

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.


回答1:


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

output:

[6 2 6]



回答2:


you can try this:

import collections
d=[1,1,2,3,3,3,6,6,7,7,7,8,8,8]
collections.Counter([i-i%3+3 for i in d])

it would generate a dictionary with what you want.




回答3:


I found the solution when you are given data in array/list with repetition

You didn't say what the solution was, but if it supports taking an iterator, you can generate it, instead of creating the entire list:

import itertools

values = [1,2,3,6]
freqs =  [2,1,3,2]

v_iter = itertools.chain(*[ itertools.repeat(v,f) for v, f in zip(values, freqs) ])

#for x in v_iter:
#    print x

your_solution(v_iter)


来源:https://stackoverflow.com/questions/15697350/binning-frequency-distribution-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!