…
…
Clean messy apartment rental listings, explain the rent with a log transformation, correlation and multiple regression, find the expensive listings, and predict the rent of a new apartment.
A real-estate site shared the apartment rental listings it collected from six neighborhoods of Port Alder in fall 2026. The site wants to add a "This price is high / reasonable for the area" label to its listing pages. For that it needs answers to two questions: What determines the rent, and is a listing expensive compared with similar ones?
Data: listings.csv, each row is one listing.
| Column | Description |
|---|---|
listing_id | Listing number |
neighborhood | Harborside, Old Town, Riverside, Midtown, Southgate, Eastfield |
bedrooms | 1BR, 2BR, 3BR, 4BR |
area_m2, building_age, floor | Gross area in square meters, age of the building, floor it is on (−1 basement, 0 ground) |
furnished | yes / no |
metro_min | Walking minutes to the nearest metro station |
rent | Monthly rent ($) |
Some listings were published again for the same apartment, and a few have a wrong rent or area. The values are sample data.
Log model: Rents are skewed to the right, and effects are often proportional (being furnished changes the rent by a percentage, not by a fixed amount). That is why the model uses the natural logarithm of the rent (np.log). In such a model, when a variable with coefficient b goes up by one unit, the rent changes by about exp(b) − 1; the coefficient of log area shows how many percent the rent changes for a one percent change in area.
Lessons used: repeats and outliers (14–15), log transformation (16), correlation (34), simple and multiple regression (35–36).
What you'll do
listings.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
First look at the summary of the numeric columns: are the smallest and largest area and rent reasonable? Then check whether the same apartment was published again. A republished listing has a different number but all its other columns are the same, so we look for repeats using the columns other than listing_id.
This step's code cell is for members.Sign up free
Drop the repeats and compute the rent per square meter. Use two business rules: the area must be between 25 and 400, and the rent per square meter must not be extremely unusual. For "extremely unusual" use the strict form of the IQR rule: Q3 + 3 × IQR. We choose the multiplier 3 instead of 1.5 so that we do not delete real listings in expensive neighborhoods.
Before deleting, print the faulty rows and check whether they really are errors. Finally, convert the furnished column to 1/0.
This step's code cell is for members.Sign up free
Are rent and area skewed to the right? Add their natural logarithms as the columns
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
log_rentlog_area"2BR"Look at the median rent by neighborhood to see which neighborhoods are expensive.
This step's code cell is for members.Sign up free
Look at the correlation of the numeric variables with log_rent. Which are strong and which are weak? Why could the correlation of being furnished be so close to zero?
Also print the correlation between log_area and bedroom_count. If two explanatory variables are strongly related to each other, putting both in the model makes the coefficients unstable (multicollinearity). That is why we will use only the area in the model.
This step's code cell is for members.Sign up free
First build a model with area only: log_rent ~ log_area. Because both sides use logarithms, the slope is an elasticity: when the area grows by 1 percent, the rent grows by about the slope in percent. R² shows how much of the differences in rent the area explains.
This step's code cell is for members.Sign up free
Area explains only about half of the rents. Add building age, being furnished, the distance to the metro and the neighborhood (categorical) to the model. The reference category for the neighborhood is the first neighborhood alphabetically, Eastfield; the neighborhood coefficients show the difference from Eastfield.
Interpret the coefficients as percentages with exp(b) − 1. The correlation of being furnished came out close to zero. What does its coefficient say now?
This step's code cell is for members.Sign up free
Convert the model's prediction back to dollars with np.exp(m.fittedvalues). How much higher the actual rent is than the prediction (gap) is the basis of the site's "high" label. List the five most expensive listings and show the actual rent against the prediction in a scatter plot with a diagonal reference line.
Finally, predict the rent of a new listing. The model was built with apartments between 50 and 200 m²; a prediction for an apartment outside this range would be unreliable.
This step's code cell is for members.Sign up free