Predict a continuous value—and quantify the error
The learning task estimates a fictional property price from area, bedrooms, age, distance, parking, neighborhood and property type. Unlike classification, the target is continuous. A useful evaluation must say how far predictions are from actual values in currency units, not only report a dimensionless score.
Input
Property characteristics available before the fictional sale.
Target
Generated sale price in rupees.
Decision boundary
Educational regression—not valuation, lending or investment advice.
Success criteria
The model should beat a median-price baseline on held-out MAE, report RMSE and R², avoid test-set tuning, inspect errors across neighborhoods and attach a validation-derived residual range to predictions.
Generate learnable patterns without pretending they are real
| Feature | Type | Example relationship |
|---|---|---|
| area_sqft | Numeric | Larger area generally raises generated price |
| bedrooms | Numeric | Adds capacity but overlaps with area |
| age_years | Numeric | Older properties receive a synthetic reduction |
| distance_km | Numeric | Longer distance receives a synthetic reduction |
| parking_spaces | Numeric | Adds generated value |
| neighborhood | Categorical | Different fictional location effects |
| property_type | Categorical | Apartment/Independent/Duplex effects |
Noise prevents a perfect deterministic mapping. Missing values are injected into selected columns so preprocessing is exercised. The target formula is known only because this is a teaching generator; in a real project, relationships must be learned from ethically sourced, time-stamped transactions.
Train, validation and test have different jobs
- Train: fit imputers, encoders and each candidate forest.
- Validation: compare candidate maximum depths and estimate a residual radius.
- Test: evaluate the chosen workflow once.
Looking at test MAE and then changing depth makes the test part of model selection. Its result is no longer an unbiased final estimate. The program uses fixed random states so the demonstration is reproducible.
Why a random split is not always enough
Real property markets change over time and nearby properties are correlated. A production study may need a chronological holdout, group split by locality, or both. The split should mimic where and when the model will be used.
Learn preprocessing from training data only
Numeric branch
Median imputation for missing area or distance.
Categorical branch
Most-frequent imputation, then one-hot encoding with unknown-category handling.
A ColumnTransformer applies the correct branch to each column. Wrapping it with the regressor in one Pipeline ensures fitting preprocessing on train and applying the learned transformations consistently to validation, test and future records.
Random forest intuition
Each decision tree partitions the feature space with threshold rules and predicts an average target in a leaf. The forest trains many randomized trees and averages their predictions, reducing the instability of one tree and capturing nonlinear interactions.
ŷ(x) = (1/T) Σ treeₜ(x), for T trees
Maximum depth and minimum leaf size control complexity. More flexibility can lower training error but increase variance; validation data chooses among candidate depths.
Three metrics answer different questions
| Metric | Formula/meaning | Interpretation |
|---|---|---|
| MAE | mean |y−ŷ| | Typical absolute currency error |
| RMSE | sqrt(mean (y−ŷ)²) | Penalizes large misses more |
| R² | 1−SSE/SST | Improvement relative to mean prediction |
The DummyRegressor predicts the training median. If the forest does not improve materially over this baseline, its complexity is unjustified.
Validation-derived residual radius
Let residual magnitude be |y−ŷ|. The program takes the 90th percentile on validation and reports prediction ± radius, then measures test coverage. This is an educational empirical interval, not a formal guarantee: coverage can fail under distribution shift and residual size may vary across property types.
Slice audit
Overall MAE can hide weak localities. Test absolute errors are grouped by neighborhood with both count and mean. Small groups should be interpreted cautiously.
Reproducible Python ML workflow
Loading source…Install the package versions listed in requirements.txt and run locally. The script downloads nothing: all records are reproducibly generated and explicitly labeled synthetic.
Trace one honest experiment
- Create educational data.
- Protect final evaluation.
- Create selection set.
- Establish minimum comparison.
- Train candidates.
- Select complexity.
- Estimate empirical uncertainty.
- Evaluate generalization.
- Audit uncertainty and slices.
- Interpret carefully.
Press Next to begin.
Test data boundaries as well as functions
Reproducible generation
Pipeline missing values
Unseen category
No test-driven selection
Metric sanity
Baseline comparison
A model learns its dataset—not “the housing market”
- Synthetic coefficients and feature importances do not prove causal effects.
- Random splits may overstate transfer to future dates or unseen areas.
- Location encoded as four labels is far simpler than real spatial variation.
- Unrecorded renovation, legal status, plot conditions and negotiation create omitted-variable error.
- An empirical interval from one validation distribution is not guaranteed after shift.
Deployment checklist
A real system needs lawful data provenance, documented intended users, time-aware backtesting, drift monitoring, subgroup and geographic error audits, prediction logging, human review, appeal/correction pathways, security and scheduled revalidation.
Test experimental judgment
Where should maximum depth be selected?
Which metric most directly reports typical absolute currency error?
Extensions
- Compare Ridge regression and gradient boosting with identical splits.
- Use time-based splitting and quantify performance decay.
- Plot residuals versus predicted price and area.
- Build conformal intervals on a separate calibration set.
- Create a model card covering data, metrics, limits and monitoring.
Explain evaluation—not only the algorithm
Why use a baseline?
It establishes whether learned features improve on a simple constant prediction. Without it, an apparently good metric lacks context.
Why put preprocessing in a pipeline?
It learns imputations and encodings only during fit and applies the same transformation sequence consistently, reducing leakage and deployment mismatch.
MAE versus RMSE?
MAE is directly interpretable and less dominated by extreme errors; RMSE weights large residuals more heavily. Reporting both reveals tail behavior.
Can R² be negative?
Yes. On evaluation data, a model can perform worse in squared error than predicting the target mean.
Why audit neighborhoods?
Aggregate performance can hide large errors in particular groups or regions; slice count and error expose uneven reliability.
What is data leakage?
Information unavailable at prediction time—or information learned from validation/test during training—enters the model and creates unrealistically optimistic evaluation.
A regression prediction is incomplete without an error story
The valuable workflow protects its test set, learns preprocessing within a pipeline, beats a baseline, expresses errors in meaningful units, examines residuals and states where evidence ends. The forest is one component; experimental design and responsible interpretation make the result defensible.
