Exam practice
Every question from the end of every unit, with hints and answers to open. After you check an answer, mark how you did, and come back to the ones you didn’t get.
12 questions
Unit 4 · Data Science
Explain these words, with an example of each: DataFrame, outlier, multivariate chart, simulation, parameter.
Answer
- DataFrame: pandas’s table, with named columns and numbered rows. Example:
pd.read_csv("districts.csv")gives a DataFrame of 77 rows and 6 columns. - Outlier: a value far from the others. Example: Kathmandu’s 4,936 people per km², among hill districts mostly between 100 and 250.
- Multivariate chart: a chart that shows three or more variables at once. Example: a scatter chart of the districts’ height and rain, with dots colored by belt.
- Simulation: a program that acts out a real system with made-up data that behaves like the real thing, to answer “what if?” questions. Example: the ticket counter’s mornings with one or two counters.
- Parameter: a number that sets how simulated data is made. Example: 25 people an hour, or service taking 1 to 3 minutes; the 48 and 12 in
random.gauss(48, 12).
Write the pandas lines that, from districts.csv: (i) print the five districts with the most people per km²; (ii) print how many Mountain districts get more than 2,500 mm of rain; (iii) print the median height of each belt.
Answer
import pandas as pd districts = pd.read_csv("districts.csv")print(districts.sort_values("people", ascending=False).head(5))wet = districts[(districts["belt"] == "Mountain") & (districts["rain"] > 2500)]print(len(wet))print(districts.groupby("belt")["height"].median())Output
district belt height slope rain people34 Kathmandu Hill 1421 13 2137 49369 Bhaktapur Hill 1378 10 1745 351037 Lalitpur Hill 1521 22 2111 139061 Rupandehi Terai 115 3 1997 86057 Rautahat Terai 98 2 1820 7836beltHill 1370.0Mountain 3595.0Terai 117.0Name: height, dtype: float64A survey file has a column “age” whose values include 14, 15 years, fifteen, 16, an empty cell and 150. List the cleaning problems, and say how a program should deal with each.
Answer
15 years: a number with words attached. Take the number out (for example, the digits only) and put the unit in the column’s name.fifteen: a number written as a word. Convert it, with a small dictionary of words, or mark it for a person to check.- The empty cell: a missing value. Leave it missing (
None/<NA>), never 0, and report how many answered. 150: an impossible value for a student (probably 15 typed with an extra 0). Check it with the person if you can; otherwise mark it missing and say so. A range check (say, 5 to 25) finds such values.
Then convert the column to numbers on purpose and check dtypes. Keep the original file, and write down every change and why.
Why must cleaning be done before two tables are merged? Use an example.
Answer
A merge matches the key values exactly. If one table has “ Kathmandu” (with a space) or “Kavre”, and the other has “Kathmandu” and “Kavrepalanchok”, those rows find no match. A plain merge then drops them without any error, so their numbers silently vanish from every total; a left merge keeps them but with empty values. In this unit, before cleaning, the merge left out six of Bagmati’s 13 districts, about four million people. Cleaning the keys to one spelling first makes every row match, and checking for unmatched rows after the merge (isna().sum()) proves it.
What is a log scale? When should a chart use one, and what must it show?
Answer
A log scale is an axis on which each equal step is a multiplication, usually by 10 (1, 10, 100, 1,000), instead of an addition. Use it when the values run over several powers of ten, such as people per km² of Nepal’s districts (2 to 4,936) or the populations of countries, so that small values aren’t squashed into a line at the bottom. It must be named (“log scale”) and its ticks labeled, because equal gaps no longer mean equal differences.
Write a program that makes up, and checks, the heights of 30 students: a normal distribution with a mean of 155 cm and a standard deviation of 7 cm, rounded to whole centimeters, none below 130 or above 185. Print the mean and standard deviation of the heights you made.
Answer
import randomimport statistics random.seed(1)heights = []for i in range(30): h = round(random.gauss(155, 7)) h = max(130, min(185, h)) heights.append(h) print("Mean:", round(statistics.mean(heights), 1))print("Spread:", round(statistics.stdev(heights), 1))Output
Mean: 155.8Spread: 6.3The mean and spread are close to 155 and 7 but not exactly, as with any 30 real students: a different seed gives slightly different numbers.
In the ticket counter simulation, explain what each of these lines does, and why it is needed:start = max(now, free_at[k]) and free_at[k] = start + random.uniform(1, 3).
Answer
start = max(now, free_at[k]): the person is served at whichever is later, the moment they arrive (now) or the moment counter k is free. If the counter is free already, they start at once (no wait); if it is busy, they wait until it is free. Without it the program couldn’t work out waits at all.free_at[k] = start + random.uniform(1, 3): counter k will be busy with this person fromstartfor a random time between 1 and 3 minutes, so it is next free then. This is how one person’s service makes the next person wait.
A simulation of the counter, run once, gives an average wait of 3 minutes. A friend says: “So people wait 3 minutes.” What is wrong with this, and what should be done instead?
Answer
One run is one made-up morning; another run, with other random numbers, can give 1 minute or 20. The simulation should be run many times (say 100 mornings), and the results reported as data: the typical (median) average wait, and the spread, including how bad the worst mornings are (a histogram shows it). And even then the result is only as good as the model: its parameters should come from real counts, and its results be checked against real waits.
A news report says: “Students who use phones more than 3 hours a day score 15% lower in exams. Phones are ruining results.” Write three questions you would ask before believing the conclusion.
Answer
Any three, such as:
- How many students were in the data, from where, and how were they chosen? (Is it one school or the whole country? Are the groups big enough?)
- How was phone use measured: by the students’ own guesses, or by the phones?
- Could a third thing explain both, such as students with less help at home or less time for study, or students who work in the evenings? Were the groups compared like with like (Simpson’s paradox)?
- “15% lower” than whom, and is the difference bigger than the usual spread of marks?
- “Ruining” claims a cause; the data only shows that the two go together. Was there an experiment?
Look at the hospital table in “A third variable can turn a result round”. Explain, in your own words, how the district hospital can have the higher recovery rate overall while the city hospital does better in both groups. What should a newspaper report?
Answer
Seriously ill patients recover less often wherever they are treated. The city hospital treats mostly seriously ill patients (400 of its 500), while the district hospital treats mostly mild cases (450 of its 500). So the city hospital’s overall rate is mostly its seriously-ill rate (80%), pulled down by who it treats, and the district hospital’s overall rate is mostly its mild rate (96%). In each group, comparing like with like, the city hospital does better (80% against 70%, and 98% against 96%).
A newspaper should report the comparison within each group, say that the city hospital treats far more seriously ill patients, and not rank the hospitals by their overall rates alone.
Write a four-part conclusion (claim, evidence, how sure, limits) for this finding from districts.csv: the median number of people per km² is 551 in the Terai, about 146 in the Hill belt and about 40 in the Mountain belt.
Answer
Claim. In Nepal’s districts, the lower belts tend to be much more crowded: Terai districts have the most people per km², Mountain districts the fewest.
Evidence. The median district has 551 people per km² in the Terai, about 146 in the Hill belt and about 40 in the Mountain belt (2021 census, divided by each district’s area). On a scatter chart of height and people, the Terai dots sit high and the Mountain dots low.
How sure. These are all 77 districts, not a sample, so the difference is not chance. But the Hill belt varies hugely: the Kathmandu valley’s three districts are more crowded than any Terai district, which is why the median is used.
Limits. A district’s average hides crowded towns and empty land inside it. People per km² of the whole district counts snow, rock and forest where nobody can live. The data says where people live, not why (flat land for farming, roads, jobs, history are all possible reasons).
Plan a simulation to help your school decide whether its canteen needs a second person at the counter at break time. Say what parameters you would need, how you would find them, what the simulation would report, and two things your model might leave out.
Answer
Parameters: how many students come to the canteen during the break (and whether they all come in the first few minutes); how long serving one student takes (the shortest and longest, or the average and spread); how long the break is; how many people serve.
Finding them: on a few normal days, count the students arriving each minute of the break and time a sample of students being served, with a watch.
The simulation: like one_morning, with arrivals bunched at the start of the break, run for 100 breaks with one server and with two. Report the typical wait and the spread, the share of students who wait longer than, say, 5 minutes, and how many reach the counter before the bell rings, which matters most here.
Left out (any two): students who give up when they see a long line; friends who buy for each other; days when there is a special food; a server who is slower or faster; students who pay with a phone and take longer or shorter. Check the model by running it with today’s settings and comparing it with the real lines.
