The business problem: a fair rent for every new listing
What it is
A flat-rental platform in Bengaluru wants to show owners a suggested monthly rent the moment they list, and to flag listings priced far above similar flats. Each past listing has its area (sq ft), bedrooms, bathrooms, building age, floor, distance to the nearest metro station, locality, furnishing, who listed it (owner, agent or builder), parking, the date it was listed, and the rent it actually went for. The target is that rent, in thousands of rupees a month.
Why it matters
This is a regression problem — the answer is a number, not a category — and the platform needs more than a number: owners ask *why* the suggestion is ₹42,000 and not ₹50,000. A linear model answers that directly, one feature at a time, which is why it is the first model to try and the one every fancier model has to beat.
In the real world: Rental and resale sites like NoBroker or 99acres suggest a price the moment an owner lists, and flag listings far above similar homes.
Visual example
Training dataFive past listings. **Everything but rent is an input; rent is what the model learns to predict.**
12,000 listings like these; 70% train the model and 30% are held back to test it.
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv("listings.csv") # 12,000 past listings
df["rent"].describe() # ₹ thousands a month
train, test = train_test_split(df, test_size=0.3, random_state=1)
# Locality has 30 values: replace each with its average rent — learned on TRAIN only.
loc_mean = train.groupby("locality")["rent"].mean()
train["locality_rent"] = train["locality"].map(loc_mean)
test["locality_rent"] = test["locality"].map(loc_mean)Encoding a category by its average target over the whole dataset before splitting. The test rows' own rents then leak into a feature, and that feature looks far more important than it is. Learn the averages on the training rows only, and apply them to the test rows.