one-hot encoding
One-hot encoding is a way of representing a categorical value as a vector of zeros with a single one marking which category is present. A color feature holding red, green, or blue becomes three columns, so a green row encodes as [0, 1, 0].
The scheme exists because most machine learning models expect numbers rather than labels, and numbering the colors 0, 1, and 2 would imply an ordering and a spacing that the categories don’t have. One-hot vectors keep every category equidistant. In Python, scikit-learn’s OneHotEncoder and get_dummies() in pandas both produce this representation.
The two encodings below place the same three colors at different distances from each other:
The cost is width. A vocabulary of 50,000 tokens needs 50,000 dimensions, and no two of those vectors say anything about how similar their categories are. Embeddings replace them with dense learned representations for that reason, though one-hot targets are still the reference distribution for cross-entropy loss during training.
Related Resources
Tutorial
Practical Text Classification With Python and Keras
Learn about Python text classification with Keras. Work your way from a bag-of-words model with logistic regression to more advanced methods leading to convolutional neural networks. See why word embeddings are useful and how you can use pretrained word embeddings. Use hyperparameter optimization to squeeze more performance out of your model.
For additional information on related topics, take a look at the following resources:
- Split Your Dataset With scikit-learn's train_test_split() (Tutorial)
- Linear Regression in Python (Tutorial)
- Logistic Regression in Python (Tutorial)
- Pythonic Data Cleaning With pandas and NumPy (Tutorial)
- ChromaDB: Embeddings and Vector Databases in Python (Tutorial)
- Python AI: How to Build a Neural Network & Make Predictions (Tutorial)
- Learn Text Classification With Python and Keras (Course)
- Splitting Datasets With scikit-learn and train_test_split() (Course)
- Split Your Dataset With scikit-learn's train_test_split() (Quiz)
- Starting With Linear Regression in Python (Course)
- Linear Regression in Python (Quiz)
- Data Cleaning With pandas and NumPy (Course)
- Vector Databases and Embeddings With ChromaDB (Course)
- Embeddings and Vector Databases With ChromaDB (Quiz)
- Building a Neural Network & Making Predictions With Python AI (Course)
- Python AI: How to Build a Neural Network & Make Predictions (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 29, 2026