liveBook · Manning

Relative frequency

As explained in Natural Language Processing in Action, Second Edition

Relative frequency is the proportion of a document's token positions occupied by a specified term.

Relative frequency measures how much of a document’s token stream belongs to a specified term. Divide the term’s count by the document’s total token count to obtain a fraction or percentage, making comparisons less dependent on document length. section 3.1

Measuring “justice”

In a 23-token sentence, “justice” appears once, so its relative frequency is 1 ÷ 23, or about 4%. That score reflects occurrence share, not necessarily the term’s importance. section 3.1

>>> len(counts)    #1
18
>>> counts.sum()
23
>>> len(tokens)         #2
23
>>> counts / counts.sum()    #3
,              0.217391
and            0.086957
It             0.043478
has            0.043478
also           0.043478
...

Inspect token counts, verify that their sum matches the token total, and divide the counts by that total with pandas. section 3.1

Because common words can dominate these percentages while carrying little meaning, relative frequency serves as an intermediate normalization before broader document statistics provide term significance. section 3.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines the calculation and demonstrates it in Python, then stops short of explaining the later significance-weighting step.
Examples
Calculates “justice” at about 4% in a 23-token sentence and inspects counts with Python collections and pandas.
What it assumes
Assumes token counts and document boundaries, while introducing normalization from raw counts.
What you can do after
Apply relative frequency by dividing a term count by the document’s total token count.
How it connects
Connects term frequency, stop words, bag-of-words vectors, and TF-IDF.

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.