13.3.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 has 16 recent sales (prices in $1,000s).
# 1. Fit a LinearRegression model that predicts price from sqft, rooms, and age.
# 2. Print the intercept and each coefficient, rounded to 2 decimals,
# in the order sqft, rooms, age.
# Print exactly:
# Intercept: 34.71
# sqft: 0.1
# rooms: 15.5
# age: -1.87
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
homes = pd.DataFrame({
"sqft": [2800, 2700, 2600, 2700, 1300, 2600, 2800, 1600, 1500, 1300, 2600, 2900, 2200, 2300, 2600, 1200],
"rooms": [9, 9, 8, 9, 5, 8, 9, 6, 6, 3, 9, 9, 7, 9, 9, 4],
"age": [44, 26, 42, 7, 45, 41, 27, 25, 17, 13, 15, 39, 20, 49, 20, 27],
"price": [400, 417, 350, 450, 151, 363, 390, 247, 259, 198, 403, 387, 326, 325, 409, 168], # $1,000s
})
### Your code starts here.
### Your code ends here.
Intercept: 34.71
sqft: 0.1
rooms: 15.5
age: -1.87
### Question 2: Holding Other Features Fixed
# 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. Compare it
# with the rooms coefficient.
# Print exactly:
# House A: 276.7
# House B: 292.2
# Difference: 15.5
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
homes = pd.DataFrame({
"sqft": [2800, 2700, 2600, 2700, 1300, 2600, 2800, 1600, 1500, 1300, 2600, 2900, 2200, 2300, 2600, 1200],
"rooms": [9, 9, 8, 9, 5, 8, 9, 6, 6, 3, 9, 9, 7, 9, 9, 4],
"age": [44, 26, 42, 7, 45, 41, 27, 25, 17, 13, 15, 39, 20, 49, 20, 27],
"price": [400, 417, 350, 450, 151, 363, 390, 247, 259, 198, 403, 387, 326, 325, 409, 168], # $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.
House A: 276.7
House B: 292.2
Difference: 15.5
### Question 3: Spotting Multicollinearity
# 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.94
import numpy as np
import pandas as pd
homes = pd.DataFrame({
"sqft": [2800, 2700, 2600, 2700, 1300, 2600, 2800, 1600, 1500, 1300, 2600, 2900, 2200, 2300, 2600, 1200],
"rooms": [9, 9, 8, 9, 5, 8, 9, 6, 6, 3, 9, 9, 7, 9, 9, 4],
"age": [44, 26, 42, 7, 45, 41, 27, 25, 17, 13, 15, 39, 20, 49, 20, 27],
"price": [400, 417, 350, 450, 151, 363, 390, 247, 259, 198, 403, 387, 326, 325, 409, 168], # $1,000s
})
### Your code starts here.
### Your code ends here.
Most correlated pair: sqft and rooms
Correlation: 0.94
### Question 4: Simple vs. Multiple on Test Data
# 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: 32.8
# Multiple RMSE: 7.38
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
homes = pd.DataFrame({
"sqft": [2800, 2700, 2600, 2700, 1300, 2600, 2800, 1600, 1500, 1300, 2600, 2900, 2200, 2300, 2600, 1200],
"rooms": [9, 9, 8, 9, 5, 8, 9, 6, 6, 3, 9, 9, 7, 9, 9, 4],
"age": [44, 26, 42, 7, 45, 41, 27, 25, 17, 13, 15, 39, 20, 49, 20, 27],
"price": [400, 417, 350, 450, 151, 363, 390, 247, 259, 198, 403, 387, 326, 325, 409, 168], # $1,000s
})
### Your code starts here.
### Your code ends here.
Simple RMSE: 32.8
Multiple RMSE: 7.38
### Question 5: R-squared on the Test Set
# 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 3 decimals.
# Print exactly:
# Test R-squared: 0.99
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
homes = pd.DataFrame({
"sqft": [2800, 2700, 2600, 2700, 1300, 2600, 2800, 1600, 1500, 1300, 2600, 2900, 2200, 2300, 2600, 1200],
"rooms": [9, 9, 8, 9, 5, 8, 9, 6, 6, 3, 9, 9, 7, 9, 9, 4],
"age": [44, 26, 42, 7, 45, 41, 27, 25, 17, 13, 15, 39, 20, 49, 20, 27],
"price": [400, 417, 350, 450, 151, 363, 390, 247, 259, 198, 403, 387, 326, 325, 409, 168], # $1,000s
})
### Your code starts here.
### Your code ends here.
Test R-squared: 0.99
Question 1
Question 2
Question 3
Question 4
Question 5