12.5.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: The Correlation Coefficient
# Marketing recorded weekly ad spend and sales for 8 weeks.
# 1. Write standard_units(x): (x - mean of x) / SD of x, using np.mean and np.std.
# 2. Compute r: the mean of the products of ad_spend and sales in standard units.
# 3. Print r rounded to 3 decimals.
# Print exactly:
# r: 0.972
import numpy as np
import pandas as pd
ads = pd.DataFrame({
"ad_spend": [2, 3, 4, 5, 6, 7, 8, 9], # $1,000s per week
"sales": [60, 72, 70, 85, 88, 98, 95, 110], # $1,000s per week
})
### Your code starts here.
### Your code ends here.
r: 0.972
### Question 2: The Regression Line
# Find the regression line for predicting sales from ad spend.
# slope = r * (SD of sales) / (SD of ad_spend)
# intercept = (mean of sales) - slope * (mean of ad_spend)
# 1. Compute r, the slope, and the intercept (use np.std for SDs).
# 2. Print the slope and intercept rounded to 2 decimals.
# Print exactly:
# Slope: 6.57
# Intercept: 48.61
import numpy as np
import pandas as pd
ads = pd.DataFrame({
"ad_spend": [2, 3, 4, 5, 6, 7, 8, 9], # $1,000s per week
"sales": [60, 72, 70, 85, 88, 98, 95, 110], # $1,000s per week
})
### Your code starts here.
### Your code ends here.
Slope: 6.57
Intercept: 48.61
### Question 3: Prediction and Residual
# Use the regression line (unrounded slope and intercept) to:
# 1. Predict sales for a week with ad spend of 6.5, rounded to 2 decimals.
# 2. Compute the residual for the week with ad spend 5 (actual sales 85):
# actual minus fitted, rounded to 2 decimals.
# Print exactly:
# Predicted sales at 6.5: 91.32
# Residual at 5: 3.54
import numpy as np
import pandas as pd
ads = pd.DataFrame({
"ad_spend": [2, 3, 4, 5, 6, 7, 8, 9], # $1,000s per week
"sales": [60, 72, 70, 85, 88, 98, 95, 110], # $1,000s per week
})
### Your code starts here.
### Your code ends here.
Predicted sales at 6.5: 91.32
Residual at 5: 3.54
### Question 4: Least Squares Beats a Rule of Thumb
# A manager uses the rule of thumb: sales = 50 + 6 * ad_spend.
# 1. Compute the root mean squared error (RMSE) of the regression line's
# fitted values: sqrt(mean of (actual - fitted) ** 2).
# 2. Compute the RMSE of the rule of thumb's predictions.
# 3. Round both to 2 decimals.
# Print exactly:
# Regression RMSE: 3.67
# Rule of thumb RMSE: 4.27
import numpy as np
import pandas as pd
ads = pd.DataFrame({
"ad_spend": [2, 3, 4, 5, 6, 7, 8, 9], # $1,000s per week
"sales": [60, 72, 70, 85, 88, 98, 95, 110], # $1,000s per week
})
### Your code starts here.
### Your code ends here.
Regression RMSE: 3.67
Rule of thumb RMSE: 4.27
### Question 5: Reading the Residuals
# A café tracked units sold of a pastry at different prices (in dollars).
# 1. Fit the regression line of units on price and compute the residuals
# (actual minus fitted).
# 2. The rows are sorted by price. Compute the mean residual for the first
# 3 rows (low prices), the middle 3, and the last 3 (high prices),
# each rounded to 1 decimal.
# 3. If the low and high groups are positive and the middle is negative,
# print "curved"; otherwise print "no clear pattern".
# Print exactly:
# Low prices: 4.0
# Middle prices: -8.7
# High prices: 4.6
# Pattern: curved
import numpy as np
import pandas as pd
cafe = pd.DataFrame({
"price": [5, 6, 7, 8, 9, 10, 11, 12, 13],
"units": [90, 70, 55, 44, 37, 33, 31, 30, 30],
})
### Your code starts here.
### Your code ends here.
Low prices: 4.0
Middle prices: -8.7
High prices: 4.6
Pattern: curved
Question 1
Question 2
Question 3
Question 4
Question 5