A complete, realistic worked case study — predicting apartment rent from a handful of features, from raw data to a business-interpretable conclusion, focused on reading the model's output correctly rather than just producing it.
The Business Question
A property management company wants to understand: how much does apartment size actually influence monthly rent, and can a simple model estimate fair rent for a new listing?
The Dataset
import pandas as pd
df = pd.DataFrame({
"size_sqft": [450, 600, 700, 800, 950, 1100, 1250, 1400, 1550, 1700],
"bedrooms": [1, 1, 1, 2, 2, 2, 3, 3, 3, 4],
"distance_km": [8, 6, 7, 5, 4, 5, 3, 2, 3, 1], # distance from city center
"rent": [12000, 15500, 16800, 21000, 24500, 26200, 32000, 36500, 38800, 45000],
})
print(df.describe())
Quick EDA First
print(df.corr(numeric_only=True)["rent"])
# size_sqft and bedrooms both correlate strongly and positively with rent;
# distance_km correlates negatively (further from center -> cheaper rent, as expected)
Fitting the Model
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X = df[["size_sqft", "bedrooms", "distance_km"]]
y = df["rent"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression().fit(X_train, y_train)
print(model.intercept_)
print(dict(zip(X.columns, model.coef_)))
Interpreting the Coefficients — The Actual Point of This Example
Suppose the fitted coefficients come out approximately: \(b_{\text{size\_sqft}} \approx 22\), \(b_{\text{bedrooms}} \approx 850\), \(b_{\text{distance\_km}} \approx -900\). The business-readable translation:
- Each additional square foot is associated with about ₹22 more monthly rent, holding bedroom count and distance constant.
- Each additional bedroom is associated with about ₹850 more rent, holding size and distance constant — notably smaller than you might expect once size is already accounted for, since bedroom count and size are themselves correlated.
- Each additional km from the city center is associated with about ₹900 less rent, holding size and bedrooms constant.
This is exactly the kind of statement a business stakeholder can act on — and exactly why linear regression's interpretability is often worth more than a small accuracy gain from a black-box model.
Evaluating and Being Honest About Limits
from sklearn.metrics import mean_absolute_error, r2_score
y_pred = model.predict(X_test)
print("MAE:", mean_absolute_error(y_test, y_pred))
print("R²:", r2_score(y_test, y_pred))
Being realistic: on real rental data (not this clean toy example), expect R² well below 1.0 — rent also depends on factors this model doesn't capture (building age, amenities, floor level, market conditions). Reporting "our model explains roughly 70% of rent variation, with average error of ₹X" is a far more honest and useful summary than an unqualified prediction.
Using the Model for a New Prediction
new_listing = [[900, 2, 4]] # 900 sqft, 2 bedrooms, 4 km from center
predicted_rent = model.predict(new_listing)
print(predicted_rent)
In practice, this single-point prediction should be paired with a sense of typical error (from the MAE above) — "approximately ₹24,000, ± ₹1,500 based on typical model error" is more useful and honest than a bare point estimate.
Common Mistakes
- Reporting only the prediction without any sense of the model's typical error margin.
- Interpreting coefficients causally ("more bedrooms directly causes higher rent") when the data is observational — correlated features and confounders complicate a strict causal reading.
Interview Relevance
Q: "Walk me through how you'd explain a linear regression model's results to a non-technical stakeholder." Translate each coefficient into a plain-language "holding other factors constant" statement, report the model's typical error (MAE) alongside any prediction, and be explicit about what the model doesn't account for rather than implying it's more complete than it is.
Practice Question
The model above gives \(b_{\text{bedrooms}} \approx 850\), noticeably smaller than a naive expectation. Explain, using the multiple regression coefficient interpretation, why this might be lower than you'd expect from bedrooms alone.