为什么以下tfidf矢量化失败?

时间:2016-12-18 23:24:39

标签: scikit-learn tf-idf

您好我正在进行以下实验,首先我创建了一个名为:tfidf:

的矢量化器
tfidf_vectorizer = TfidfVectorizer(min_df=10,ngram_range=(1,3),analyzer='word',max_features=500)

然后我对以下列表进行了矢量化:

tfidf = tfidf_vectorizer.fit_transform(listComments)

我的评论列表如下:

listComments = ["hello this is a test","the car is red",...]

我尝试按如下方式保存模型:

#Saving tfidf
with open('vectorizerTFIDF.pickle','wb') as idxf:
    pickle.dump(tfidf, idxf, pickle.HIGHEST_PROTOCOL)

我想使用我的矢量化器将相同的tfidf应用于以下列表:

lastComment = ["this is a car"]

开放模式:

with open('vectorizerTFIDF.pickle', 'rb') as infile:
    tdf = pickle.load(infile)

vector = tdf.transform(lastComment)

但是我得到了:

Traceback (most recent call last):
  File "C:/Users/LDA_test/ldaTest.py", line 141, in <module>
    vector = tdf.transform(lastComment)
  File "C:\Program Files\Anaconda3\lib\site-packages\scipy\sparse\base.py", line 559, in __getattr__
    raise AttributeError(attr + " not found")
AttributeError: transform not found

我希望有人能提前感谢此人支持我,

1 个答案:

答案 0 :(得分:1)

你已经腌制了矢量化数组,而不是变换器,你需要pickle.dump(tfidf_vectorizer, idxf, pickle.HIGHEST_PROTOCOL)