Principle:Scikit learn Scikit learn Text Feature Extraction
| Knowledge Sources | |
|---|---|
| Domains | Natural Language Processing, Feature Engineering |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Text feature extraction converts unstructured text documents into structured numerical feature vectors that machine learning algorithms can process.
Description
Raw text cannot be directly consumed by most machine learning algorithms, which require fixed-length numerical input. Text feature extraction methods transform collections of text documents into numerical matrices by representing each document as a vector in a vocabulary-defined feature space. These techniques solve the problem of bridging the gap between natural language and mathematical models. The most common approaches are bag-of-words representations (count-based and TF-IDF weighted), feature hashing for memory-efficient encoding, and dictionary vectorization for pre-structured feature dictionaries. Text feature extraction is the foundational preprocessing step in text classification, clustering, and information retrieval.
Usage
Use CountVectorizer to obtain raw term frequency vectors, suitable as a baseline and when downstream algorithms handle normalization. Use TfidfVectorizer to produce TF-IDF weighted vectors that down-weight common terms and up-weight discriminative ones, typically yielding better performance for classification and retrieval tasks. Use FeatureHasher when the vocabulary is very large or unknown in advance and memory efficiency is critical, accepting the trade-off of losing interpretability. Use DictVectorizer when features arrive as dictionaries of feature-value pairs rather than raw text. Use stop word removal to filter out common, non-informative words that add noise.
Theoretical Basis
Bag of Words represents a document as a vector of term frequencies, ignoring word order:
The vocabulary defines the feature space of dimension .
TF-IDF (Term Frequency-Inverse Document Frequency) weights each term by its discriminative power:
where:
- Term Frequency: (raw count or sub-linear: )
- Inverse Document Frequency:
where is the total number of documents and is the number of documents containing term . The "+1" terms provide smoothing.
After TF-IDF weighting, documents are typically L2-normalized:
Feature Hashing (the hashing trick) maps features to a fixed-size vector using a hash function:
where is a hash function mapping terms to indices in and is a sign function that reduces collision effects. This avoids storing a vocabulary dictionary, enabling constant-memory operation.
N-grams extend the bag-of-words model by considering sequences of consecutive tokens:
- Unigrams: individual words
- Bigrams: pairs of consecutive words
- Character n-grams: sequences of characters, useful for capturing morphological patterns and handling misspellings
Stop Words are high-frequency words (e.g., "the", "is", "at") that carry little discriminative information. Removing them reduces dimensionality and noise.