Lab

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.

Hide code cell source

### Solution
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
})

X = homes[['sqft', 'rooms', 'age']]
y = homes['price']
model = LinearRegression().fit(X, y)

print("Intercept:", round(model.intercept_, 2))
for name, coefficient in zip(X.columns, model.coef_):
    print(name + ":", round(coefficient, 2))
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.

Hide code cell source

### Solution
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]})

model = LinearRegression().fit(homes[['sqft', 'rooms', 'age']], homes['price'])
price_a = model.predict(house_a)[0]
price_b = model.predict(house_b)[0]

print("House A:", round(price_a, 1))
print("House B:", round(price_b, 1))
print("Difference:", round(price_b - price_a, 2))
# The difference is the rooms coefficient: the change in predicted price for
# one more room, with square feet and age held fixed.
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.

Hide code cell source

### Solution
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
})

predictors = homes[['sqft', 'rooms', 'age']]
corr = predictors.corr()
columns = list(predictors.columns)

best_pair = None
best_value = 0
for i in range(len(columns)):
    for j in range(i + 1, len(columns)):
        value = corr.iloc[i, j]
        if abs(value) > abs(best_value):
            best_value = value
            best_pair = (columns[i], columns[j])

print("Most correlated pair:", best_pair[0], "and", best_pair[1])
print("Correlation:", round(best_value, 2))
# Bigger houses have more rooms, so the two coefficients are hard to separate.
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.

Hide code cell source

### Solution
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
})

X = homes[['sqft', 'rooms', 'age']]
y = homes['price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

simple = LinearRegression().fit(X_train[['sqft']], y_train)
multiple = LinearRegression().fit(X_train, y_train)

simple_rmse = np.sqrt(mean_squared_error(y_test, simple.predict(X_test[['sqft']])))
multiple_rmse = np.sqrt(mean_squared_error(y_test, multiple.predict(X_test)))
print("Simple RMSE:", round(simple_rmse, 2))
print("Multiple RMSE:", round(multiple_rmse, 2))
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.

Hide code cell source

### Solution
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
})

X = homes[['sqft', 'rooms', 'age']]
y = homes['price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

multiple = LinearRegression().fit(X_train, y_train)
print("Test R-squared:", round(r2_score(y_test, multiple.predict(X_test)), 3))
# The predictions account for about 99% of the variation in test prices.
# With only 4 test houses, treat that as a rough figure.
Test R-squared: 0.99