YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

πŸš— Car Price Intelligence: From Raw Specs to Market Predictions

Python Scikit-Learn Pandas Kaggle License Status

A full-cycle Machine Learning pipeline for automotive price prediction and market segmentation

Regression Β· Classification Β· Clustering Β· Feature Engineering Β· Dimensionality Reduction


Project Overview & Data Walkthrough

πŸ“‹ Abstract

This project presents a comprehensive Machine Learning study on the Car Features and MSRP dataset (CooperUnion, Kaggle), comprising 11,914 vehicles spanning model years 1990–2017 across 16 features. The central research question is:

Which observable car attributes β€” engine specifications, drivetrain configuration, brand identity, and fuel efficiency β€” most significantly determine a vehicle's Manufacturer's Suggested Retail Price (MSRP), and can these relationships be leveraged to build a deployable price prediction system?

The pipeline encompasses the full ML lifecycle: structured Exploratory Data Analysis, OOP-based Data Cleaning with intelligent imputation, multi-strategy Feature Engineering (including unsupervised clustering), Regression modelling for continuous price prediction, and a Regression-to-Classification conversion for market-tier segmentation. The final winning models are exported and made available in this repository for inference and further research.


πŸ“ Repository Contents

β”œβ”€β”€ car_price_model.pkl          # πŸ† Winning regression model (Gradient Boosting)
β”œβ”€β”€ classification_model.pkl     # πŸ† Winning classifier (Gradient Boosting)
β”œβ”€β”€ correlation_heatmap.png      # EDA β€” feature correlation matrix
β”œβ”€β”€ cluster_pca.png              # Feature Engineering β€” K-Means + PCA
β”œβ”€β”€ confusion_matrix_gb.png      # Classification β€” Gradient Boosting confusion matrix
β”œβ”€β”€ feature_importance_reg.png   # Regression β€” top feature importances
β”œβ”€β”€ feature_importance_cls.png   # Classification β€” top feature importances
└── README.md

πŸ“Š Interactive EDA β€” The Data Story

The Dataset at a Glance

Property Value
Source Kaggle β€” CooperUnion/cardataset
Raw Shape 11,914 rows Γ— 16 columns
Final Shape (post-cleaning) β‰₯ 10,000 rows Γ— 30+ engineered columns
Target Variable MSRP (USD) β€” continuous for regression, 3-class for classification
Feature Mix 8 numeric + 5 categorical (+ engineered features)
Missing Values Engine HP (0.58%), Engine Cylinders (0.25%), Market Category (31.4%)

πŸ” Key EDA Findings

  • MSRP is heavily right-skewed β€” a small cohort of exotic vehicles (Bugatti, Lamborghini, Maybach) creates a long tail exceeding $200k, making log-transformation essential for linear models.
  • Engine HP is the single strongest predictor of price (Pearson r β‰ˆ +0.65), with a non-linear, accelerating relationship β€” a 100-HP gain at the high end adds disproportionately more to price than the same gain at the low end.
  • Fuel efficiency (MPG) is inversely correlated with price overall, but this relationship inverts within the SUV segment β€” larger, more expensive SUVs also consume more fuel, a key anomaly surfaced during EDA.
  • Brand identity (captured via Target Encoding) is the dominant pricing signal when combined with engine specs, explaining the majority of variance in the model.

🌑️ Correlation Heatmap

Correlation Heatmap β€” Numeric Features vs MSRP

The heatmap reveals strong positive correlations between MSRP and engine-related features (HP, Cylinders), and a meaningful negative correlation with fuel efficiency metrics β€” confirming that performance and economy sit at opposite ends of the price spectrum.


βš—οΈ The "Secret Sauce" β€” Feature Engineering

Feature engineering is where this project goes beyond a standard tutorial. Rather than feeding raw columns into a model, we constructed a rich, information-dense feature space through four compounding strategies:

1. 🏷️ Intelligent Categorical Encoding

NaΓ―ve one-hot encoding of Make (48 unique brands) would add 47 sparse binary columns with no ordinal information. Instead:

