Lecture 15: Zipf's Law
Overview
Professor Rosen uses an English word-frequency dataset of 5,000 words to teach file parsing, lists, slicing, logarithms, and graphing while investigating Zipf's law: word frequency falls roughly in inverse proportion to rank, producing a log-log slope near -1. The examples connect frequency patterns to SETI signal detection and show how Python tools such as `sum`, `math.log`, and Altair can reveal patterns that are difficult to see on a linear graph.
Key takeaways
- In the English corpus of roughly 329 million word occurrences, the top ten words account for about 27 percent of usage, while the bottom ten contribute only a tiny fraction.
- Zipf's law becomes visually apparent when both word rank and frequency are plotted logarithmically, yielding a line with a slope near -1.
- Frequency alone can be misleading: dispersion reveals whether a word is common across many genres or concentrated in fields such as legal writing.
- Python's `sum`, slicing, and parallel lists provide a concise way to analyze a 5,000-row CSV without manually looping through every calculation.
- SETI researchers can look for structured statistical patterns, including Zipf-like frequency distributions, as evidence that a signal may be communication rather than random noise.
Chapters
- Professor Rosen plans to coordinate exam grading with sick TAs over Zoom and merge Canvas and Runestone gradebooks using a Python script.
- The merge script matches students by name or email and uses dictionaries as key-value lookups, a topic scheduled to begin Thursday.
- Labs 4 and 5 are due by Monday under the week-eight deadline; students with missing demos should attend office hours, and zeros may be entered as a reminder.
- Project 11, Exploring Common Words in SETI, asks how researchers could distinguish communication from random noise without knowing an extraterrestrial language.
- Zipf's law describes a strongly uneven word distribution: a few words are very common while many are rare.
- Professor Rosen notes that similar frequency patterns have been observed in human language and communication signals from bottlenose dolphins, humpback whales, and squirrel monkeys.
- The dataset contains 5,000 common English words and five fields: rank, word, part of speech, frequency, and dispersion.
- Frequency counts how often a word occurs, while dispersion indicates how evenly it appears across the texts in the corpus.
- The example word "re" has high frequency but low dispersion because it is concentrated in legal writing rather than broadly distributed across genres.
- The program opens the CSV, reads its lines, and uses `split(',')` to separate each row into fields.
- Five lists, `rank`, `word`, `part`, `frequency`, and `dispersion`, store the corresponding values by column.
- The header row must be skipped before processing data, and each split field is appended to its matching list.
- The rank and frequency fields are converted with `int`, while dispersion is converted with `float`; converting the header would fail, so it is excluded.
- A comic about the word "sustainable" illustrates how extrapolating a rising frequency trend can produce absurd predictions.
- Zipf's law predicts an approximate inverse relationship between a word's rank and its frequency, though individual rankings do not match perfectly.
- The frequency list sums to about 329 million word occurrences in the corpus.
- Python's `sum` and list slicing calculate the top ten words' combined share without writing a loop; the result is about 27 percent.
- Negative indexing selects the bottom ten entries, whose combined usage is tiny compared with the most frequent words.
- The lowest-ranked examples include terms such as "plaintiff," "middle class," "laundry," and "stereotype," which are less common or concentrated in particular genres.
- Dispersion helps distinguish a word's overall count from how broadly it appears across texts; "plaintiff" is associated particularly with legal writing and news.
- Higher-ranked entries such as "school," "call," and "try" illustrate the more general vocabulary near the common end of the list.
- Professor Rosen explains log base 2 as the number of repeated halvings: 64 can be halved six times to reach 1, so log2(64) is 6.
- The corresponding base-10 example divides 1,000 into groups of ten three times, illustrating why log10(1,000) is 3.
- Logarithms are useful in computer science runtimes such as n log n and in data displays spanning very different magnitudes.
- The example uses Altair to build a chart from rank and frequency data, with rank on the x-axis and frequency on the y-axis.
- Professor Rosen contrasts Altair with Matplotlib, the graphing library he uses for scientific publications.
- On linear axes, the frequency curve drops sharply at the start, making differences among the many less-common words difficult to inspect.
- Applying `math.log` to both rank and frequency compresses their ranges and turns the word-frequency curve into an approximately straight line.
- The observed slope is close to -1, the pattern expected under Zipf's law.
- Changing the logarithm base from 10 to 2 changes the axis scale but does not substantially alter the plotted relationship.
- The exercise searches for words whose reversals also appear in the corpus, using reverse slicing with `word[::-1]`.
- A membership test checks whether each reversed word appears in the word list; examples include "pot" and "top," and "live" and "evil."
- The results also include palindromes, which match their own reversals, and pairs such as "part" and "trap."
- Students who have not submitted Labs 4 and 5 are asked to finish them and arrange demos during Tuesday or Thursday office hours.
- Professor Rosen warns that zeros may appear for undemonstrated assignments, but students can still seek credit by completing the required demo.
- The two-week demo limit is described as mainly relevant near semester's end, with exceptions possible for students who have been attending.
Transcript excerpt
0:00 So, let's go ahead and get this lecture started. One sec. There we go. Okay. So, next what I need to do I should have gone into Canvas. Yes, we did. Canvas introduction to problem solving modules week eight. So, the goal is I'm going to meet So, some of our TAs have reported sick. So, I'm going to meet with them over Zoom either tonight or tomorrow in order to get your exam graded. Uh, your TA what's going to happen is because of again because some of them are sick, I'd rather not get into a room together uh like I normally do. Um, so I'm just going to meet them over Zoom and give
0:47 them directions on how to grade a lot of the material even though a lot of the material a lot of the questions are fairly simple to grade because they were multiple choice, right? Um, so let's see. I need to just change the link for today for module 8. We're still going to be working on um file reading today. On Thursday, we're going to start on dictionaries. Okay. So, and then with that will continue on next week until we get and then next and then the Thursday after that we'll start using dictionaries on our um on on something I really appreciate called Monte Carlo approximations. They're
1:36 um part of my effort for this class being accessible to everybody. um which is that I I know that math is not necessarily what you want to do with your life and so why do the hard math when you can make the computer do the math for you right uh with the runstone stuff I've gone through and I've started re uh running the grader to make sure that I can import it uh tonight into your um
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.