Combining LLM Embeddings with Tabular Features in a Unified Scikit-learn Pipeline

In this article, you will learn how to build a unified scikit-learn pipeline that combines text embeddings generated by a lightweight open-source language model with structured tabular features for classification tasks.

Topics we will cover include:

  • How to generate text embeddings using Hugging Face’s sentence-transformers library and wrap them in a custom scikit-learn transformer class.
  • How to use a ColumnTransformer to run parallel preprocessing branches for text, numeric, and categorical features simultaneously.
  • How to assemble and evaluate a complete, deployment-ready classification pipeline on a mixed dataset combining real text data with synthetic tabular features.

Combining LLM Embeddings with Tabular Features in a Unified Scikit-learn Pipeline

Introduction

Real-world tasks like ticket triage or customer churn prediction are typically addressed by building classification models. Yet, in an increasingly data-pervaded era, the data used to construct these models and perform inference on them rarely comes in a single flavor. We are often faced with a mix of tabular, structured data of numeric and qualitative nature, as well as unstructured data like text — for instance, ticket descriptions or customer messages. Feeding these data types together into machine learning models requires effective and unified pipelines that accommodate the latest data nuances and techniques to handle them.

This article shows you how to build a clean, deployment-ready solution that encapsulates embeddings generated by open-source LLMs (language models) into a unified scikit-learn pipeline, bringing together text representations and tabular features of distinct types — all based on the use of a ColumnTransformer. To illustrate its use, we will consider a classification scenario for detecting spammer users in a customer base.

Prerequisites

Instead of resorting to a paid API like OpenAI’s or Google Gemini’s, or a massive open-source LLM like LLaMA 3, we will use a more lightweight, CPU-friendly solution to generate embeddings from a collection of texts: Hugging Face’s sentence-transformers. Depending on your running environment, all you may need is to install the following libraries and dependencies:

Remove the ! if you are working in your own Python IDE rather than a cloud notebook environment like Google Colab.

Step-by-Step Guide

Here’s what our intended, unified scikit-learn pipeline architecture looks like:

Scikit-learn Pipeline Architecture

But first, we need a mixed dataset that looks reasonably realistic. For this, we adopt a hybrid approach: we pull a real dataset available on GitHub — the well-known SMS Spam Collection dataset containing users’ text messages labeled as spam or not — and augment it with synthetic tabular data features. Put together, the data will serve us to set up a customer churn/triage scenario.

The code excerpt required for data generation is a bit large, but there are plenty of comments to help you understand every decision behind the synthetic data creation process:

Example output:

Sample of the semi-synthetic dataset combining text and tabular features

The next step is key, as this is where we create the custom text transformer — see the leftmost branch in the previous diagram. In scikit-learn, this is done by creating a custom class that inherits from TransformerMixin and BaseEstimator. The requirement is to define fit() and transform() methods, just like any pre-existing data transformation class in the library (e.g. standard scalers and one-hot encoders).

Notice that we specify the Hugging Face sentence-transformer model to use — namely all-MiniLM-L6-v2 — in the constructor method, and call the model in transform() to map texts into embeddings.

Next, once we have our embeddings, we apply the parallel data preprocessing required by the other features. Since this relies entirely on already-implemented classes in scikit-learn, we can directly assemble all the type-specific preprocessing steps into an overarching, unified pipeline. We distinguish numerical columns from categorical ones, applying standard scaling to the former and one-hot encoding to the latter. Together with the previously implemented text embedding step, this gives us three processing branches that run in parallel. The way to implement this is through a ColumnTransformer object that contains a list of three “processing branches.” This mechanism keeps the whole dataset together, without the need to manually split and re-unify features.

After that, we add the final stage: a random forest classifier. The entire process looks as follows:

Now that we have assembled the entire pipeline, it’s time to try it out! The final piece of code trains the model — a process that, thanks to the pipeline encapsulation, implicitly carries out all the preceding data preparations — and evaluates it on the test set we set aside earlier:

Results:

These results are pretty decent. Part of the reason is that the real dataset used for the labeled texts is known for being easily class-separable and therefore not hard to classify with high accuracy. We also intentionally added noise and overlap when creating the other synthetic attributes to introduce a bit of challenge for our classifier — otherwise, it might have achieved 100% accuracy, which would not be very informative.

Conclusion

This article tackled an increasingly common problem in the AI and data science landscape: leveraging text data and combining it with structured data features traditionally fed to downstream machine learning models for predictive tasks like classification. We used scikit-learn’s transformer classes and a pre-trained language model to build a unified pipeline that cleanly and elegantly processes these mixed data types, yielding a robust and easily reusable solution.

No comments yet.

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.