Column Strategy Rationale
Make Target Encoding (mean MSRP per brand) Condenses brand prestige into one continuous signal
Model Frequency Encoding (count per model) Encodes market popularity without dimensionality explosion
Driven Wheels, Transmission, etc. One-Hot Encoding Low cardinality (≀ 12 categories) β€” safe to OHE

2. πŸ”§ Domain-Knowledge Features

Feature Formula Intuition
car_age 2017 βˆ’ year Newer models command higher prices
hp_per_cyl engine_hp / engine_cylinders Power density β€” distinguishes sports from economy engines
hp_age_interaction engine_hp / (car_age + 1) High power and new β†’ exponential premium
is_luxury 1 if make_target_enc β‰₯ p75 Binary flag capturing brand-tier threshold effects
mpg_avg (city_mpg + highway_mpg) / 2 Composite efficiency signal

3. πŸ€– Unsupervised Clustering as a Supervised Feature

The most technically distinctive component of this project: K-Means clustering was applied to the scaled numeric feature space to discover latent market segments, and the resulting cluster_id and dist_to_centroid were injected as new features into the supervised models.

Why this works: A car's membership in a market segment (e.g., "high-power, low-efficiency, premium-brand SUV") carries pricing information that no single individual feature fully captures. Cluster membership encodes this holistic profile as a learnable signal.

The optimal number of clusters was selected automatically using the Silhouette Score, with results validated visually via PCA dimensionality reduction to 2 components:

K-Means Clusters Visualised via PCA

Left panel: cluster membership in PCA space, with centroids marked (βœ•). Right panel: the same projection coloured by log(MSRP) β€” the strong alignment between cluster boundaries and price gradient confirms that the discovered segments are genuine price tiers, not statistical artefacts.

4. πŸ“ Scaling Strategy

StandardScaler was applied to all continuous features for linear models. Tree-based models received raw (unscaled) features, as decision tree splits are scale-invariant. The target variable MSRP was never scaled, preserving full interpretability of predictions in USD.


πŸ“ˆ Regression Models β€” Predicting the Exact Price

Model Architecture

Three regression models were trained on the engineered feature matrix using an 80/20 train-test split with random_state=42 for full reproducibility.

πŸ† Model Comparison Table

Model MAE (USD) RMSE (USD) RΒ² Notes
Baseline Linear Regression (Part 3) 18855.77 38494.17 0.5865 Raw features, no engineering
Retrained Linear Regression (Part 5) 9744.57 19309.38 0.9230 Full engineered feature matrix
Random Forest Regressor (Part 5) 4385.84 29115.68 0.8250 300 trees, max_features="sqrt"
πŸ† Gradient Boosting Regressor (Part 5) 4170.55 14456.17 0.9569 500 trees, lr=0.05, depth=5

Why Gradient Boosting Wins

Linear Regression β€” even enriched with engineered features β€” is fundamentally constrained to additive, linear decision boundaries. Car pricing is non-linear: crossing from 6 to 8 cylinders does not produce a constant additive price increment β€” it triggers a market-tier reclassification that multiplies price. Gradient Boosting learns these thresholds automatically through sequential residual correction, with each of its 500 trees trained specifically to fix the errors left by its predecessors. The combination of a low learning rate (0.05), stochastic subsampling (80%), and shallow tree depth (5) achieves the optimal bias-variance balance for this dataset.

πŸ“‰ Feature Importance β€” Regression

Feature Importance β€” Gradient Boosting Regressor


🎯 Classification Deep-Dive β€” Market Tier Segmentation

The Regression-to-Classification Transition

Beyond point-estimate price prediction, a practical business application requires understanding which market tier a vehicle belongs to. We convert the continuous MSRP target into three discrete classes using Quantile Binning β€” dividing the price distribution into three equally-populated thirds:

Class Price Range (approx.) Business Meaning
🟦 Budget $0 β€” ~$21,000 Economy & entry-level vehicles
🟧 Mid-Range ~$21,000 β€” ~$40,000 Mainstream & near-premium vehicles
🟩 Luxury ~$40,000+ Premium, performance & exotic vehicles

Quantile Binning was chosen over fixed thresholds because it guarantees balanced class sizes (~33% each), preventing the dominant-class bias that undermines accuracy as a headline metric.

πŸ“ Evaluation Metric Rationale

