Chapter 2. Distributions
This chapter introduces one of the most fundamental ideas in statistics, the distribution. We’ll start with frequency tables—which represent the values in a dataset and the number of times each of them appears—and use them to explore data from the National Survey of Family Growth (NSFG). We’ll also look for extreme or erroneous values, called outliers, and consider ways to handle them.
Frequency Tables
One way to describe a variable is a frequency table, which contains the values of the variable and their frequencies—that is, the number of times each value appears. This description is called the distribution of the variable.
To represent distributions, we’ll use a library called empiricaldist. In this context, “empirical” means that the distributions are based on data rather than mathematical models. empiricaldist provides a class called FreqTab that we can use to compute and plot frequency tables. We can import it like this:
fromempiricaldistimportFreqTab
To show how it works, we’ll start with a small list of values:
t=[1.0,2.0,2.0,3.0,5.0]
FreqTab provides a method called from_seq that takes a sequence and makes a FreqTab object:
ftab=FreqTab.from_seq(t)ftab
| freqs | |
|---|---|
| 1.0 | 1 |
| 2.0 | 2 |
| 3.0 | 1 |
| 5.0 | 1 |
A FreqTab object is a kind of Pandas Series that contains values and their frequencies. In this example, the value 1.0 corresponds to frequency 1, the value 2.0 corresponds to frequency 2, etc.
FreqTab provides a method called bar that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access