Homework

13.3.3. Homework#

  • 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.

1. Reading a Coefficient

In a regression of house price (in dollars) on square feet, rooms, and age, the coefficient on square feet is 120. Holding rooms and age fixed, each additional square foot is associated with a $120 higher predicted price.

Hide code cell source

### Solution
# True
# A coefficient in multiple regression is the change in the prediction for a one-unit increase in that feature, with the other features held fixed.

2. Correlated Predictors

When two predictors are strongly correlated with each other, such as square feet and rooms, their individual coefficients can be unstable and hard to interpret.

Hide code cell source

### Solution
# True
# The model has trouble separating the effects of predictors that move together (multicollinearity).

3. Training Error

A model’s RMSE on its training data is the best estimate of how accurately it will predict the prices of new houses.

Hide code cell source

### Solution
# False
# Training error is optimistic because the model was fitted to those same houses. Test-set error is the honest estimate.

4. More Predictors

Adding more predictors to a regression can never make its predictions on new data worse.

Hide code cell source

### Solution
# False
# Extra predictors always lower training error but can fit noise (overfitting), which makes test-set predictions worse.

5. R-squared

A regression has an R-squared of 0.8 on the test set. Its predictions account for 80% of the variation in test-set prices.

Hide code cell source

### Solution
# True
# R-squared is 1 - SSE/SST: the share of the variation around the mean that the predictions explain.
### 6. Pricing Three Listings
#   1. Fit a LinearRegression model of price on sqft, rooms, and age using homes.
#   2. Predict the prices of the three listings, each rounded to 1 decimal.
# Print exactly:
# Listing 1: 211.5
# Listing 2: 352.4
# Listing 3: 296.4
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
})
listings = pd.DataFrame({
    "sqft": [1500, 2200, 1900],
    "rooms": [5, 7, 6],
    "age": [30, 10, 15],
})

### 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
})
listings = pd.DataFrame({
    "sqft": [1500, 2200, 1900],
    "rooms": [5, 7, 6],
    "age": [30, 10, 15],
})

model = LinearRegression().fit(homes[['sqft', 'rooms', 'age']], homes['price'])
for number, value in enumerate(model.predict(listings), 1):
    print("Listing " + str(number) + ":", round(value, 1))
Listing 1: 211.5
Listing 2: 352.4
Listing 3: 296.4
### 7. Test-Set Errors
# A model's predictions for six test houses are below.
#   1. Compute the residuals (actual minus predicted) as a list.
#   2. Compute the test RMSE with np.sqrt(mean_squared_error(...)), rounded to 2 decimals.
# Print exactly:
# Residuals: [-5, 10, -5, 15, -7, -5]
# Test RMSE: 8.65
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, r2_score

test = pd.DataFrame({
    "actual": [245, 310, 280, 355, 298, 330],
    "predicted": [250, 300, 285, 340, 305, 335],
})

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, r2_score

test = pd.DataFrame({
    "actual": [245, 310, 280, 355, 298, 330],
    "predicted": [250, 300, 285, 340, 305, 335],
})

residuals = test['actual'] - test['predicted']
print("Residuals:", residuals.tolist())
print("Test RMSE:", round(np.sqrt(mean_squared_error(test['actual'], test['predicted'])), 2))
Residuals: [-5, 10, -5, 15, -7, -5]
Test RMSE: 8.65
### 8. Nearest-Neighbor Regression
# Predict a house's price from the 3 most similar houses. Both features are
# already in standard units.
#   1. Compute the distance from new_house to each house (or use
#      KNeighborsRegressor(n_neighbors=3) from sklearn.neighbors).
#   2. Predict the price as the average price of the 3 nearest houses,
#      rounded to 1 decimal.
# Print exactly:
# Predicted price: 298.3
import numpy as np
import pandas as pd

houses = pd.DataFrame({
    "size_su": [-1.2, -0.5, 0.0, 0.4, 0.9, 1.5, -0.8, 0.2],
    "age_su": [0.8, -0.3, 0.5, -1.0, 0.2, -0.6, 1.2, -0.2],
    "price": [210, 265, 280, 330, 350, 410, 230, 300],
})
new_house = np.array([0.3, -0.4])

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

houses = pd.DataFrame({
    "size_su": [-1.2, -0.5, 0.0, 0.4, 0.9, 1.5, -0.8, 0.2],
    "age_su": [0.8, -0.3, 0.5, -1.0, 0.2, -0.6, 1.2, -0.2],
    "price": [210, 265, 280, 330, 350, 410, 230, 300],
})
new_house = np.array([0.3, -0.4])

features = houses[['size_su', 'age_su']]
houses['distance'] = np.sqrt(((features - new_house) ** 2).sum(axis=1))
nearest = houses.sort_values('distance').head(3)
print("Predicted price:", round(nearest['price'].mean(), 1))
Predicted price: 298.3
### 9. Checking Two Predictors
#   1. Compute the correlation between square feet and number of bathrooms,
#      rounded to 2 decimals.
#   2. Flag the pair if the absolute correlation is above 0.8.
# Print exactly:
# Correlation: 0.83
# Multicollinearity flag: True
import numpy as np
import pandas as pd

houses = pd.DataFrame({
    "sqft": [1200, 1500, 1700, 1900, 2200, 2600, 3000],
    "bathrooms": [1, 2, 2, 2, 3, 2, 4],
})

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

houses = pd.DataFrame({
    "sqft": [1200, 1500, 1700, 1900, 2200, 2600, 3000],
    "bathrooms": [1, 2, 2, 2, 3, 2, 4],
})

r = round(houses['sqft'].corr(houses['bathrooms']), 2)
print("Correlation:", r)
print("Multicollinearity flag:", abs(r) > 0.8)
Correlation: 0.83
Multicollinearity flag: True
### 10. Choosing a Model
# Three models of increasing complexity were fitted. Their RMSEs (in $1,000s) are below.
#   1. Print the model with the lowest TEST RMSE.
#   2. For the most complex model C, compute the gap: test RMSE minus training RMSE.
# Print exactly:
# Best model: B
# Gap for C: 12.5
import numpy as np
import pandas as pd

models = pd.DataFrame({
    "train_rmse": [24.0, 18.5, 9.0],
    "test_rmse": [25.5, 20.0, 21.5],
}, index=["A", "B", "C"])

### Your code starts here.



### Your code ends here.

Hide code cell source

### Solution
import numpy as np
import pandas as pd

models = pd.DataFrame({
    "train_rmse": [24.0, 18.5, 9.0],
    "test_rmse": [25.5, 20.0, 21.5],
}, index=["A", "B", "C"])

print("Best model:", models['test_rmse'].idxmin())
print("Gap for C:", models.loc['C', 'test_rmse'] - models.loc['C', 'train_rmse'])
# Model C fits the training data best but predicts new houses worse: overfitting.
Best model: B
Gap for C: 12.5