From raw CarDekho data to a production-ready ML model โ EDA, feature engineering, preprocessing, and regression.
Created by Jayan Gupta • Day 11 Workshop
The used car market is a โน4.5 trillion industry in India. Buyers and sellers constantly struggle to determine fair market prices.
Our Goal: Build a machine learning model that predicts a car's resale price given its specifications โ enabling fair, data-driven valuations.
2,059 real used car listings with 20 features covering make, specs, and location.
| Feature | Type | Example / Notes |
|---|---|---|
Make | String | Honda, Maruti Suzuki, Toyota |
Model | String | Amaze 1.2 VX i-VTEC |
Year | Integer | Manufacturing year (2011โ2020) |
Kilometer | Integer | Total km driven (e.g. 87,150) |
Fuel Type | Categorical | Petrol, Diesel, Electric, CNG |
Transmission | Categorical | Manual / Automatic |
Engine | Object โ Float | "1198 cc" โ 1198.0 (requires cleaning) |
Max Power | Object โ Float | "87 bhp @ 6000 rpm" โ 87.0 |
Max Torque | Object โ Float | "109 Nm @ 4500 rpm" โ 109.0 |
Seating Capacity | Float | 5.0, 7.0, 8.0 |
| Price | Integer (Target) | โน220,000 โ โน19,500,000 |
Running df.isnull().sum() reveals significant missing data in technical specs.
Key insight: Missing technical specs (Engine, Power) are NOT random โ they come from Electric Vehicles which physically don't have combustion engine specs!
Strategy: Apply domain knowledge โ fill EV rows with 0, drop remaining missing critical rows rather than imputing arbitrarily.
MISSING VALUE COUNTS
ML models need numbers. Raw string columns like Engine contain units that must be parsed out.
Before: Raw Strings
After: Clean Numbers
.str.extract()?Uses a regex pattern to pull the first numeric value from a string. The pattern ([\d\.]+) matches any digit or dot sequence.
Car prices are heavily right-skewed โ a few luxury cars stretch the distribution enormously.
Price frequency distribution
Price Range (Rupees)
โก Why Random Forest? The heavy skew means linear models will struggle. Tree-based models like Random Forest are naturally robust to non-linear, skewed distributions.
Exploring correlations between car features and price reveals powerful signals.
Hybrid & Electric command the highest premium
Automatic is a strong luxury indicator
Who you buy from matters a lot
Cars lose value fast as they age
Keep 8 columns: Year, Kilometer, Engine, Max Power, Seating Capacity, Fuel Type, Transmission, Seller Type. High-cardinality text like Model/Color is simply left out.
Numeric gaps (e.g. Seating Capacity) filled with the median; categorical gaps filled with the most frequent value โ both inside the pipeline.
Fuel Type, Transmission, Seller Type converted via OneHotEncoder(handle_unknown='ignore', drop='first').
80/20 split โ 1,588 training rows / 398 test rows โ via train_test_split() with random_state=42.
๐ก sklearn Pipeline: Wraps preprocessor + model into one object, fitting imputers and the encoder only on training data. No manual scaling is used โ Random Forest doesn't need it.
To showcase the performance difference, two pipelines are trained: a simple Linear Regression baseline and a Random Forest โ an ensemble of decision trees whose final prediction is the average of all tree outputs.
Evaluating regression models requires different metrics than classification โ we measure how close our predictions are in โน, on the held-out 398-row test set.
What does this jump mean? Switching from Linear Regression to Random Forest raises Rยฒ from 0.54 to 0.74 and cuts the average error by more than half โ evidence that car pricing is a non-linear problem, and this data still includes โน50L+ luxury outliers.
Measures how much of the variance in the target the model explains. 1.0 = perfect, 0 = no better than mean.
Average absolute difference between prediction and reality. In the same units as target (โน). Intuitive and robust to outliers.
Switching models keeps the exact same pipeline and only swaps the estimator โ isolating the effect of model choice on accuracy.
A correlation heatmap over the numeric columns shows what moves with price โ before any model is even trained.
Key Insight: Max Power has the strongest positive correlation with price, followed by torque and engine size โ faster, bigger-engined cars cost more. Kilometer is the only feature here with a negative correlation: more distance driven, lower resale value.
When the model transforms the test set, it can meet category values it never saw during training. handle_unknown='ignore' is what keeps the pipeline from crashing.
Actual warning raised while scoring the test set:
Why it happens & why it's safe
With an 80/20 split, a rare value of a categorical column (e.g. a seller-type variant) can land only in the test fold and never appear in training.
OneHotEncoder(handle_unknown='ignore') encodes the unseen category as all zeros instead of raising an error โ the pipeline keeps predicting.
This is exactly the behavior you want in a deployed API: a brand-new seller type from a live listing shouldn't take the whole service down.
Once trained, we save the entire Pipeline (preprocessor + model) using joblib.
Saving the full Pipeline bundles the imputers and one-hot encoder together with the trained model. No need to reapply feature engineering by hand โ just call .predict() on raw rows.
Beyond this notebook, a real platform could wrap the loaded pipeline in a web API endpoint that accepts a car listing and returns an estimated price.
Retrain monthly as new listings arrive. Car market prices shift โ a stale model will drift from reality without periodic retraining.
Raw strings (Engine: "1198 cc") must be parsed into numbers before ML can use them. Domain knowledge guides what to extract and how.
Not all NaN values should be dropped or filled the same way. EVs have 0 engine specs by design โ applying domain knowledge prevents data loss.
Fitting imputers and the one-hot encoder inside a Pipeline ensures they only learn from training data. This is critical for valid evaluation and deployment safety.
Correlation analysis shows Max Power (+0.78), Max Torque (+0.67), and Engine size (+0.61) are the strongest numeric predictors โ far ahead of Kilometer (โ0.15).
We built an end-to-end ML pipeline that predicts used car prices with Rยฒ = 0.74 and a mean error of โน3.78 Lakhs โ more than double the Linear Regression baseline's explanatory power.
Created by Jayan Gupta
Thank you! ๐ ยท Day 11 Workshop
Stack: Python • pandas • scikit-learn • matplotlib • seaborn • joblib