ā return to practice.dsc10.com
Instructor(s): Vrinda Desai
Vrinda opened an ice cream shop called The Parlor and you have been
hired as the newest employee. The DataFrame orders lists
ice cream orders from every customer since The Parlor opened. Every ice
cream flavor offered at The Parlor has been sold at least once. The
columns of orders are:
"flavor" (str): The flavor of ice cream
selected by the customer."scoops" (int): The number of scoops for
the ice cream order."cone" (bool): True if the
ice cream was served in a cone and False if it was served
in a cup."price" (float): The total price of the
ice cream order in dollars."temp" (int): The temperature high in
Fahrenheit of the day on which the order was placed."days" (int): The number of days since The
Parlor opened (i.e.Ā opening day is 0, 30th day open is
29).The first few rows of orders are shown below, though
orders has many more rows than shown.
| flavor | scoops | cone | price | temp | days | |
|---|---|---|---|---|---|---|
| 0 | double chocolate | 2 | True | 7.50 | 74 | 0 |
| 1 | peanut butter | 1 | True | 4.50 | 74 | 0 |
| 2 | raspberry lemon | 2 | False | 6.00 | 77 | 1 |
| 3 | birthday cake | 4 | True | 13.50 | 77 | 1 |
| 4 | raspberry lemon | 1 | False | 3.00 | 90 | 2 |
Additionally, we are given another DataFrame called
flavors. Every ice cream flavor offered at The Parlor
appears in flavors alphabetically. Each flavor only appears
once. The only column in flavors is "name"
(str). The first few rows of flavors are shown
below, though flavors has many more rows than shown.
| name | |
|---|---|
| 0 | apple pie |
| 1 | birthday cake |
| 2 | butterscotch |
Throughout this exam, we will refer to orders and
flavors repeatedly. Assume that we have already run
import babypandas as bpd and
import numpy as np.
The Parlor uses a fixed pricing for each order. The function
get_price takes two arguments: the number of scoops (an
int) and whether the ice cream was served in a cone (a
bool). You may assume that a customer can order no more
than 4 scoops of ice cream. get_price should return the
price (a float) that would appear in the
"price" column of orders for an ice cream with
the same attributes.
Example inputs and outputs are given below.
>>> get_price(2, True)
7.50
>>> get_price(2, False)
6.00
>>> get_price(4, True)
13.50Consider the following four function definitions.
Option 1:
def get_price(scoops, cone):
return np.arange(0, 5 * 2, 2)[scoops] + (cone * 1.5)Option 2:
def get_price(scoops, cone):
return np.arange(0, 4 * 3, 3)[scoops] + (cone * 1.5)Option 3:
def get_price(scoops, cone):
return np.arange(0, 5 * 3, 3)[scoops] + (cone * 1.5)Option 4:
def get_price(scoops, cone):
return np.arange(3, 5 * 3, 3)[scoops] + (cone * 1.5)Which of the above functions correctly defines
get_price? Select only one of the
options.
Option 1
Option 2
Option 3
Option 4
Answer: Option 3
From the examples, a cup costs $3.00 per scoop
(get_price(2, False) is 6.00), and a cone adds $1.50
(get_price(2, True) is 7.50). Since True
behaves like 1 and False behaves like 0 in arithmetic,
cone * 1.5 adds 1.5 for a cone and 0 for a cup. So we need
an array where the element at position scoops is
3 * scoops.
np.arange(0, 5 * 3, 3) is
[0, 3, 6, 9, 12]. Position 2 holds 6 and position 4 holds
12, so get_price(4, True) is 12 +
1.5 = 13.5. ā
np.arange(0, 5 * 2, 2) is
[0, 2, 4, 6, 8], so get_price(2, False) would
be 4, not 6.np.arange(0, 4 * 3, 3) is
[0, 3, 6, 9], which has only 4 elements, so
[4] causes an error for a 4-scoop order.np.arange(3, 5 * 3, 3) is
[3, 6, 9, 12], so get_price(2, False) would be
9, not 6. Every price is shifted by one scoop.We want to take a simple random sample (SRS) of 10 ice cream orders
from the orders DataFrame. Which of the following samples
produce a SRS? Select only one of the options.
orders.sample(10, replace=True)
orders.take(np.arange(10))
orders[orders.get("flavor") == "birthday cake"].sample(10)
None of the above.
Answer: None of the above.
A simple random sample is drawn uniformly at random without replacement from the entire population.
orders.sample(10, replace=True) samples
with replacement, so the same order could appear more
than once. That is not a simple random sample.orders.take(np.arange(10)) always returns the first 10
rows of orders. Thereās no randomness at all.orders[orders.get("flavor") == "birthday cake"].sample(10)
only samples from birthday cake orders, so orders of any other flavor
have no chance of being selected.Consider the following code:
orders_count = orders.shape[0]
flavors_count = flavors.shape[0]
merged = flavors.merge(orders, left_on="name", right_on="flavor")
merged_count = merged.shape[0]Which of the following expressions evaluate to True?
Select only one of the options.
flavors_count > orders_count
flavors_count < orders_count
flavors_count == orders_count
Answer:
flavors_count < orders_count
flavors has exactly one row per flavor. Every flavor has
been sold at least once, so every flavor shows up in at least one row of
orders. Many flavors are ordered many times (for example,
"raspberry lemon" already appears twice in the first five
rows of orders), so orders has more rows than
flavors.
Which of the following expressions evaluate to True?
Select only one of the options.
orders_count > merged_count
orders_count < merged_count
orders_count == merged_count
Answer:
orders_count == merged_count
When merging, each row of orders is matched with every
row of flavors that has the same flavor name. Each flavor
appears exactly once in flavors, and every flavor in
orders is one The Parlor offers, so it appears in
flavors. That means each row of orders matches
exactly one row of flavors. No rows are
dropped and no rows are duplicated, so merged has the same
number of rows as orders.
Which of the following expressions evaluate to True?
Select only one of the options.
flavors_count > merged_count
flavors_count < merged_count
flavors_count == merged_count
Answer:
flavors_count < merged_count
From the previous two parts,
merged_count == orders_count and
flavors_count < orders_count. Combining these,
flavors_count < merged_count.
Customers often struggle to pick a flavor. Janine writes the
following code to randomly select an ice cream flavor from
flavors. Note that np.ones(n) returns an array
of length n where every element is 1.
distA = orders.groupby("flavor").count().get("cone") / orders.shape[0]
distB = np.ones(flavors.shape[0]) / flavors.shape[0]
selection = np.random.multinomial(1, __(i)__) == 1
print(flavors[selection].get("name"))Which of the following best describes the type of
selection if (i) is replaced with
distA? Select only one of the options.
int
bool
boolean array
int array
Answer: boolean array
np.random.multinomial(1, distA) returns an
array of integers with one element per flavor. Since we
only draw 1 item, exactly one element is 1 and the rest are 0. Comparing
that array to 1 with == 1 happens element-wise, which gives
an array of True/False values. Thatās a
boolean array, with True in the position of the selected
flavor.
What is the code most likely to output if (i) is
replaced with distA? Select only one of the
options.
The most popular ice cream flavor
The least popular ice cream flavor
The newest ice cream flavor
Every flavor is equally likely to be outputted
Answer: The most popular ice cream flavor
orders.groupby("flavor").count().get("cone") gives the
number of orders of each flavor. Dividing by
orders.shape[0] turns these counts into the proportion of
all orders that were each flavor. The groupby sorts flavors
alphabetically, the same order as flavors, so the boolean
array selection lines up with the rows of
flavors.
Each flavor is selected with probability equal to its share of orders, so the flavor ordered most often has the highest chance of being printed.
What is the code most likely to output if (i) is
replaced with distB? Select only one of the
options.
The most popular ice cream flavor
The least popular ice cream flavor
The newest ice cream flavor
Every flavor is equally likely to be outputted
Answer: Every flavor is equally likely to be outputted
distB is an array of length
flavors.shape[0] where every element is
1 / flavors.shape[0]. Thatās the uniform distribution over
all flavors, so each flavor has the same chance of being selected.
Noah has been working the register this week and puts together a
dataset of all ice cream orders from this week. The DataFrame
week contains every ice cream sold this week and has all of
the same columns as orders. Due to administrative
permissions, Noah does not have access to the orders
DataFrame.
Consider the following bootstrap code which uses the
week DataFrame.
estimates = np.array([])
for i in np.arange(10000):
estimate = 0
for j in np.arange(100):
value = week.sample(1).get("scoops").iloc[0]
estimate += value
estimates = np.append(estimates, estimate * 0.01)True or False: The week DataFrame is
your population in this example.
True
False
Answer: False
The population is every ice cream order since The Parlor opened,
which is the data in orders. Noah doesnāt have access to
orders, so he uses week, a
sample of orders from one week, and bootstraps from it
to estimate a parameter of the population.
What population parameter is being estimated by the bootstrap code?
Answer: The mean number of scoops per order among
all orders at The Parlor (the mean of the "scoops" column
in orders).
In each iteration of the outer loop, the inner loop adds up 100
randomly selected "scoops" values. Multiplying that sum by
0.01 is the same as dividing by 100, so each
estimate is the mean number of scoops in a
resample. These means estimate the mean number of scoops per order in
the population.
What is the size of every resample generated by the bootstrap code? Give your answer as an int.
Answer: 100
Each resample is built one row at a time in the inner loop, which
runs 100 times. Every call to week.sample(1) picks one row
from all of week, independently of previous picks. So the
same row can be selected more than once, which means the resample is
drawn with replacement. Each resample has 100
values.
Suppose we were to plot a histogram of estimates. Which
of the following best describes this histogram? Select only one
of the options.
The histogram reflects the shape of the population.
The histogram reflects the shape of the sample.
The histogram is roughly normal because of the Central Limit Theorem (CLT).
The shape of the histogram cannot be determined.
Answer: The histogram is roughly normal because of the Central Limit Theorem (CLT).
Each element of estimates is the mean
of a random sample of 100 values drawn with replacement. The CLT says
that the distribution of sample means is roughly normal for large
samples, no matter what the shape of the original data looks like. So
the histogram of estimates is roughly normal (bell-shaped),
not shaped like the population or the sample.
Sam bakes The Parlorās sugar cones from scratch. He would like to
estimate the proportion of customers that purchase their ice cream in a
cone. Recall the week DataFrame introduced in the previous
question. You will use it to answer the following questions.
Complete the implementation of the function cone_ci. The
function returns the endpoints of a 95% CLT-based confidence interval
for the proportion of customers who purchased their ice cream in a cone
based on the data in week.
def cone_ci():
size = week.shape[0]
prop = week.get("cone").mean()
std = np.std(week.get("cone"))
low = __(i)__
high = __(ii)__
return [low, high]Fill in blanks (i) and (ii).
Answer (i):
prop - 2 * std / np.sqrt(size)
Answer (ii):
prop + 2 * std / np.sqrt(size)
The "cone" column is made up of Trues and
Falses, which behave like 1s and 0s. That means the
proportion of cone orders is a mean, so we can use the
CLT. The distribution of the sample proportion is roughly normal,
centered at prop, with a standard deviation of
std / np.sqrt(size). About 95% of a normal distribution is
within 2 standard deviations of the mean, so we go 2 standard deviations
in each direction from prop.
Consider the following code.
>>> interval = cone_ci()
>>> interval[1] - interval[0] <= 0.10
TrueWhat is the minimum value of size in
cone_ci that would guarantee that the code
above runs as described? Give your answer as an int and show
your work.
Hint: The standard deviation of a collection of 0s and 1s is \sqrt{p(1-p)}, where p is the proportion of 1s.
Answer: 400
The width of the interval is the distance from low to
high:
\text{width} = 4 \cdot \frac{\text{std}}{\sqrt{\text{size}}}
We donāt know the proportion of cone orders ahead of time, so to guarantee the width is small enough we use the largest possible standard deviation. \sqrt{p(1-p)} is largest when p = 0.5, which gives \sqrt{0.5 \cdot 0.5} = 0.5. Then we need:
4 \cdot \frac{0.5}{\sqrt{\text{size}}} \leq 0.10
\frac{2}{\sqrt{\text{size}}} \leq 0.10
\sqrt{\text{size}} \geq 20
\text{size} \geq 400
So the minimum is 400.
Now consider the following code:
>>> interval = cone_ci()
>>> interval[1] - interval[0] <= 0.20
TrueRegardless of your answer above, now assume we refer to the correct
answer to part (b) as s. What is the
minimum value of size in cone_ci that would
guarantee that the code above runs as described? Express your
answer in terms of s.
Answer: 0.25s (equivalently, \frac{s}{4})
The width of the interval is proportional to \frac{1}{\sqrt{\text{size}}}. Weāre now
allowing an interval that is twice as wide (0.20 instead of 0.10). To
double the width, \sqrt{\text{size}}
needs to be half as large, which means size needs to be
\frac{1}{4} as large. So the minimum
size is \frac{s}{4}.
Using the numbers from the previous part: \frac{2}{\sqrt{\text{size}}} \leq 0.20 gives \sqrt{\text{size}} \geq 10, so \text{size} \geq 100 = \frac{400}{4}.
Sam loves making cones and hopes that at least 90% of the population
of customers at The Parlor are purchasing cones. Which pair of
hypotheses can we test using a confidence interval generated by
cone_ci? Select all that apply.
Null Hypothesis: The proportion of people that purchase cones is at least 0.90. Alternative Hypothesis: The proportion of people that purchase cones is less than 0.90.
Null Hypothesis: The proportion of people that purchase cones is equal to 0.90. Alternative Hypothesis: The proportion of people that purchase cones is not equal to 0.90.
Null Hypothesis: The proportion of people that purchase cones is equal to 0.90. Alternative Hypothesis: The proportion of people that purchase cones is greater than 0.90.
Answer: Only the second option: Null Hypothesis: The proportion of people that purchase cones is equal to 0.90. Alternative Hypothesis: The proportion of people that purchase cones is not equal to 0.90.
We can use a confidence interval to test hypotheses of the form āthe parameter equals some valueā vs.Ā āthe parameter does not equal that valueā. If 0.90 is not in the interval, we reject the null; if it is in the interval, we fail to reject the null.
The alternative hypothesis must be two-sided (ānot equal toā) because the confidence interval cuts off an equal amount from both tails. It canāt tell us which direction the parameter is in. The other two options have one-sided alternatives (āless thanā or āgreater thanā), so we canāt test them with a confidence interval. The first option also has a null hypothesis that isnāt a single value (āat least 0.90ā).
Sam uses cone_ci to produce a 95% confidence interval
with endpoints [0.72, 0.81]. Unfortunately, 0.90 does not fall within
this interval. Suppose you updated cone_ci() to produce a
68% confidence interval with endpoints [L,
R].
Could 0.90 fall within the new interval [L, R]? In no more than three sentences, explain why.
Answer: No.
Both intervals are centered at the same sample proportion, 0.765. A 68% confidence interval goes out only 1 standard deviation in each direction instead of 2, so it is narrower than the 95% interval and fits entirely inside [0.72, 0.81]. Since 0.90 is not in the 95% interval, it canāt be in the 68% interval either.