How to save the relative frequencies of a data set as a list?


I have data that contains a list of over thousand values, such as:

[3.481, 2.413, 4.682,…]

and can plot the histogram of it easily. However, I want to save the relative frequencies (or probability of each value occurring) of those values as a list, and make sure the frequency adds up to one. I tried using

import numpy

numpy.histogram(data, density=None)

But it’s not giving me what I want as the sum doesn’t equal one. And setting density equal to ‘True’ will normalize such that the integral over the range is 1, but I want the sum to equal one. Any help would be greatly appreciated, I tried looking all over the place for a simple code.


This should do the job.

def rel_freq(x):
    freqs = [(x.count(value) / len(x)) for value in set(x)]
    return freqs

However, note that the sum will not come up to exactly 1 but something like 0.9999 and that is because of floating point error.

Answered By – prnvbn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published