An end-to-end Machine Learning regression pipeline built to model non-linear vehicle depreciation and predict used car market prices with high precision.
This project implements an end-to-end Machine Learning pipeline to predict the selling price of used cars based on the Autotrader "Car Sale Adverts" dataset. Predicting vehicle market value is a continuous regression task complicated by steep non-linear depreciation curves, heavy right-skewed pricing distributions, and high-cardinality categorical attributes (e.g., specific vehicle makes and standard models).
The primary objective is to build a robust, leakage-free predictive model that minimizes absolute percentage error across various market segments.
To prepare the raw data for modeling while maintaining strict adherence to machine learning best practices, data processing was decoupled into stateless and stateful operations:
-
Target Transformation (Log Scaling): Car prices and mileages exhibited strong positive right-skewness. A logarithmic transformation (
np.log1p) was applied to the target variable to compress scale, stabilize variance, and optimize algorithms for percentage-based evaluation. -
Feature Engineering: A ratio-scale
vehicle_agefeature was derived from registration data to capture depreciation directly. Invalid entries (e.g., registration years prior to 1886 or future dates) were cleaned and filtered out. -
Leakage-Free Imputation: Preprocessing parameters were derived solely from the training partition (
$X_{\text{train}}$ ). Continuous numerical missing values were imputed using median strategies, while categorical missingness was imputed using the mode. -
Hybrid Categorical Encoding:
- Low-Cardinality Features (< 100 unique values): Encoded using One-Hot Encoding.
-
High-Cardinality Features (> 100 unique values, e.g.,
standard_model): Encoded using Target Encoding (Mean Target Mapping) with smoothed out-of-fold training targets to prevent the curse of dimensionality.
-
Outlier Clipping & Feature Scaling: Extreme numerical values were clipped at the 1st and 99th percentiles of
$X_{\text{train}}$ to prevent distortion. Standard Scaling ($Z$ -score normalization) was applied within pipelines to optimize distance-based estimators. - Train / Test Partitioning: An 80/20 train-test split was established using a fixed random seed prior to fitting stateful transformers.
All data transformation steps were encapsulated inside Scikit-Learn Pipeline and ColumnTransformer objects to guarantee zero data leakage during cross-validation and testing. Four distinct model families were benchmarked and optimized via GridSearchCV:
- Linear Regression: Evaluated as a baseline benchmark model to test linear feature relationships.
-
K-Nearest Neighbors (KNN Regressor): Non-parametric distance estimator tuned for optimal
$k$ -neighbors ($k=7$ ) and distance-weighted neighbor scoring. -
Decision Tree Regressor: Non-linear tree-based estimator optimized with a maximum depth limit of 15 and
min_samples_leaf=4to curb variance and overfitting. -
Random Forest Regressor: Ensemble bagging architecture tuned across estimators (
$n=100$ ), tree depth ($\text{max\_depth}=20$ ), and minimum node splits ($\text{min\_samples\_split}=5$ ) to minimize variance and generalize complex interactions.
Models were evaluated on the unseen test set using R-Squared (
| Model Architecture |
|
RMSE (£) | MAE (£) | MAPE (%) |
|---|---|---|---|---|
| Random Forest Regressor | 0.8997 | £3,780.01 | £1,963.86 | 15.58% |
| Decision Tree Regressor | 0.8541 | £4,558.10 | £2,259.61 | 17.07% |
| K-Nearest Neighbors (KNN) | 0.8198 | £5,064.08 | £2,884.28 | 23.19% |
| Linear Regression | 0.7711 | £5,705.80 | £3,450.48 | 35.47% |
-
Predictive Superiority: The Random Forest Regressor achieved the highest accuracy, explaining approximately 90% of variance (
$R^2 = 0.8997$ ) in vehicle market price. - Percentage Error Accuracy: The model achieved a MAPE of 15.58%, demonstrating strong valuation precision across both economy and luxury market segments.
- Residual Stability: Residual plots confirmed homoscedastic error distributions, demonstrating that the ensemble approach successfully eliminated individual tree overfitting.
Feature importance extraction from the champion Random Forest Regressor identified the top structural drivers of used vehicle values:
- Vehicle Age (Year of Registration): The dominant predictor establishing the baseline pricing tier and overall depreciation bracket.
- Mileage: The primary usage metric that adjusts value within a vehicle's specific age group.
- Standard Model / Target Encoded Brand: Critical for distinguishing segment pricing tiers (e.g., Premium vs. Mass Market vehicles).
├── atml.py # Complete Machine Learning pipeline & notebook source
├── portfolio_thumbnail.png # High-resolution visual banner for portfolio summary
├── adverts.csv # Raw Autotrader car sale dataset
└── README.md # Repository documentation
git clone https://github.com/your-username/your-repo-name.git
cd your-repo-namepip install pandas numpy scikit-learn seaborn matplotlibpython3 atml.pyDeveloped as a machine learning investigation into vehicle price prediction and automated valuation modeling.