scikit-learn
data preprocessing
mixed data types
numerical and nominal data
machine learning

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.

python
1import pandas as pd
2from sklearn.compose import ColumnTransformer
3from sklearn.impute import SimpleImputer
4from sklearn.pipeline import Pipeline
5from sklearn.preprocessing import OneHotEncoder, StandardScaler
6
7X = pd.DataFrame({
8    "age": [25, 32, None, 41],
9    "income": [50000, 64000, 58000, None],
10    "city": ["Toronto", "Paris", "Toronto", "Berlin"],
11    "segment": ["A", "B", "A", "C"],
12})
13
14numeric_features = ["age", "income"]
15categorical_features = ["city", "segment"]
16
17numeric_pipeline = Pipeline([
18    ("imputer", SimpleImputer(strategy="median")),
19    ("scaler", StandardScaler()),
20])
21
22categorical_pipeline = Pipeline([
23    ("imputer", SimpleImputer(strategy="most_frequent")),
24    ("encoder", OneHotEncoder(handle_unknown="ignore")),
25])
26
27preprocessor = ColumnTransformer([
28    ("num", numeric_pipeline, numeric_features),
29    ("cat", categorical_pipeline, categorical_features),
30])

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.

python
1from sklearn.linear_model import LogisticRegression
2
3model = Pipeline([
4    ("preprocess", preprocessor),
5    ("classifier", LogisticRegression(max_iter=1000)),
6])

Then fit as usual:

python
1y = [0, 1, 0, 1]
2model.fit(X, y)
3
4pred = model.predict(pd.DataFrame({
5    "age": [29],
6    "income": [61000],
7    "city": ["Paris"],
8    "segment": ["B"],
9}))
10
11print(pred)

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, 2 creates 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.
  • 'ColumnTransformer and Pipeline are 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.

Course illustration
Course illustration

All Rights Reserved.