<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/medium-pxxluf6o1" -->

---
title: Medium | daily.dev
description: Choosing the wrong encoding for categorical variables can silently destroy model accuracy — illustrated by an 18-point gap between two data scientists using...
canonical: https://daily.dev/posts/medium-pxxluf6o1
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Medium | daily.dev
og:description: Choosing the wrong encoding for categorical variables can silently destroy model accuracy — illustrated by an 18-point gap between two data scientists using...
og:url: https://daily.dev/posts/medium-pxxluf6o1
og:image: https://api.daily.dev/og/posts/PXxLuf6o1.png
og:image:alt: Medium
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Medium

**[Medium](https://daily.dev/sources/medium_js)** · 9 min read · 0 upvotes · 0 comments

## Summary

Choosing the wrong encoding for categorical variables can silently destroy model accuracy — illustrated by an 18-point gap between two data scientists using the same data. LabelEncoder assigns arbitrary numeric order to nominal categories, misleading models into treating meaningless differences as real. The post covers five encoding strategies: one-hot (nominal default), ordinal (genuine rank only), target (high-cardinality with train-only fit), frequency, and binary/hash encoding. A sklearn pipeline example demonstrates combining ordinal and one-hot encoders with explicit category ordering and unknown-category handling. Key pitfalls include encoding identifiers as features, one-hot encoding high-cardinality columns, and fitting target encoding before the train-test split.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@banerjeevictor06/encoding-categorical-variables-why-the-wrong-encoding-teaches-your-model-the-ec551a97b5d8>

## Questions this post answers

### Why does LabelEncoder hurt accuracy on nominal categorical columns like subscription plan?

LabelEncoder assigns categories numbers based on alphabetical order, which implies a false rank and distance between values that the model treats as meaningful. In one churn model example, using LabelEncoder on a four-value subscription_plan column versus one-hot encoding the same data produced a 71% versus 89% accuracy gap, purely from the implied ordering.

_Anyone debugging a stubbornly inaccurate model can compare encoding writeups like this on daily.dev._

### When should I use target encoding instead of one-hot encoding for a high-cardinality column?

Target encoding replaces each category with a statistic like the mean of the target variable, and works well for high-cardinality columns such as neighborhood where one-hot encoding would create too many sparse columns. It must be fit only on training data, never the full dataset, because it directly learns from the outcome and can leak information otherwise.

_Data scientists weighing encoding trade-offs can track practical feature-engineering guidance on daily.dev._

### How do I handle categories in production that the model never saw during training with scikit-learn?

Set handle_unknown='ignore' on scikit-learn's OneHotEncoder so an unseen category at inference time doesn't crash the prediction pipeline instead of raising an error. This setting should be included in any ColumnTransformer pipeline handling nominal features to make production traffic safe against unfamiliar category values.

_Engineers hardening ML pipelines against unseen inputs can follow practical scikit-learn patterns on daily.dev._

## Similar posts on daily.dev

- [Encoding Ordinal Categorical Data: Teaching Machine Learning the Meaning Behind Rankings](https://daily.dev/posts/encoding-ordinal-categorical-data-teaching-machine-learning-the-meaning-behind-rankings-rvxvv5gdg) · Medium · 0 upvotes · 0 comments
- [3 Smart Ways to Encode Categorical Features for Machine Learning](https://daily.dev/posts/3-smart-ways-to-encode-categorical-features-for-machine-learning-ekdu8qdci) · Machine Learning Mastery · 1 upvotes · 0 comments
- [7 Categorical Data Encoding Techniques](https://daily.dev/posts/7-categorical-data-encoding-techniques-6gk7qk4t5) · Daily Dose of Data Science \| Avi Chawla \| Substack · 0 upvotes · 0 comments
- [Encoding Categorical Data for Outlier Detection](https://daily.dev/posts/encoding-categorical-data-for-outlier-detection-rljflcern) · Towards Data Science · 0 upvotes · 0 comments
- [Understanding One-Hot Encoding in Machine Learning](https://daily.dev/posts/understanding-one-hot-encoding-in-machine-learning-eqcybx6gm) · DigitalOcean Community · 1 upvotes · 0 comments

---

Tags: [#machine-learning](https://daily.dev/tags/machine-learning), [#python](https://daily.dev/tags/python), [#scikit](https://daily.dev/tags/scikit), [#feature-engineering](https://daily.dev/tags/feature-engineering)

[View this post on daily.dev](https://daily.dev/posts/medium-pxxluf6o1)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Medium","url":"https://daily.dev/posts/medium-pxxluf6o1","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/medium-pxxluf6o1"},"datePublished":"2026-07-19T20:55:53.112Z","dateModified":"2026-09-14T07:49:36.087Z","description":"Choosing the wrong encoding for categorical variables can silently destroy model accuracy — illustrated by an 18-point gap between two data scientists using...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1aaf707775f152c11e130de13476a36a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/1aaf707775f152c11e130de13476a36a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/medium-pxxluf6o1","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"machine-learning,python,scikit,feature-engineering","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Medium"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/medium-pxxluf6o1#faq","mainEntity":[{"@type":"Question","name":"Why does LabelEncoder hurt accuracy on nominal categorical columns like subscription plan?","acceptedAnswer":{"@type":"Answer","text":"LabelEncoder assigns categories numbers based on alphabetical order, which implies a false rank and distance between values that the model treats as meaningful. In one churn model example, using LabelEncoder on a four-value subscription_plan column versus one-hot encoding the same data produced a 71% versus 89% accuracy gap, purely from the implied ordering. Anyone debugging a stubbornly inaccurate model can compare encoding writeups like this on daily.dev."}},{"@type":"Question","name":"When should I use target encoding instead of one-hot encoding for a high-cardinality column?","acceptedAnswer":{"@type":"Answer","text":"Target encoding replaces each category with a statistic like the mean of the target variable, and works well for high-cardinality columns such as neighborhood where one-hot encoding would create too many sparse columns. It must be fit only on training data, never the full dataset, because it directly learns from the outcome and can leak information otherwise. Data scientists weighing encoding trade-offs can track practical feature-engineering guidance on daily.dev."}},{"@type":"Question","name":"How do I handle categories in production that the model never saw during training with scikit-learn?","acceptedAnswer":{"@type":"Answer","text":"Set handle_unknown='ignore' on scikit-learn's OneHotEncoder so an unseen category at inference time doesn't crash the prediction pipeline instead of raising an error. This setting should be included in any ColumnTransformer pipeline handling nominal features to make production traffic safe against unfamiliar category values. Engineers hardening ML pipelines against unseen inputs can follow practical scikit-learn patterns on daily.dev."}}]}
```

