【发布时间】:2017-05-12 01:52:09
【问题描述】:
我正在尝试腌制贝叶斯类对象。我的代码是:
file = open('Bayes.pkl', 'wb')
pickle.dump(bayes, file)
file.close()
我收到以下错误
TypeError: can't pickle instancemethod object
为了克服这个问题,我参考了这个 StackOverflow 解决方案: How to remove instancemethod objects, for the sake of pickle, without modifying the original class
这表明使用方法 getstate() 和 setstate()。这有效,我能够腌制我的文件。 但是,当我尝试测试我的数据时,我收到如下错误:
File "/home/anaconda2/lib/python2.7/site-packages/reverend/thomas.py", line 254, in guess
res[pname]=self.combiner(p, pname)
TypeError: 'NoneType' object is not callable
我的代码如下:
my_str = ' '
for val in sourceCode:
my_str += str(val)
results = classifier.guess(my_str)
print results
my_str 的类型是字符串。但是,它仍然没有猜测。
【问题讨论】:
-
用
getstate()和setstate()的代码更新了您的问题。
标签: python machine-learning pickle bayesian