CoolFace
Apppublic

tschool01/zurich-apartment-price

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

Apartment Price Prediction (Regression)

Task: Predict monthly rental prices for apartments in the canton of Zurich


Summary of Iterative Process

IterationObjectiveKey ChangesModels UsedCV Mean R²CV Std DevChange in PerformanceFit Diagnosis
1Build baseline model- Drop missing values<br>- Drop duplicates<br>- Select 7 original features<br>- 5-fold CVLinear Regression<br>Random Forest (n_estimators=100)0.54 (LR)<br>0.49 (RF)RMSE std: 144 (LR)<br>135 (RF)BaselineLR: ☐ Overfitting ☑ Good Fit<br>RF: ☑ Overfitting ☐ Good Fit
2Improve generalisation- Add employment_rate feature<br>- Replace LR with Gradient Boosting<br>- Tune RF hyperparameters<br>- 5-fold CVGradient Boosting (n_estimators=200, lr=0.05)<br>Tuned Random Forest (n_estimators=200, max_depth=12)0.55 (GB)<br>0.57 (RF)RMSE std: 128 (GB)<br>146 (RF)+0.08 R² (RF vs iter 1 RF)<br>+0.03 R² (RF vs iter 1 LR)☑ Overfitting ☐ Good Fit (reduced gap vs iter 1)

Notes

Metric: R² and RMSE (CHF) – 5-Fold Cross-Validation

Created Features:

  • —employment_rate = emp / pop – fraction of the municipal population that is employed (new feature, not in prior exercises)

Final Selected Features:

  • —rooms – number of rooms
  • —area – living area in m²
  • —pop – municipal population
  • —pop_dens – population density (per km²)
  • —frg_pct – percentage of foreign residents
  • —emp – number of employees in municipality
  • —tax_income – average taxable income
  • —employment_rate – emp / pop (engineered)

Reason for Selection: Selected based on availability in the BFS municipality dataset (enabling live lookup in the app), feature importance from the Random Forest, and cross-validation performance improvement when adding employment_rate. The new feature reduces the gap between train and test R², indicating better generalisation.


Preprocessing Steps

  1. 1.Load raw apartment listing CSV merged with BFS municipality socioeconomic data
  2. 2.Remove rows with missing values (dropna())
  3. 3.Remove duplicate rows (drop_duplicates())
  4. 4.Engineer new feature: employment_rate = emp / pop
  5. 5.Select final 8 features as input; price (CHF/month) as target
  6. 6.No feature scaling applied – tree-based models are scale-invariant

Final Model

RandomForestRegressor (tuned, Iteration 2)

HyperparameterValue
n_estimators200
max_depth12
min_samples_leaf4
random_state42

Selected based on lowest cross-validation RMSE in Iteration 2. Trained on the full dataset and saved as apartment_price_model.pkl.


Files

FileDescription
model_training.ipynbFull training notebook with both iterations
app.pyGradio web application
apartment_price_model.pklSerialised final model + feature list
bfs_municipality_and_tax_data.csvMunicipality socioeconomic data (BFS)
requirements.txtPython dependencies