One-Hot Encoding: Why It Breaks Between Train and Test
Your model works perfectly in testing. You deploy it. It crashes on the first real prediction. ValueError: The feature names should match those that were passed during fit. Feature names seen at fit time, yet now missing: - Delhi If you've seen an error like this — a feature that's missing, or a mismatch in feature names — it almost always traces back to one decision: how a categorical column got converted into numbers. What Actually Happened Somewhere in the pipeline, a column like city — Bangalore, Mumbai, Delhi — got converted into numbers. That conversion is called one-hot encoding . Instead of one column holding text labels, you get one new column per category , each holding a 1 or a 0. Bangalore becomes city_Bangalore = 1 , everything else 0. Mumbai becomes city_Mumbai = 1 , everything else 0. Why Not Just Use 1, 2, 3? Models only understand numbers. They can't do math on the word "Bangalore....