CASE STUDY 02 · END-TO-END REGRESSION

Core ML Random Forest + Residual Audit

House-Price Prediction

Develop a leakage-safe regression experiment with synthetic properties, a median baseline, validation-only model selection, held-out evaluation and honest uncertainty limits.

01 · PROBLEM FRAMING

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.

Responsible-use boundary: The dataset and market relationships are synthetic. A model that performs well here has no demonstrated validity for any real neighborhood, date or property.

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.

02 · DATA & TARGET DESIGN

Generate learnable patterns without pretending they are real

FeatureTypeExample relationship
area_sqftNumericLarger area generally raises generated price
bedroomsNumericAdds capacity but overlaps with area
age_yearsNumericOlder properties receive a synthetic reduction
distance_kmNumericLonger distance receives a synthetic reduction
parking_spacesNumericAdds generated value
neighborhoodCategoricalDifferent fictional location effects
property_typeCategoricalApartment/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.

Leakage question: A final negotiated sale amount, post-sale tax or future appraisal would not be a valid pre-sale feature. Availability at prediction time matters more than correlation.
03 · EXPERIMENTAL BOUNDARY

Train, validation and test have different jobs

All synthetic rows
60% train
20% validation
20% untouched test
  • 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.

04 · LEAKAGE-SAFE PIPELINE

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.

05 · BASELINE, METRICS & RESIDUALS

Three metrics answer different questions

MetricFormula/meaningInterpretation
MAEmean |y−ŷ|Typical absolute currency error
RMSEsqrt(mean (y−ŷ)²)Penalizes large misses more
1−SSE/SSTImprovement 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.

06 · COMPLETE IMPLEMENTATION

Reproducible Python ML workflow

programs/house-price-prediction.py
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.

07 · INTERACTIVE WORKFLOW TRACE

Trace one honest experiment

  1. Create educational data.
  2. Protect final evaluation.
  3. Create selection set.
  4. Establish minimum comparison.
  5. Train candidates.
  6. Select complexity.
  7. Estimate empirical uncertainty.
  8. Evaluate generalization.
  9. Audit uncertainty and slices.
  10. Interpret carefully.
Current state

Press Next to begin.

08 · WORKFLOW TESTS

Test data boundaries as well as functions

Reproducible generation
Two calls with the same row count and random state must produce identical records and missing positions.
Pipeline missing values
Training and prediction must finish when supported columns contain missing values.
Unseen category
A future property with a new property_type must transform without crashing because the encoder ignores unknown categories.
No test-driven selection
Inspect code/data flow: candidate ranking may reference validation targets but never test targets.
Metric sanity
MAE/RMSE are non-negative, RMSE≥MAE, and identical y/ŷ yields zero error and R²=1.
Baseline comparison
Report both models on the same untouched test rows; never compare validation forest with test baseline.
09 · ERROR ANALYSIS, LIMITS & MONITORING

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.

Do not infer causation: Feature importance shows how this fitted model used features to predict its generated target. Changing an important feature does not prove a real property’s value would change by that amount.
10 · KNOWLEDGE CHECK & EXTENSIONS

Test experimental judgment

Where should maximum depth be selected?

Which metric most directly reports typical absolute currency error?

Extensions

  1. Compare Ridge regression and gradient boosting with identical splits.
  2. Use time-based splitting and quantify performance decay.
  3. Plot residuals versus predicted price and area.
  4. Build conformal intervals on a separate calibration set.
  5. Create a model card covering data, metrics, limits and monitoring.
11 · INTERVIEW PREPARATION

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.

12 · KEY TAKEAWAY

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.