这个错误意味着什么“idf vector not fitted”

时间:2015-01-30 16:05:57

标签: python scikit-learn

这就是我调用TFIDFVectorizer的方式:

vectorizer = TfidfVectorizer(
                vocabulary=selected_vocabulary,
                stop_words='english',
                use_idf=True,
                norm=norm,
                tokenizer=self.tokenize,
                lowercase=True,
                smooth_idf=True) 

我拨打电话时出现此错误

vectorizer.transform(data_to_vectorize)

错误:

  File "/root/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1305, in transform
    return self._tfidf.transform(X, copy=False)

  File "/root/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1024, in transform
    raise ValueError("idf vector not fitted")

ValueError: idf vector not fitted

这个错误在这里意味着什么?

1 个答案:

答案 0 :(得分:6)

在变换任意文本之前,您需要首先拟合模型(例如,根据数据构建词汇表):

vectorizer.fit(data_to_vectorize)
X = vectorizer.transform(data_to_vectorize)

X = vectorizer.fit_transform(data_to_vectorize)