Accuracy alone is misleading. A naΓ―ve model predicting the majority class achieves high accuracy without learning anything β€” and even with balanced classes, accuracy treats all misclassifications equally. Confusing Budget with Luxury carries severe financial consequences; confusing Budget with Mid-Range does not. We therefore report:

  • Primary metric: Weighted F1-Score (balances Precision and Recall across all three classes)
  • Business-critical metric: Precision on Luxury class (minimises False Positives = overbidding risk)

The dealership framing: A False Positive on Luxury (predicting a $15,000 car is worth $80,000) leads to direct, unrecoverable financial loss. A False Negative (missing a genuine Luxury vehicle) is an opportunity cost β€” painful but survivable. This asymmetry makes Precision on the Luxury class the single most business-critical number in the classification evaluation.

πŸ† Classification Model Comparison

Model Weighted F1 Luxury Precision Luxury Recall
Logistic Regression 0.8584 0.89 0.89
Random Forest Classifier 0.8866 0.91 0.90
πŸ† Gradient Boosting Classifier 0.8919 0.91 0.91

πŸ”² Confusion Matrix β€” Winning Classifier

Confusion Matrix β€” Gradient Boosting Classifier

The confusion matrix reveals that the vast majority of errors occur at the adjacent class boundaries (Budget↔Mid-Range and Mid-Range↔Luxury), which are the economically lower-risk mistakes. Cross-boundary errors (Budget predicted as Luxury or vice versa) are rare, confirming that the model has successfully internalised the market tier structure.

πŸ“Š Feature Importance β€” Classification

Feature Importance β€” Gradient Boosting Classifier


πŸ”¬ How to Use the Models

Load and Run Inference

import pickle
import pandas as pd

# ── Regression model (predicts exact MSRP in USD) ────────────
with open("car_price_model.pkl", "rb") as f:
    reg_model = pickle.load(f)

# ── Classification model (predicts Budget / Mid-Range / Luxury)
with open("classification_model.pkl", "rb") as f:
    cls_model = pickle.load(f)

# ── Example inference ─────────────────────────────────────────
# X_new must match the engineered feature matrix from Part 4
predicted_price = reg_model.predict(X_new)
predicted_tier  = cls_model.predict(X_new)

print(f"Predicted MSRP  : ${predicted_price[0]:,.0f}")
print(f"Predicted Tier  : {predicted_tier[0]}")

⚠️ Important: Both models expect the fully engineered feature matrix produced by the Part 4 pipeline (scaled numerics, target-encoded Make, frequency-encoded Model, OHE categoricals, cluster features). Raw CSV columns cannot be passed directly.


πŸ› οΈ Technical Stack

Component Library / Tool
Data manipulation pandas, numpy
Visualisation matplotlib, seaborn
ML models scikit-learn
Dimensionality reduction sklearn.decomposition.PCA
Clustering sklearn.cluster.KMeans
Model serialisation pickle
Dataset source Kaggle (CooperUnion/cardataset)
Model hosting Hugging Face Hub

πŸ“š Methodology Summary

Raw Data (11,914 Γ— 16)
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Data Cleaning    β”‚  OOP DataCleaner class Β· Group-median imputation
β”‚  (Part 2)         β”‚  Β· Market Category β†’ one-hot sub-labels
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  EDA & Viz        β”‚  4 research questions Β· Correlation analysis
β”‚  (Part 2)         β”‚  Β· Outlier detection & justified handling
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Feature Eng.     β”‚  Target/Frequency encoding Β· Domain features
β”‚  (Part 4)         β”‚  Β· K-Means clustering Β· PCA validation
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Regression       β”‚  Linear β†’ Random Forest β†’ Gradient Boosting
β”‚  (Parts 3 & 5)    β”‚  Reusable evaluate_model() & plot_predictions()
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Classification   β”‚  Quantile binning β†’ 3 market tiers
β”‚  (Parts 7 & 8)    β”‚  Logistic β†’ RF β†’ Gradient Boosting classifier
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
  Deployed Models (.pkl) on Hugging Face Hub

πŸ‘€ Author

Eliel Halfon
Machine Learning Project β€” Automotive Price Intelligence

Hugging Face Kaggle


Built with precision. Validated with rigour. Deployed with purpose.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support