A nonlinear mapping transforms features into predictions through relationships that a straight-line model cannot represent. It matters when a feature’s effect changes across its range or when features interact. section 7.2.3 section 1.3
A linear baseline assigns each feature an unbounded contribution that simply increases or decreases with its value. This can create implausible results when domain patterns are conditional: many bedrooms may indicate a hostel, while a long minimum stay may indicate seasonal accommodation. section 7.2.3
Address nonlinear mappings through complementary approaches:
- Engineer transformed features so a linear model can approximate nonlinear relationships, using relevant signals and domain knowledge. section 1.3
- Use a model that represents nonlinearities and interactions directly when the baseline is too simple; XGBoost is used as the more sophisticated next model. section 7.2.3
- Expect a trade-off: manual feature engineering requires problem knowledge, whereas deep learning can discover nonlinear dependencies through representation learning. section 1.3
Tokyo Airbnb prediction
A linear regression baseline produced negative predictions and extreme positive outliers. Inspecting individual feature contributions linked those errors to unusually high bedroom counts or minimum-night values, motivating nonlinearities and interactions. section 7.2.3
Listing 7.14 Linear regression baseline model with diagnostic plots
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
lm = LinearRegression(fit_intercept=False) #1
lm.fit(Xt, y) #2
cv_splits = cv.split(
X,
y = X["neighbourhood_more_than_30"]
) #3
y_pred_cv = cross_val_predict(
lm, Xt, y, cv=cv_splits
) #4
prediction_range = y_pred_cv.min()} - {y_pred_cv.max()
print(f"prediction range: {prediction_range}") #5
r2 = r2_score(y, y_pred_cv)
rmse = np.sqrt(mean_squared_error(y, y_pred_cv))
mae = mean_absolute_error(y, y_pred_cv) #6
print(f'R-squared: {r2:.3f}')
print(f'RMSE: {rmse:.3f}')
print(f'MAE: {mae:.3f}')
plt.scatter(y, y_pred_cv) #7
plt.plot([y.min(), y.max()], [y.min(), y.max()], 'r--', label='Ideal Fit')
plt.axhline(
0, color='orange', linestyle='--', label='Zero Line'
) #8
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.title('Linear Regression - Fitted Results')
plt.legend()
plt.show()
Inspect the runnable linear-regression baseline, prediction range, diagnostics, and per-feature contributions that reveal where linear assumptions fail. section 7.2.3






