Students continue to explore the Animals Dataset, and consider the kinds of questions that can be asked about a dataset. They also learn to define values, and to define functions using a structured approach to problem solving called the "Design Recipe". They then use these functions to filter the animals dataset, using methods.
Students define several row values from the animals table
Students define several functions over rows from the animals table
Standards and Evidence Statements:
Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.
Data 3.1.1: Use computers to process information, find patterns, and test hypotheses about digitally processed information to gain insight and knowledge. [P4]
Computers are used in an iterative and interactive way when processing digital information to gain insight and knowledge.
Digital information can be filtered and cleaned by using computers to process information.
BS-DR.1: The student is able to translate a word problem into a Contract and Purpose Statement
given a word problem, identify the domain and range of a function
given a word problem, write a Purpose Statement (i.e. - rewrite the problem in their own words)
BS-DR.2: The student can derive test cases for a given contract and purpose statement
given a Contract and a Purpose Statement, write multiple examples or test cases
BS-PL.3: The student is able to use the syntax of the programming language to define values and functions
defining and retrieving values
writing test cases
defining and using functions
Length: 90 Minutes
Glossary:
method: a function that is associated with particular object, such as a Table or Structure. It consumes inputs and produces an output based the object and those inputs
Materials:
Preparation:
Computer for each student (or pair), with access to the internet
Students learn about different categories of questions
Evidence Statementes
Product Outcomes
Materials
Preparation
Question Types(Time 10 minutes)
Question TypesOnce we have a dataset, we can start asking questions! But how do we know what questions to ask? There’s an art to asking the right questions, and good Data Scientists think hard about what kind of questions can and can’t be answered.
Turn to back to Page 3 in your Student Workbook, and look at the "Wonder" questions that you wrote about this dataset.
Have students brainstorm some questions they might ask of the animals table.
Most questions can be broken down into one of three categories:
Lookup questions - These can be answered simply by looking up a single value in the table and reading it out. Once you find the value, you’re done! Examples of lookup questions might be "is Sunflower fixed?" or "How many legs does Felix have?"
Compute questions - These can be answered by computing an answer across a single row or column. Examples of computing questions might be "how much does the heaviest animal weigh?" or "What is the average age of animals at the shelter?"
Relate questions - These ones take the most work, because they require looking for relationships between multiple columns. Examples of analysis questions might be "Do cats tend to be adopted faster than dogs?" or "Are older animals heavier than young ones?"
Have students come up with questions for each type.
Look back at the Wonders you wrote on Page 3. Are any of these Lookup, Compute, or Relate questions? Circle the question type that’s appropriate. Can you come up with additional examples for each type of question?
Have students share their questions with the class. Allow time for discussion!
Lookup questions are easy to answer: just find the right row and column, and read out the answer! Let’s learn how to do this in Pyret...
Row and Column Lookups
Overview
Learning Objectives
Students extend their understanding of function application
Evidence Statementes
Product Outcomes
Materials
Preparation
Row and Column Lookups(Time 15 minutes)
Row and Column LookupsOpen Animals Starter File - Unit 2, save a copy, and click "Run". This is the same program you saw before, but with one extra line of code. Which line is new, and what do you think it does?
Tables have special functions associated with them, called Methods, which allow us to do all sorts of things with those tables. For example, we can get the first data row in a table by using the .row-n method:
Note: data rows start at zero!
For practice, in the Interactions Area, use the row-n method to get the second and third data rows.
What is the Domain of .row-n? What is the Range? Find the contract for this method in your contracts table. A table method is a special kind of function which always operates on a specific table. In our example, we always use .row-n with the animals table, so the number we pass in is always used to grab a particular row from animals-table.
Pyret also has a way for us to get at individual columns of a Row, by using a Row Accessor. Row accessors start with a Row value, followed by square brackets and the name of the column where the value can be found. Here are three examples that use row accessors to get at different columns from the first row in the animals-table:
How would you get the weeks column out of the second row? The third?
Let’s get some practice playing with the row-n method, and row-accessors!
Tables have other methods too! The .order-by method consumes a String (the name of the column you want to order) and a Boolean (true for ascending, false for descending). Try typing the following expressions into the Interactions Area. What do you get?
What is the Domain of .order-by? What is the Range? Find the contract for this method in your contracts table, and make sure it makes sense!
Defining Values
Overview
Learning Objectives
Students learn about value definitions in Pyret
Evidence Statementes
Product Outcomes
Students define several row values from the animals table
Materials
Preparation
Computer for each student (or pair), with access to the internet
Defining ValuesPyret allows us to define names for values using the = sign. In math, you’re probably used to seeing definitions like , which defines the name x to be the value 4. Pyret works the same way, and you’ve already seen two names defined in this file: shelter-sheet and animals-table. We generally write definitions on the left, in the Definitions Area.
You can add your own definitions, for example:
With your partner, take turns adding definitions to this file:
Define a value with name food, whose value is a String representing your favorite food
Define a value with name year, whose value is a Number representing the current year
Define a value with name likes-cats, whose value is a Boolean that is true if you like cats and false if you don’t
Each row of our animals-table represents a single animal in our shelter. We can use the row-n method to define values. Type the following lines of code into the Definitions Area and click "Run":
What happens when you evaluate animalA in the Interactions Area?
Define at least two additional values to be animals from the animals-table, called animalC and animalD.
Let’s get some practice combining Lookups with Value Definitions.
Students learn how to define functions using the Design Recipe
Evidence Statementes
Product Outcomes
Students define several functions over rows from the animals table
Materials
Preparation
Defining Functions(Time 30 minutes)
Defining FunctionsSuppose you work at the animal shelter, taking care of all the animals who live there. You want to make sure they’re healthy, happy, and find good homes. For each animal, you might want to ask certain questions:
What kind of animal is it?
Has it been fixed?
When was it born?
Is it a kitten?
Have students brainstorm additional questions!
Let’s try finding all the fixed animals by hand. Turn to Page 2, and walk down the table one row at a time, putting a check next to each animal that is fixed.
Give students 2min to find all the fixed animals they can.
What did you do to complete this activity? You went through the table one row at a time, and for each row you did a lookup on the fixed column. Lookups are easy, but they can get really repetitive!
In the Interactions Area, type the code that will look up if animalA is fixed or not.
Then type the code to look up if animalB is fixed or not. Repeat for animalC and animalD. Suppose I wanted to do this for every animal in the table, just as you did by hand?
This seems really repetitive, doesn’t it? We keep typing the same thing over and over, but all that’s really changing is the animal. Wouldn’t it be great if Pyret had a function called is-fixed, that would do this for us?
Have a student act out the is-fixed function. You give them an animal, and they tell you what they would type to find out if it is fixed.
Look back to the Definitions Area, and find the line that starts with fun is-fixed. This function isn’t built into Pyret, but it’s defined here in the program, so we can use it just as if it were built into the language!
Type is-fixed(animalA) into the Interactions Area. What did the function do?
You already know about the .row-n and .order-by methods. But suppose you want to get a table of only animals that have been fixed? Try typing this expression into the Interactions Area. What do you get?
If time allows, ask students to explain what they think is going on.
The filter method walks through the table, applying whatever function it was given to each row, and producing a new table containing all the rows for which the function returned true. In this case, we gave it the is-fixed function, so the new table had only rows for fixed animals.
But how do we define functions like this?
To build our own functions, we’ll use a series of steps called the Design Recipe. The Design Recipe is a way to think through the behavior of a function, to make sure we don’t make any mistakes with the animals that depend on us! The Design Recipe has three steps, and we’ll go through them together for our first function.
The first thing we do is write a Contract for this function. You already know a lot about contracts: they tell us the Name, Domain and Range of the function. Our function is named is-fixed, and it consumes a row from the animals table. It looks up the value in the fixed column, which will always be a Boolean. A Purpose Statement is just a description of what the function does:
Since the contract and purpose statement are notes for humans, we add the # symbol at the front of the line to turn it into a comment.
Be sure to check students’ contracts and purpose statements before having them move on!
Step 2: Write Examples
Examples are a way for us to tell the computer how our function should behave for a specific input. We can write as many examples as we want, but they must all be wrapped in an examples: block and an end statement. Examples start with the name of the function we’re writing, followed by an example input. Suppose we have two animals defined, where animalA is fixed and animalB isn’t. What work do we have to do on each row to look up whether they are fixed? What is will the result be for each animal?
Make sure students understand (1) that is-fixed came from the Name in our contract, (2) that animalA and animalB came from the Domain in our contract, and (3) that the Booleans are determined by whether those animals are fixed or not.
When writing examples, we replace the look-up operation with the actual value in the table.
This is a MAJOR point. Make sure students see it (and maybe even repeat it!).
Step 3: Define the Function
We start with the fun keyword (short for "function"), followed by the name of our function and a set of parentheses. This is exactly how all of our examples started, too. But instead of writing animalA or animalB, we’ll use the label from our Domain. Then we add a colon (:) in place of is, and write out the work we did to get the answers for our examples. Finally, we finish with the end keyword.
This program is missing examples! Add an examples block in the Definitions Area, using your animalA and animalB. Check the Animals Dataset to make sure that your Booleans are correct for your animals. If you click "Run", you’ll see a report on whether the examples are correct. Make sure both of them pass!
Walk around to make sure everyone’s examples pass.
Now let’s try coming up with a totally new function, and use the Design Recipe to help us write it.
Type in the Contract, Purpose Statement, Examples and Definition into the Definitions Area.
Click "Run", and make sure all your examples pass!
Type gender(animalA) into the Interactions Area.
So far, our functions have all been Lookup Functions: they consume a row, and they look up one column from that row as-is. And as long as that row contains Boolean values, we can use that function with the .filter method. But what if we want to filter by a Boolean expression? For example, what if we want to find out specifically whether or not an animal is a cat, or whether it’s young? Let’s walk through an example of a Compute Function using the Design Recipe, by turning to Page 11.
Define a function called is-cat, which consumes a row from the animals-table and returns true if the animal is a cat.
Is this a Lookup, Compute or Relate question?
What is the name of this function? What are its Domain and Range?
Is Sasha a cat? What did you do to get that answer?
Have students explain their thinking carefully, step-by-step. Repeat this with other animals.
To find out if an animal is a cat, we look-up the species column and check to see if that value is equal to"cat". Suppose animalA is a cat and animalB is a lizard. What should our examples look like? Remember: we replace any lookup with the actual value, and check to see if it is equal to "cat".
Write two examples for your defined animals. Make sure one is a cat and one isn’t!
Note that the string on the left is the lookup value: the actual species for that specific animal.
As before, we’ll use the pattern from our examples to come up with our definition.
What is the function name?
What is the name of the variable(s)?
What do we do in the body in the function?
Type this definition - and its examples! - into the Definitions Area, then click "Run" and try using it to filter the animals-table.
For practice, try solving the word problem for is-young at the bottom of Page 11.
More About Table Methods
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
More About Table Methods(Time 15 minutes)
More About Table MethodsFind the contract for .filter in your contracts page. The .filter method is taking in a function, calling it on every row in the table, and producing a new table with only the rows for which it returns true.
Try using the gender function to filter. What happens?
Notice that the Domain for .filter says that test must be a function (that’s the arrow), which consumes a Row and produces a Boolean. If the function we pass in produces anything else, we’ll get an error.
If time allows: have them make a pie chart using a table of only cats, or a bar chart of only the animals that have been fixed.
Sometimes we want to add a column to a table. For example, we could add a boolean column called "young" to the table, which is true if the animal is less than four years old and false if it’s not. Pyret has another method for this.
Type this into the Interactions Area and hit Enter. What did you get back?
Closing
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Closing(Time 5 minutes)
ClosingCongratulations! You’ve explored the Animals dataset, formulated your own questions and begun to think critically about the connections between data and the questions we ask about it. For the rest of this course, you’ll be learning new programming and Data Science skills, practicing them with the Animals dataset and then applying them to your own data.
Have students share which dataset they chose, and pick one question they’re looking at.
Bootstrap:Data Science by Emmanuel Schanzer, Nancy Pfenning, Emma Youndtsmith, Jennifer Poole, Shriram Krishnamurthi, Joe Politz and Ben Lerner was developed partly through support of the National Science Foundation, (awards 1535276, 1647486, and 1738598), and is licensed under a Creative Commons 4.0 Unported License. Based on a work at www.BootstrapWorld.org. Permissions beyond the scope of this license may be available by contacting schanzer@BootstrapWorld.org.