For feature extraction from text, how to check if a vectorizer (e.g. TfIdfVectorizer or CountVectorizer) has been already fit on a training data?
In particular, I want the code to automatically figure out if a vectorizer has been already fit.
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer()
def vectorize_data(texts):
# if vectorizer has not been already fit
vectorizer.fit_transform(texts)
# else
vectorizer.transform(texts)
You can use the check_is_fitted
which is basically made for doing this.
In the source of TfidfVectorizer.transform()
you can check its usage:
def transform(self, raw_documents, copy=True):
# This is what you need.
check_is_fitted(self, '_tfidf', 'The tfidf vector is not fitted')
X = super(TfidfVectorizer, self).transform(raw_documents)
return self._tfidf.transform(X, copy=False)
So in your case, you can do this:
from sklearn.utils.validation import check_is_fitted
def vectorize_data(texts):
try:
check_is_fitted(vectorizer, '_tfidf', 'The tfidf vector is not fitted')
except NotFittedError:
vectorizer.fit(texts)
# In all cases vectorizer if fit here, so just call transform()
vectorizer.transform(texts)
I propose 2 ways to check this:
import inspect
def my_inspector(model):
return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )
from sklearn.feature_extraction.text import TfidfVectorizer
import inspect
vectorizer = TfidfVectorizer()
def my_inspector(model):
return 0 < len( [k for k,v in inspect.getmembers(model) if k.endswith('_') and not k.startswith('__')] )
my_inspector(vectorizer)
# False
check_is_fitted
from sklearn.utils.validation import check_is_fitted
check_is_fitted(vectorizer, '_tfidf', 'The tfidf vector is not fitted')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With