【问题标题】:Not able to write the Count Vectorizer vocabulary无法编写 Count Vectorizer 词汇表
【发布时间】:2019-01-29 10:24:11
【问题描述】:

我想保存和加载计数矢量化词汇表。这是我的代码

from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 1500)
Cv_vec = cv.fit(X['review'])
X_cv=Cv_vec.transform(X['review']).toarray()
dictionary_filepath='CV_dict'
pickle.dump(Cv_vec.vocabulary_, open(dictionary_filepath, 'w'))

它告诉我

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-407-3a9b06f969a9> in <module>()
      1 dictionary_filepath='CV_dict'
----> 2 pickle.dump(Cv_vec.vocabulary_, open(dictionary_filepath, 'w'))

TypeError: write() argument must be str, not bytes

我想保存计数向量器的词汇并加载它。有人可以帮我吗?

【问题讨论】:

标签: python-3.x scikit-learn nlp countvectorizer


【解决方案1】:

挑选对象时以二进制模式打开文件。并尝试使用context manager,即

from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 1500)
Cv_vec = cv.fit(X['review'])
X_cv=Cv_vec.transform(X['review']).toarray()
dictionary_filepath='CV_dict'

with open('CV_dict.pkl', 'wb') as fout:
    pickle.dump(Cv_vec.vocabulary_, fout)

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2020-04-07
    • 2015-07-13
    • 2014-04-03
    • 2016-05-04
    • 2023-02-07
    • 2016-09-25
    • 1970-01-01
    • 2022-10-19
    相关资源
    最近更新 更多