Lab

13.4.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: Fitting a Multiple Regression
# A real estate agent studies 16 recent home sales (prices in $1,000s).
#   1. Fit a LinearRegression model that predicts price from sqft, rooms, and age.
#   2. Print the intercept rounded to 2 decimals, then each coefficient rounded
#      to 3 decimals, in the order sqft, rooms, age.
# Print exactly:
# Intercept: 108.23
# sqft: 0.145
# rooms: -16.384
# age: -1.452
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# 16 real sales from Ames, Iowa (Ames Housing data, De Cock 2011): sqft is living area,
# rooms is total rooms above ground, age is years old when sold.
homes = pd.DataFrame({
    "sqft": [1511, 864, 848, 1616, 990, 1635, 1456, 1174, 1412, 1616, 2654, 1211, 1433, 2475, 1295, 1073],
    "rooms": [6, 5, 3, 5, 5, 5, 7, 5, 6, 7, 11, 5, 7, 7, 4, 4],
    "age": [13, 36, 6, 15, 11, 96, 33, 30, 70, 1, 87, 40, 45, 12, 29, 2],
    "price": [225.0, 120.5, 143.0, 236.5, 156.0, 117.0, 143.0, 158.0, 132.5, 232.0, 158.0, 134.0, 161.0, 355.0, 213.8, 157.0],   # $1,000s
})
### Your code starts here.



### Your code ends here.
### Question 2: Holding Other Features Fixed
# The data are the same 16 Ames sales as in Question 1.
# Use the model from Question 1 (fit it again here).
#   1. Predict the price of house_a and of house_b, which is the same house
#      with one more room. Round each to 1 decimal.
#   2. Compute the difference, B minus A, rounded to 2 decimals.
#   3. Print the model's rooms coefficient, rounded to 2 decimals, to compare.
# Print exactly:
# House A: 242.6
# House B: 226.2
# Difference: -16.38
# Rooms coefficient: -16.38
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# 16 real sales from Ames, Iowa (Ames Housing data, De Cock 2011): sqft is living area,
# rooms is total rooms above ground, age is years old when sold.
homes = pd.DataFrame({
    "sqft": [1511, 864, 848, 1616, 990, 1635, 1456, 1174, 1412, 1616, 2654, 1211, 1433, 2475, 1295, 1073],
    "rooms": [6, 5, 3, 5, 5, 5, 7, 5, 6, 7, 11, 5, 7, 7, 4, 4],
    "age": [13, 36, 6, 15, 11, 96, 33, 30, 70, 1, 87, 40, 45, 12, 29, 2],
    "price": [225.0, 120.5, 143.0, 236.5, 156.0, 117.0, 143.0, 158.0, 132.5, 232.0, 158.0, 134.0, 161.0, 355.0, 213.8, 157.0],   # $1,000s
})
house_a = pd.DataFrame({"sqft": [1800], "rooms": [6], "age": [20]})
house_b = pd.DataFrame({"sqft": [1800], "rooms": [7], "age": [20]})
### Your code starts here.



### Your code ends here.
### Question 3: Spotting Multicollinearity
# The data are the same 16 Ames sales as in Question 1.
#   1. Compute the correlation matrix of the three predictors with .corr().
#   2. Find the pair of DIFFERENT predictors with the largest absolute correlation.
#   3. Print the pair (in column order) and its correlation, rounded to 2 decimals.
# Print exactly:
# Most correlated pair: sqft and rooms
# Correlation: 0.81
import numpy as np
import pandas as pd

# 16 real sales from Ames, Iowa (Ames Housing data, De Cock 2011): sqft is living area,
# rooms is total rooms above ground, age is years old when sold.
homes = pd.DataFrame({
    "sqft": [1511, 864, 848, 1616, 990, 1635, 1456, 1174, 1412, 1616, 2654, 1211, 1433, 2475, 1295, 1073],
    "rooms": [6, 5, 3, 5, 5, 5, 7, 5, 6, 7, 11, 5, 7, 7, 4, 4],
    "age": [13, 36, 6, 15, 11, 96, 33, 30, 70, 1, 87, 40, 45, 12, 29, 2],
    "price": [225.0, 120.5, 143.0, 236.5, 156.0, 117.0, 143.0, 158.0, 132.5, 232.0, 158.0, 134.0, 161.0, 355.0, 213.8, 157.0],   # $1,000s
})
### Your code starts here.



### Your code ends here.
### Question 4: Simple vs. Multiple on Test Data
# The data are the same 16 Ames sales as in Question 1.
#   1. Split homes into training and test sets with
#      train_test_split(X, y, test_size=0.25, random_state=1).
#   2. On the training set, fit a simple model (sqft only) and a multiple model
#      (sqft, rooms, age).
#   3. Compute each model's test RMSE: np.sqrt(mean_squared_error(...)),
#      rounded to 2 decimals.
# Print exactly:
# Simple RMSE: 96.23
# Multiple RMSE: 32.34
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score

# 16 real sales from Ames, Iowa (Ames Housing data, De Cock 2011): sqft is living area,
# rooms is total rooms above ground, age is years old when sold.
homes = pd.DataFrame({
    "sqft": [1511, 864, 848, 1616, 990, 1635, 1456, 1174, 1412, 1616, 2654, 1211, 1433, 2475, 1295, 1073],
    "rooms": [6, 5, 3, 5, 5, 5, 7, 5, 6, 7, 11, 5, 7, 7, 4, 4],
    "age": [13, 36, 6, 15, 11, 96, 33, 30, 70, 1, 87, 40, 45, 12, 29, 2],
    "price": [225.0, 120.5, 143.0, 236.5, 156.0, 117.0, 143.0, 158.0, 132.5, 232.0, 158.0, 134.0, 161.0, 355.0, 213.8, 157.0],   # $1,000s
})
### Your code starts here.



### Your code ends here.
### Question 5: R-squared on the Test Set
# The data are the same 16 Ames sales as in Question 1.
#   1. Use the same split and multiple model as Question 4.
#   2. Compute R-squared on the test set with r2_score(y_test, predictions),
#      rounded to 2 decimals.
# Print exactly:
# Test R-squared: 0.85
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score

# 16 real sales from Ames, Iowa (Ames Housing data, De Cock 2011): sqft is living area,
# rooms is total rooms above ground, age is years old when sold.
homes = pd.DataFrame({
    "sqft": [1511, 864, 848, 1616, 990, 1635, 1456, 1174, 1412, 1616, 2654, 1211, 1433, 2475, 1295, 1073],
    "rooms": [6, 5, 3, 5, 5, 5, 7, 5, 6, 7, 11, 5, 7, 7, 4, 4],
    "age": [13, 36, 6, 15, 11, 96, 33, 30, 70, 1, 87, 40, 45, 12, 29, 2],
    "price": [225.0, 120.5, 143.0, 236.5, 156.0, 117.0, 143.0, 158.0, 132.5, 232.0, 158.0, 134.0, 161.0, 355.0, 213.8, 157.0],   # $1,000s
})
### Your code starts here.



### Your code ends here.