liveBook · Manning

Zipf's law

As explained in Natural Language Processing in Action, Second Edition

Zipf's law is an empirical distribution law stating that the frequency of an item is approximately inversely proportional to its rank in a frequency-ordered list.

Zipf's law describes a recurring pattern in ranked data: item frequency decreases approximately with rank. On a log-log plot, frequencies often form a near-straight line, making deviations worth investigating. section 3.5

The pattern appears in word counts and in broader systems such as city populations, economic output, resource distribution, and social networks. In language processing, it explains why a few words dominate counts while many occur rarely. section 3.5

Apply the observation to language data by:

  • Plotting frequency against rank and investigating points that depart from the expected line. section 3.5

Zipf's law also motivates logarithmic scaling in TF–IDF. Applying logarithms to term frequency and inverse document frequency reduces the disproportionate effect of large count differences, producing more uniformly distributed scores. section 3.6.1

Figure 3.4 City population distribution27
figure

Compare the ranked distribution of US city populations with the same kind of frequency pattern found in word usage. section 3.5

>>> log_tf = log(term_occurences_in_doc) -\
...     log(num_terms_in_doc)                  #1
>>> log_log_idf = log(log(total_num_docs) -\
...     log(num_docs_containing_term))     #2
>>> log_tf_idf = log_tf + log_log_idf    #3

Inspect a compact implementation that applies logarithmic transformations to term frequency, document frequency, and their combined TF–IDF score. section 3.6.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains rank-frequency plots, cross-domain examples, outlier inspection, and logarithmic TF–IDF scaling without a formal derivation.
Examples
Uses US city populations, the Brown Corpus, ranked word counts, and contrasting cat and dog document frequencies.
What it assumes
Assumes counted tokens and ranked lists, while demonstrating corpus inspection and frequency counting with NLTK.
What you can do after
Apply it via ranked frequency plots and logarithmic scaling when interpreting word counts or constructing TF–IDF features.
How it connects
Connects word frequency with corpora, term frequency, inverse document frequency, TF–IDF, logarithms, and cosine similarity.

Related concepts

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.