Article
Finding patterns: filter, sort and chart
A streaming service keeps millions of records: who watched what, and when. Nobody can read them all. A program turns them into one useful sentence, such as "Most people watch at 9 p.m." How does it get from the records to the sentence? It processes the data in steps, then looks for patterns.
Four tools for processing data
Two shops sell cold drinks. For four days, each shop wrote down the temperature and how many drinks it sold:
| day | shop | temp_c | drinks_sold |
|---|---|---|---|
| Mon | North | 22 | 30 |
| Tue | North | 27 | 52 |
| Wed | North | 31 | 71 |
| Thu | North | 34 | 90 |
| Mon | South | 22 | 41 |
| Tue | South | 27 | 60 |
| Wed | South | 31 | 83 |
| Thu | South | 34 | 104 |
Programs process a table with four main tools:
| Tool | What it does | Example on the table | Result |
|---|---|---|---|
| Filter | keeps only the records that pass a test | shop is North | 4 records |
| Sort | puts the records in order by a field | biggest drinks_sold first | Thu South (104) is first |
| Group and combine | joins records that share a value into one result | add up drinks_sold for each shop | North 243, South 288 |
| Transform | changes every value, or makes a new field from old ones | drinks_sold × 20 gives income | 600, 1040, 1420, 1800, … |
Combine also means joining two data sets that share a field. A weather table and a sales table can be joined on day. Transform keeps the meaning. Changing 22 °C into 71.6 °F is the same temperature in new units.
Here is the filter in AP pseudocode. records is the table as a list of lists, and record[2] is the shop field:
north ← []
FOR EACH record IN records
{
IF(record[2] = "North")
{
APPEND(north, record)
}
}
DISPLAY(LENGTH(north))It displays 4. This is the "filter into a new list" pattern from Unit 3. The original records list is not changed. AP has no sort command, so the exam describes sorting in words. Python can do all four. Run it, then change the filter to "South":
Check your understanding
A table has the fields customer, item and amount. A program must find the total amount spent by each customer. Which tool does this?
Two more words appear on the exam. Clustering puts similar records together in groups. Classifying gives each record a label, such as "hot day" when temp_c is 30 or more.
Charts: seeing the pattern
A picture of the data is called a visualisation. The kind of chart depends on the question:
| Chart | Best for | Example |
|---|---|---|
| Bar chart | comparing amounts in different categories | total drinks for North and South |
| Line chart | how a value changes over time | drinks sold each day |
| Scatter plot | the link between two numbers, one dot per record | temp_c against drinks_sold |
| Pie chart | the parts that make up a whole | each shop's share of all drinks |
This is a scatter plot of the eight records. Each * is one record:
drinks_sold
100-119 | *
80-99 | * *
60-79 | * *
40-59 | * *
20-39 | *
+---------------------
22 27 31 34 temp_cLook at the shape. The dots climb from bottom left to top right. That is a trend: a general direction in the data. A value that sits far from the rest is an outlier. An outlier may be a mistake, or it may be the most interesting record, so check it before you delete it.
Correlation is not causation
The dots climb, so hotter days go with more drinks sold. Two things that change together have a correlation. It is positive when both go up together. It is negative when one goes up as the other goes down. There is no correlation when the dots show no shape.
Here, the heat probably does cause the extra sales. But a correlation alone does not prove that one thing causes the other. A town finds that on days with more ice cream sales, more people get sunburnt. Ice cream does not burn skin. A third thing, hot sunny weather, drives both. Other times it is a coincidence, or the cause runs the other way round. To prove a cause, you need more evidence, such as a controlled experiment.
Check your understanding
A study of many cities finds that cities with more libraries also have more coffee shops. Which conclusion is best supported?
Insight is a loop
Nobody gets the answer in one step. People and programs take turns. You ask a question, the program processes the data, you look at the result, and the result gives you a better question.
How insight grows
Ask a question
questionProcess the data
filter, sort, group, transform
resultLook at the result
a table or a chart
new ideaRefine the question
then go back to the first step
For the shops: "Which shop sold more?" gives South. "Is that true every day?" Yes, by 8 to 14 drinks. "Why?" Now you need new data, such as the shop's position, that this table does not have. Some questions cannot be answered until you collect more data.
Common mistakes
- Reading a correlation as a cause. "Both rise together" is not "one makes the other rise".
- Thinking a filter or sort changes the data. They give a new view. The records stay the same.
- Choosing the wrong chart. A pie chart cannot show change over time.
- Deleting an outlier without checking. It may be a real, important value.
- Joining data sets with different units. Convert first, then combine.
- Stopping at the first answer. A good analysis asks a second question.
On the exam
You may be asked which tool answers a question (filter to keep some records, group to total by category), or what a chart or table shows. A favourite is "A study finds a correlation… Which conclusion is best supported?" The safe answer says the two are linked, never that one causes the other. Questions also ask which data you would need to answer a new question.
Key words
Filter: keep only the records that pass a test.
Sort: put the records in order by one field.
Transform: change every value, or make a new field from old ones.
Visualisation: a picture of data, such as a chart.
Trend: a general direction in data, such as rising over time.
Correlation: two things changing together.
Causation: one thing making another happen.