CoolFace
Apppublic

dubattim/zurich-apartment-price-predictor

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

Apartment Price Prediction - Model Iterations Documentation

Task: Apartment Rental Price Prediction (Regression)


Overview

This project predicts monthly rental prices for apartments in the Canton of Zurich using a Random Forest regressor. The model was developed iteratively over three iterations, progressively adding new features and tuning hyperparameters. The final model achieves an R² of 0.66 using 5-fold cross-validation.


Summary of Iterative Process

IterationObjectiveKey ChangesModels UsedCV Mean R²CV Std DevChange in PerformanceFit Diagnosis
1Build baseline model- Outlier removal (price, area)<br>- Drop missing values<br>- 7 original features<br>- 5-fold CVRandom Forest (nestimators=100)<br>Ridge Regression (alpha=1.0)<br>Gradient Boosting (nestimators=100)0.54 (Ridge)<br>0.51 (GB)<br>0.46 (RF)0.09<br>0.06<br>0.05Baseline☑ Underfitting
2Add new features + tuning- Added distance_to_zurich (haversine distance to Zurich HB)<br>- Added area_per_room (derived feature)<br>- Hyperparameter tuning<br>- 5-fold CVTuned RF (nestimators=300, maxdepth=20)<br>Ridge (alpha=1.0)<br>Tuned GB (nestimators=300, maxdepth=5)<br>Lasso (alpha=1.0)0.59 (RF)<br>0.53 (Lasso)<br>0.53 (Ridge)<br>0.52 (GB)0.06<br>0.08<br>0.08<br>0.08+0.13 (RF)☐ Overfitting ☐ Underfitting ☑ Good Fit
3Log-transform target- Log1p transformation of target variable<br>- Enhanced features from Iter 2<br>- Increased n_estimators to 500<br>- 5-fold CVRF (nestimators=500, maxdepth=20)<br>GB (nestimators=500, maxdepth=5, lr=0.05)0.66 (RF)<br>0.61 (GB)0.05<br>0.06+0.07 (RF)☐ Overfitting ☐ Underfitting ☑ Good Fit

Preprocessing Steps

  1. 1.Data loading: Loaded 804 apartment listings from enriched dataset (week 2), merged with BFS municipality demographic data
  2. 2.Missing value handling: Dropped rows missing essential features (rooms, area, price, population stats, coordinates)
  3. 3.Outlier removal: Removed apartments with price < 200 or > 15,000 CHF, area < 10 or > 500 m², rooms <= 0
  4. 4.Feature engineering:
  5. 5.distance_to_zurich (NEW): Haversine distance in km from apartment to Zurich main station (47.3769°N, 8.5417°E). Computed per listing, then averaged per municipality for the prediction app.
  6. 6.area_per_room: Living area divided by number of rooms
  7. 7.Target transformation (Iteration 3): Applied log1p to the rental price for training; predictions are converted back with expm1
  8. 8.String cleaning: Converted tax_income from formatted strings (e.g., "108'788") to float

Features Used (9 total)

FeatureSourceDescription
roomsUser inputNumber of rooms
areaUser inputLiving area in m²
popBFS dataMunicipality population
pop_densBFS dataPopulation density
frg_pctBFS dataForeign resident percentage
empBFS dataNumber of employed persons
tax_incomeBFS dataAverage taxable income
distancetozurichEngineered (NEW)Haversine distance to Zurich HB in km
areaperroomEngineeredarea / rooms

Feature Importances (Final Model)

FeatureImportance
area0.4679
distancetozurich0.2777
rooms0.1148
areaperroom0.0488
pop_dens0.0246
tax_income0.0242
pop0.0233
frg_pct0.0100
emp0.0087

The newly engineered distance_to_zurich is the second most important feature (28%), confirming that proximity to the city center is a strong predictor of rental prices.


Evaluation Method

  • —Metric: R² (coefficient of determination)
  • —Validation: 5-fold cross-validation on the full dataset
  • —All models evaluated with the same CV splits for fair comparison

Final Selected Model

  • —Model: Random Forest Regressor
  • —Hyperparameters: nestimators=500, maxdepth=20, minsamplessplit=5, minsamplesleaf=2, random_state=42
  • —Target transformation: log1p (predictions converted back with expm1)
  • —CV R²: 0.6613 (±0.0501)
  • —Reason for selection: Highest cross-validation R² across all iterations and models. The log transformation improved performance by 0.07 R² over the non-log version.

Application

The Gradio web app (app.py) accepts:

  • —Number of rooms
  • —Living area (m²)
  • —Town (dropdown of 100+ Zurich-area municipalities)

It looks up municipality data from the enriched BFS dataset, computes area_per_room, and returns the predicted monthly rent in CHF.


Files

FileDescription
train_model.pyFull training script with all 3 iterations
app.pyGradio web application
apartment_price_model.pklTrained Random Forest model
model_features.pklOrdered feature list
bfs_municipality_data_enriched.csvBFS data with distancetozurich
bfs_municipality_and_tax_data.csvOriginal BFS municipality data
requirements.txtPython dependencies