Sub topic 4
Building a Vocabulary
Prerequisites and the vocabulary map
This chapter assumes basic Python, NumPy arrays, and the idea that a machine-learning model needs numeric features. A vocabulary is the set of distinct tokens found in a text collection, called a corpus. A vectorizer assigns each vocabulary token a column number.
For example, suppose the normalized corpus contains these tokens:
catdogsatthe
The vocabulary might map cat to column 0, dog to column 1, and so on. Every document is then represented using this same column order. A token that does not occur in a document receives a value of zero.
Token rules affect the vocabulary
A vectorizer must decide what counts as a token. Common choices include:
- splitting on word boundaries
- converting text to lowercase
- ignoring punctuation
- keeping or removing stop words such as
the - including one-word tokens, two-word tokens, or both
These decisions are part of the feature design. For example, Cat and cat become one vocabulary entry after lowercasing. Without lowercasing, they may become separate columns.
The vocabulary is learned when the vectorizer is fitted. Calling transform later uses the existing vocabulary. New tokens are ignored rather than added automatically.
Building a vocabulary with scikit-learn
CountVectorizer learns a vocabulary and creates document-term counts. Its vocabulary_ attribute stores the token-to-column mapping.
from sklearn.feature_extraction.text import CountVectorizer
docs = [
"The cat sat on the mat",
"The dog sat on the log",
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(docs)
print(vectorizer.vocabulary_)
print(vectorizer.get_feature_names_out())
print(X.toarray())The default analyzer lowercases words and removes punctuation. The feature names are returned in column order. The exact integer indices in vocabulary_ are implementation details, so use get_feature_names_out() when you need to label columns.
| Choice | Example setting | Effect |
|---|---|---|
| Lowercase | `lowercase=False` | Keeps `Cat` and `cat` separate |
| Stop words | `stop_words="english"` | Removes common English words |
| Token range | `ngram_range=(1, 2)` | Includes words and two-word phrases |
> callout note > Fit a vectorizer on the training corpus only. Fitting it on validation or test text lets information from those sets influence the feature space.
Vocabulary size and unknown words
Large corpora can produce very large vocabularies. Rare spelling errors, names, and one-time terms may add columns without helping a model. You can limit this with min_df, max_df, or max_features.
vectorizer = CountVectorizer(min_df=2, max_features=10000)Here, min_df=2 keeps tokens appearing in at least two documents. A token absent from the fitted vocabulary contributes no count when new text is transformed.
Exercise
Why should fit and transform be separate operations when processing new documents?
Show answer
fit learns the vocabulary from known training documents, while transform applies that fixed column mapping to new documents without changing the feature space.