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
- Project Overview & Data Walkthrough
-
- π Abstract
- π Repository Contents
- π Interactive EDA β The Data Story
- βοΈ The "Secret Sauce" β Feature Engineering
- π Regression Models β Predicting the Exact Price
- π― Classification Deep-Dive β Market Tier Segmentation
- π¬ How to Use the Models
- π οΈ Technical Stack
- π Methodology Summary
- π€ Author
- Project Overview & Data Walkthrough
π Car Price Intelligence: From Raw Specs to Market Predictions
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
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:
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
π― 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
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
π¬ 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
Built with precision. Validated with rigour. Deployed with purpose.




