NotFittedError TfidfVectorizer - Vocabulary wasn't fitted
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NotFittedError: Vocabulary wasn't fitted means you tried to use TfidfVectorizer before it had learned a vocabulary from training text. In scikit-learn, vectorizers behave like other estimators: they must be fitted before they can transform new data. The fix is usually simple, but the deeper issue is making sure the same fitted vectorizer instance survives through the whole pipeline.
What TfidfVectorizer Learns During fit
TfidfVectorizer does two important things during fitting:
- it builds a vocabulary from the training corpus
- it computes inverse-document-frequency statistics
Until that happens, the vectorizer does not know which terms exist or how to map them into feature columns.
That is why this fails:
The vectorizer has no fitted vocabulary yet, so transform() has nothing to work with.
The Correct Basic Pattern
Fit on training data first, then transform.
fit_transform() is just a convenience method that performs fitting and transformation in one step for the training set.
A Very Common Cause: Reinitializing the Vectorizer
A frequent bug is fitting one instance and then accidentally creating a new one before prediction.
The error message is correct here: the current object has never been fitted, even though an earlier object was.
Keep the Fitted Vectorizer Together With the Model
In text pipelines, the vectorizer and the classifier belong together. If you train the vectorizer on one machine or in one script and then predict elsewhere, save and reload the same fitted object.
If you save only the classifier and not the fitted vectorizer, prediction code will eventually fail or produce inconsistent features.
Pipelines Solve This Cleanly
Scikit-learn pipelines are often the best long-term solution because they keep fitting and transformation steps attached to the estimator.
With a pipeline, you do not manually remember whether the vectorizer is fitted. The pipeline manages the lifecycle.
Vocabulary Edge Cases
Sometimes the error appears because the training corpus is empty after preprocessing. For example, if your stop-word removal or token pattern removes every token, the vocabulary can end up empty.
That situation often raises an empty-vocabulary error rather than NotFittedError, but the lesson is similar: inspect the actual training text after preprocessing, not just before it.
Common Pitfalls
The biggest mistake is calling transform() on a fresh vectorizer before any fit() or fit_transform() call.
Another mistake is fitting one vectorizer instance and later replacing it with a new instance by accident.
People also save only the downstream model and forget the fitted text-preprocessing object. In text ML, the vectorizer is part of the model pipeline, not optional glue.
Finally, do not ignore the training corpus itself. If preprocessing strips everything away, the vectorizer cannot build a meaningful vocabulary.
Summary
- '
TfidfVectorizermust be fitted before it can transform text.' - Use
fit_transform()for training data andtransform()for new data. - Keep the same fitted vectorizer instance for prediction.
- Save and reload the vectorizer together with the model, or use a pipeline.
- If fitting still fails, inspect whether preprocessing is leaving you with an empty or invalid corpus.

