numpy convert categorical string arrays to an integer array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Many machine learning and numeric workflows need category labels as integers instead of raw strings. NumPy can do this conversion cleanly, but you should understand how the mapping is created so you do not accidentally scramble category meaning between training and inference.
Use np.unique with return_inverse
The most direct NumPy solution is np.unique(..., return_inverse=True). It returns the unique sorted categories and, for each original value, the integer index of that category.
Output looks like this:
This means:
- '
birdmaps to0' - '
catmaps to1' - '
dogmaps to2'
The mapping is based on sorted unique values, not first appearance.
Build an Explicit Mapping When Order Matters
If you want categories to be assigned in the order they first appear, build the mapping yourself. That is often easier to reason about when you need consistent IDs across multiple steps.
Now the first seen category, dog, becomes 0, then cat becomes 1, and bird becomes 2.
Reuse the Same Mapping for New Data
The most important practical rule is to reuse the exact same mapping when you transform new data. If you rebuild the mapping independently on a test set, the integers can change and your model will misinterpret the values.
If unknown categories are possible, handle them explicitly:
Using -1 as a sentinel value is common when the model or downstream pipeline knows how to treat unseen labels.
Know When Integer Encoding Is Not Enough
Integer encoding is useful, but it does not mean the categories are ordered. Some models incorrectly treat 2 as larger or more meaningful than 1. Tree-based models can often tolerate integer-encoded categories better than linear models or neural networks.
If the categories are purely nominal, one-hot encoding may be the safer representation:
Choose the representation based on the model, not just on what is easiest to generate.
Common Pitfalls
The biggest mistake is rebuilding the category mapping separately for train and test data. The integer values may look valid in both places while silently meaning different things.
Another issue is assuming np.unique preserves original order. It does not. It sorts the categories, which can be surprising if you expected the first item seen to become 0.
Unknown labels also require a plan. A production system often encounters categories that were not present in training. Without a fallback code or validation rule, the encoding step may fail at runtime.
Finally, remember that integer encoding can imply a false ordering to some models. If there is no natural rank among the categories, consider one-hot encoding or another model-friendly representation.
Summary
- '
np.unique(..., return_inverse=True)is the quickest NumPy way to map strings to integer IDs.' - '
np.uniquesorts categories, so the mapping may not follow original appearance order.' - Build and store an explicit mapping when you need reproducible encoding across datasets.
- Decide how to handle unknown categories before inference time.
- Use one-hot encoding when integer IDs would create a misleading numeric ordering.

