Skip to content

Ashwashhere/MachineLearningAutoTrader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

🏎️ Predictive Analytics: Autotrader Car Valuation Engine

An end-to-end Machine Learning regression pipeline built to model non-linear vehicle depreciation and predict used car market prices with high precision.

Python Scikit Learn Pandas NumPy Seaborn Matplotlib


Autotrader Car Valuation Project Overview

📌 Overview

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.


🧹 Data Exploration & Processing Strategy

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_age feature 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.

⚙️ Modeling Pipeline & Hyperparameter Tuning

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:

  1. Linear Regression: Evaluated as a baseline benchmark model to test linear feature relationships.
  2. K-Nearest Neighbors (KNN Regressor): Non-parametric distance estimator tuned for optimal $k$-neighbors ($k=7$) and distance-weighted neighbor scoring.
  3. Decision Tree Regressor: Non-linear tree-based estimator optimized with a maximum depth limit of 15 and min_samples_leaf=4 to curb variance and overfitting.
  4. 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.

📊 Model Evaluation & Results

Models were evaluated on the unseen test set using R-Squared ($R^2$), Root Mean Squared Error ($\text{RMSE}$), Mean Absolute Error ($\text{MAE}$), and Mean Absolute Percentage Error ($\text{MAPE}$). Metrics were converted back from log-space to actual British Pounds (£) for clinical interpretability:

Model Architecture $R^2$ Score 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%

Key Insights:

  • 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 & Market Drivers

Feature importance extraction from the champion Random Forest Regressor identified the top structural drivers of used vehicle values:

  1. Vehicle Age (Year of Registration): The dominant predictor establishing the baseline pricing tier and overall depreciation bracket.
  2. Mileage: The primary usage metric that adjusts value within a vehicle's specific age group.
  3. Standard Model / Target Encoded Brand: Critical for distinguishing segment pricing tiers (e.g., Premium vs. Mass Market vehicles).

🏗️ Directory Structure

├── 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

🚀 Getting Started

1. Clone the Repository

git clone https://github.com/your-username/your-repo-name.git
cd your-repo-name

2. Install Dependencies

pip install pandas numpy scikit-learn seaborn matplotlib

3. Run the ML Pipeline

python3 atml.py

Developed as a machine learning investigation into vehicle price prediction and automated valuation modeling.

About

This project implements a comprehensive supervised machine learning pipeline to predict the selling price of vehicles using the Autotrader "Car Sale Adverts" dataset. Developed in Python, the technical workflow is structured around high-performance libraries including Pandas, Scikit-Learn, and Seaborn.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors