How to combine TFIDF features with other features
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Combining TF-IDF with non-text features is a common pattern in text classification, ranking, and fraud-detection models. The correct approach is to keep the text pipeline sparse, preprocess the other features appropriately, and combine everything in one training pipeline so the model sees a single feature matrix.
The Two Main Ways to Combine Features
In scikit-learn, the cleanest method is usually ColumnTransformer, especially when the input is a DataFrame containing text, numeric, and categorical columns.
The older manual approach is to compute TF-IDF separately and then concatenate matrices with scipy.sparse.hstack. That still works, but it is easier to make training and inference inconsistent if preprocessing is spread across multiple steps.
A Practical ColumnTransformer Example
The example below combines one text column with two numeric features. TF-IDF handles the text, while numeric features are scaled separately.
This is the right pattern for production because the same fitted pipeline transforms future inputs in exactly the same way.
Why Sparse Data Matters
TF-IDF matrices are usually sparse, meaning most entries are zero. That is good. It keeps memory use manageable for large vocabularies.
When you add dense numeric features, scikit-learn can still combine them with the sparse text matrix. The important detail is to avoid operations that accidentally densify the full text matrix, because that can explode memory usage.
For that reason, tree-based models or linear models that accept sparse input are often good fits.
Manual Concatenation With hstack
If your data is already split across custom preprocessing steps, you can combine features manually.
That code is valid, but you now have to remember to apply the exact same vectorizer and feature ordering at inference time.
Which Extra Features Work Well
Useful non-text features often include:
- message length
- link count or attachment count
- language or source metadata
- author reputation scores
- categorical flags encoded numerically
The right additions depend on the task. TF-IDF captures token importance. The extra features capture signals that plain word counts miss.
Scaling and Model Choice
Numeric features often benefit from scaling, especially for linear models, logistic regression, and neural networks. TF-IDF values are already normalized by their own pipeline, so scale the non-text features separately rather than trying to normalize the combined matrix by hand.
If you use categorical metadata, encode it with OneHotEncoder in the same ColumnTransformer.
Common Pitfalls
The biggest mistake is fitting TF-IDF on the full dataset before the train-test split. That leaks information from the test set into the vocabulary and IDF statistics.
Another mistake is converting a huge sparse matrix to dense form. That can turn a tractable problem into an out-of-memory error.
A third issue is losing feature alignment. If you manually concatenate matrices, the training and inference column order must match exactly.
Finally, do not assume extra features always help. Some metadata is noisy and can hurt generalization unless validated properly.
Summary
- Use
ColumnTransformerwhen combining TF-IDF with numeric or categorical features. - Keep text features sparse and preprocess other columns separately.
- '
hstackworks, but it is easier to make inference inconsistent.' - Scale numeric features when the downstream model benefits from it.
- Avoid data leakage by fitting TF-IDF only on training data.
- Validate whether the extra features add signal instead of just complexity.

