Featureunion field dispatch

From wenmin-wu/ds-skills · View interactive version

Featureunion field dispatch is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use sklearn FeatureUnion with closure-based preprocessors to apply different vectorizers to different DataFrame columns in a single fit_transform call

How to use this skill

Skill file

FeatureUnion Field Dispatch

Overview

When a dataset has multiple text and categorical columns that each need different vectorization (TF-IDF for descriptions, CountVectorizer for names, token-pattern matching for categoricals), use a FeatureUnion with custom preprocessor closures. Each vectorizer receives a closure that extracts its target column from the row. This keeps the entire feature pipeline in one fit_transform call and produces a single sparse matrix.

Quick Start

from sklearn.pipeline import FeatureUnion
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

default_preprocessor = CountVectorizer().build_preprocessor()

def build_preprocessor(field):
    idx = list(df.columns).index(field)
    return lambda x: default_preprocessor(x[idx])

vectorizer = FeatureUnion([
    ('name', CountVectorizer(
        ngram_range=(1, 2), max_features=50000,
        preprocessor=build_preprocessor('name'))),
    ('category', CountVectorizer(
        token_pattern='.+',
        preprocessor=build_preprocessor('category'))),
    ('brand', CountVectorizer(
        token_pattern='.+',
        preprocessor=build_preprocessor('brand'))),
    ('description', TfidfVectorizer(
        ngram_range=(1, 3), max_features=100000,
        preprocessor=build_preprocessor('description'))),
])

X = vectorizer.fit_transform(df.values)

Workflow

  1. Build a default preprocessor from CountVectorizer().build_preprocessor()
  2. Create a closure factory that captures the column index and applies the preprocessor
  3. Assign each vectorizer in the FeatureUnion its own closure
  4. Use token_pattern='.+' for single-value categoricals (treat entire cell as one token)
  5. Call fit_transform(df.values) — each row is a numpy array, closures extract the right column

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.