In scikit learn, how to deal with the data mixed with numerical and nominal value?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Scikit-learn estimators expect numeric input, so a dataset with both numerical and nominal values needs preprocessing before model training. The right solution is not to manually hack columns into arrays. It is to build a preprocessing pipeline that treats numeric and categorical features differently, then feeds the transformed result into the model.
Treat Numeric and Categorical Columns Separately
Mixed data needs different operations:
- numeric columns may need imputation and scaling
- nominal categorical columns usually need one-hot encoding
Scikit-learn's ColumnTransformer is built for this. It applies different transformers to different column groups in one reproducible pipeline.
This is the standard pattern because it keeps the preprocessing logic explicit and compatible with cross-validation.
Put the Model in the Same Pipeline
The best practice is to attach the estimator to the preprocessing pipeline so training and prediction always use the same transformations.
Then fit as usual:
This avoids a very common mistake: fitting preprocessing separately, then forgetting to apply the exact same transformation at prediction time.
One-Hot Encoding Is Usually Right for Nominal Data
Nominal features have no natural order. That means integer-coding them with values like 0, 1, and 2 is often misleading because many models interpret those numbers as ordered or distance-based.
For example:
- '
"red","green","blue"are categories, not ranks' - mapping them to
0,1,2creates fake numeric structure
That is why OneHotEncoder is the default answer for nominal data in scikit-learn.
If a feature is truly ordinal, such as "low", "medium", "high", then ordinal encoding may be appropriate. But that is a different case from nominal categories.
Handle Missing Values Before Encoding
Mixed datasets often contain missing values in both numeric and categorical columns. Imputation should happen before scaling or encoding.
The example pipeline above uses:
- median imputation for numeric data
- most-frequent imputation for categorical data
Those are simple defaults, not universal truths. The point is that missing-value strategy belongs in the pipeline too.
High Cardinality Needs More Thought
One-hot encoding is excellent for normal categorical columns, but it can become expensive when a feature has thousands of unique values. In those cases, options include:
- grouping rare categories
- using hashing-based approaches
- dropping unhelpful identifiers
Scikit-learn still supports the standard pipeline structure, but the categorical transformer may need a different strategy than plain one-hot encoding.
Common Pitfalls
- Converting categorical labels directly to integers and pretending they are numeric measurements.
- Preprocessing the training data manually and forgetting to apply the same logic at prediction time.
- Handling missing values outside the pipeline so cross-validation sees a different transformation path.
- One-hot encoding extremely high-cardinality columns without checking memory cost.
- Treating ordinal and nominal features as if they required the same encoding.
Summary
- Mixed numeric and nominal data should be preprocessed with different transformers.
- '
ColumnTransformerandPipelineare the standard scikit-learn tools for this.' - Numeric columns often need imputation and scaling.
- Nominal columns usually need one-hot encoding, not arbitrary integer labels.
- Put preprocessing and the model in one pipeline so training and prediction stay consistent.

