Alpine
Alpine

Reputation: 563

Relative Frequency in Python

Is it possible to calculate the relative frequency of elements occurring in a list in Python?

For example:

['apple', 'banana', 'apple', 'orange'] # apple for example would be 0.5

Upvotes: 5

Views: 10393

Answers (5)

Ram
Ram

Reputation: 31

This simple code will do the job, returns a list of tuples but you can adapt it easily.

lst = ['apple', 'banana', 'apple', 'orange']
counts = [(word, lst.count(word) / len(lst)) for word in set(lst)] 

It will return the relative frequencies of each word as below:

[('orange', 0.25), ('banana', 0.25), ('apple', 0.5)]

Note that :

  1. iterate over set(lst) to avoid duplicates
  2. divide the lst.count by len(lst) to get relative frequencies

Upvotes: 3

pedjjj
pedjjj

Reputation: 1038

The following snippet does exactly what the question asks for: given a Counter() object, return a dict that contains the same keys but with relative frequencies as values. No third party library required.

def counter_to_relative(counter):
    total_count = sum(counter.values())
    relative = {}
    for key in counter:
        relative[key] = counter[key] / total_count
    return relative

Upvotes: 4

petabyte
petabyte

Reputation: 1567

You can do this pretty easily by just counting the number of times the element occurs in the list.

def relative_frequency(lst, element):
    return lst.count(element) / float(len(lst))

words = ['apple', 'banana', 'apple', 'orange']
print(relative_frequency(words, 'apple'))

Upvotes: 2

craighagerman
craighagerman

Reputation: 393

You can use NLTK for this:

import ntlk
text = ['apple', 'banana', 'apple', 'orange']
fd = nltk.FreqDist(text)

Check out the tutorial in the book the how to and the source code

Alternately, you could use a Counter:

from collections import Counter
text = ['apple', 'banana', 'apple', 'orange']
c = Counter(text)

Upvotes: 9

justanothercoder
justanothercoder

Reputation: 1890

Make a dictionary with words as keys, and times of occurence as values. After you have this dictionary you can divide each value by length of list of words.

Upvotes: 0

Related Questions