Python 3.x 无法写入计数向量器词汇表

Python 3.x 无法写入计数向量器词汇表,python-3.x,scikit-learn,nlp,countvectorizer,Python 3.x,Scikit Learn,Nlp,Countvectorizer,我想保存并加载计数向量器词汇表。这是我的代码 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.vo

我想保存并加载计数向量器词汇表。这是我的代码

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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
1 dictionary\u filepath='CV\u dict'
---->2 pickle.dump(Cv_vec.词汇),打开(dictionary_filepath,'w'))
TypeError:write()参数必须是str,而不是bytes

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

在清除对象时以二进制模式打开文件。试着用一个


try可能重复:
pickle.dump(Cv\u vec.词汇),open(dictionary\u filepath,'wb'))
非常感谢。这是一个小错误。
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)