Unit 5: Data analysis · Data tools

Article

Finding patterns: filter, sort and chart

8 min read

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:

dayshoptemp_cdrinks_sold
MonNorth2230
TueNorth2752
WedNorth3171
ThuNorth3490
MonSouth2241
TueSouth2760
WedSouth3183
ThuSouth34104

Programs process a table with four main tools:

ToolWhat it doesExample on the tableResult
Filterkeeps only the records that pass a testshop is North4 records
Sortputs the records in order by a fieldbiggest drinks_sold firstThu South (104) is first
Group and combinejoins records that share a value into one resultadd up drinks_sold for each shopNorth 243, South 288
Transformchanges every value, or makes a new field from old onesdrinks_sold × 20 gives income600, 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:

AP pseudocode
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":

Runs in your browser

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:

ChartBest forExample
Bar chartcomparing amounts in different categoriestotal drinks for North and South
Line charthow a value changes over timedrinks sold each day
Scatter plotthe link between two numbers, one dot per recordtemp_c against drinks_sold
Pie chartthe parts that make up a wholeeach 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_c

Look 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

  1. Ask a question

    question
  2. Process the data

    filter, sort, group, transform

    result
  3. Look at the result

    a table or a chart

    new idea
  4. Refine the question

    then go back to the first step

Each round is quick because the program does the work, so you can try many questions.

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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.