How to Prepare Mixed Data for Clustering Using Python

How to Prepare Mixed Data for Clustering Using Python, clustering is one of the most widely used unsupervised machine learning techniques for discovering hidden patterns in data. However, many real-world datasets contain both numerical and categorical variables, making them challenging to analyze using traditional clustering algorithms.

Algorithms such as K-Means, Hierarchical Clustering, and DBSCAN are designed to work with numerical features and rely on mathematical distance measures like Euclidean distance. They cannot directly process text or categorical variables.

Although algorithms such as K-Prototypes are specifically built for mixed-type data, many data scientists still prefer to use popular clustering methods. To do so, the dataset must first be converted into a completely numerical representation while ensuring that no feature dominates the distance calculation.

How to Prepare Mixed Data for Clustering Using Python

In this tutorial, you’ll learn how to preprocess mixed data using Python so it becomes suitable for virtually any clustering algorithm.

Why Mixed Data Needs Preprocessing

A dataset may contain variables such as:

  • Continuous variables (age, salary, weight)
  • Categorical variables (gender, city, product category)
  • Binary variables (Yes/No)

Distance-based clustering algorithms compare observations by calculating distances between them. Since distance formulas only operate on numbers, categorical values must first be transformed into numerical features.

In addition, numerical variables often have different scales. For example, income might range from thousands to millions, while age varies only between 18 and 80. Without scaling, variables with larger values dominate the clustering process.

Therefore, preprocessing typically involves two steps:

  • Standardize numerical variables.
  • Encode categorical variables into numerical form.

Euclidean Distance and Feature Scaling

K-Means commonly uses Euclidean distance to measure similarity between observations.

[
d(p,q)=\sqrt{\sum_{i=1}^{n}(q_i-p_i)^2}
]

Since every feature contributes to the distance calculation, all variables should be placed on a comparable scale. Otherwise, variables with larger numerical ranges can disproportionately influence cluster formation.

Example Dataset

In this example, we’ll use the Palmer Penguins dataset available through Seaborn. It contains both numerical measurements and categorical information, making it an excellent dataset for demonstrating mixed-data preprocessing.

import pandas as pd
import seaborn as sns
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder

# Load the dataset
penguins = sns.load_dataset("penguins").dropna()

print(penguins.head())

Separate Numerical and Categorical Features

The first step is to identify which columns belong to each data type.

categorical_cols = [
    "species",
    "island",
    "sex"
]

numerical_cols = [
    "bill_length_mm",
    "bill_depth_mm",
    "flipper_length_mm",
    "body_mass_g"
]

Separating columns makes preprocessing easier and prevents accidental transformations on inappropriate data types.

Build a Preprocessing Pipeline

Scikit-learn’s ColumnTransformer allows different preprocessing methods to be applied to different sets of columns in a single workflow.

In this example:

  • Numerical variables are standardized using StandardScaler()
  • Categorical variables are converted into binary variables using OneHotEncoder()
preprocessor = ColumnTransformer(
    transformers=[
        ("numeric", StandardScaler(), numerical_cols),
        ("categorical", OneHotEncoder(), categorical_cols)
    ]
)

This creates a reusable preprocessing pipeline that prepares all variables correctly.

Transform the Dataset

Now fit the transformer and convert the original dataset into a fully numerical dataset.

encoded_data = preprocessor.fit_transform(penguins)

cat_columns = preprocessor.named_transformers_[
    "categorical"
].get_feature_names_out(categorical_cols)

all_columns = numerical_cols + list(cat_columns)

encoded_df = pd.DataFrame(
    encoded_data,
    columns=all_columns
)

print(encoded_df.head())

The resulting DataFrame contains:

  • Standardized numerical variables
  • One-hot encoded categorical variables

Every feature is now numerical and suitable for clustering.

Apply K-Means Clustering

With preprocessing complete, the dataset can be passed directly into K-Means.

from sklearn.cluster import KMeans

kmeans = KMeans(
    n_clusters=3,
    random_state=42,
    n_init="auto"
)

encoded_df["cluster"] = kmeans.fit_predict(
    encoded_df.drop(
        columns="cluster",
        errors="ignore"
    )
)

Each observation is assigned to one of three clusters.

Visualize the Clusters

To understand how the observations are grouped, create a scatter plot using two standardized numerical features.

import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(10,7))

sns.scatterplot(
    data=encoded_df,
    x="bill_length_mm",
    y="flipper_length_mm",
    hue="cluster",
    palette="viridis"
)

plt.title("K-Means Clusters")
plt.xlabel("Scaled Bill Length")
plt.ylabel("Scaled Flipper Length")
plt.grid(True)

plt.show()

The visualization provides a simple way to inspect the cluster separation produced by the algorithm.

Why This Approach Works

This preprocessing strategy offers several advantages:

  • Makes mixed datasets compatible with any numerical clustering algorithm.
  • Prevents variables with large numerical ranges from dominating distance calculations.
  • Preserves categorical information through one-hot encoding.
  • Integrates seamlessly into Scikit-learn machine learning pipelines.
  • Creates reproducible preprocessing steps for future datasets.

Conclusion

Most traditional clustering algorithms require numerical input, but many real-world datasets contain a mixture of numerical and categorical variables. By combining StandardScaler with OneHotEncoder inside a ColumnTransformer, you can efficiently convert mixed data into a format suitable for K-Means, Hierarchical Clustering, DBSCAN, and many other machine learning algorithms.

Proper preprocessing is one of the most important steps in clustering analysis. A well-prepared dataset often leads to more meaningful clusters, improved model performance, and better insights from your data.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

nine + twenty =