python-3.x - 使用 pickle 加载 TfidfVectorizer 时解决 AttributeError
问题描述
成功拟合TfidfVectorizer
对象并将其保存为对象而没有错误pickle
,我无法加载它以供以后使用。
我尝试使用joblib
,pickle
和不同的 python 版本。他们都没有工作。
保存代码:
import pickle
vectorizer = TfidfVectorizer(...)
doc_mat = vectorizer.fit_transform(corpus)
with open("vectorizer.pickle", "wb") as f:
pickle.dump(vectorizer, f)
with open("docmat.pickle", "wb") as f:
pickle.dump(doc_mat, f)
在其他地方加载代码:
import pickle
# This load generates the error below
with open("vectorizer.pickle", 'rb') as f:
vectorizer = pickle.load(f)
# This load works:
with open("docmat.pickle", 'rb') as f:
doc_mat = pickle.load(f)
错误信息:
/usr/lib/python3.5/pickle.py in load(self)
1041 raise EOFError
1042 assert isinstance(key, bytes_types)
-> 1043 dispatch[key[0]](self)
1044 except _Stop as stopinst:
1045 return stopinst.value
/usr/lib/python3.5/pickle.py in load_global(self)
1340 module = self.readline()[:-1].decode("utf-8")
1341 name = self.readline()[:-1].decode("utf-8")
-> 1342 klass = self.find_class(module, name)
1343 self.append(klass)
1344 dispatch[GLOBAL[0]] = load_global
/usr/lib/python3.5/pickle.py in find_class(self, module, name)
1394 return _getattribute(sys.modules[module], name)[0]
1395 else:
-> 1396 return getattr(sys.modules[module], name)
1397
1398 def load_reduce(self):
AttributeError: module '__main__' has no attribute 'decode'
解决方案
推荐阅读
- google-apps-script - 计算列中出现的次数
- spring-integration - Spring Boot 中的 SFTP 集成流程
- gremlin - 如何使用 gremlin 按度数对节点进行排序?
- c - 客户端无法连接到另一台机器上的服务器 - 已解决
- javascript - 获取动态添加的脚本标签的值
- dart - 为什么在这里@override 的“get”?(Dart 中的语法)
- javascript - 有没有办法在 on.click src 更改事件后将图像 src 切换回原始?
- javascript - 如何解决节点上的 UnhandledPromiseRejectionWarning?
- c++ - C++:使用 Stroustrup 示例在联合中放置新的问题
- javascript - 无法更新 highcharts 的数据