Combining Multiple Feature Types
Stack text and numeric features.
Combining Multiple Feature Types is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Type Is Rarely Enough
TF-IDF captures words, your hand-built numbers capture style. The best models often combine several feature types into one input.
Text and Numbers Together
Imagine a review's TF-IDF vector plus its length and star rating. Stacking these gives the model both word and numeric signal at once.
The Shape Problem
TF-IDF outputs a sparse matrix, while your custom features are a small dense array. You must join them along the same rows.
Side by Side, Not Stacked
We glue features as new columns for the same documents, not new rows. This horizontal join is called concatenation.
Stacking Sparse Matrices
SciPy offers hstack to place matrices side by side efficiently. It keeps everything sparse, so memory stays under control.
from scipy.sparse import hstack
combined = hstack([tfidf_matrix, numeric_features])ColumnTransformer to the Rescue
scikit-learn's ColumnTransformer applies different steps to different columns. It is the clean way to route text and numbers through one pipeline.
Wiring It Up
You give ColumnTransformer a list of named transformers and the columns each one handles. It builds a single feature matrix for you.
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([("text", TfidfVectorizer(), "review"), ("num", "passthrough", ["length"])])FeatureUnion for Parallel Steps
When several transformers read the same input, FeatureUnion runs them in parallel and joins the outputs. It is built for combining feature extractors.
Mind the Scales
Raw word counts and a 0-to-1000 length live on very different ranges. Mixing them often calls for scaling so no feature dominates.
Let Data Decide
Adding feature types should be a measured experiment. Compare a validation score before and after to confirm the combo actually helps.
More Is Not Always Better
Throwing in every feature can add noise and slow training. Aim for a small, well-chosen mix over a giant kitchen sink. 🧹
Quick Check
How should TF-IDF and numeric features be merged?
Recap
Strong models combine text and numeric features by concatenating columns. Use hstack, ColumnTransformer, or FeatureUnion, and scale before mixing. ✅
Frequently asked questions
Is the “Combining Multiple Feature Types” lesson free?
Yes — the full text of “Combining Multiple Feature Types” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Combining Multiple Feature Types”?
Stack text and numeric features. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Combining Multiple Feature Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Beyond Bag-of-Words
- Character N-Grams for Robustness
- Combining Multiple Feature Types
- Scaling and Selecting Features