Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binning frequency distribution in Python

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.

like image 238
DurgaDatta Avatar asked Mar 29 '13 04:03

DurgaDatta


1 Answers

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]
like image 182
HYRY Avatar answered Oct 20 '22 17:10

HYRY