无法 pickle Scikit 学习 NearestNeighbor 分类器 - 无法 pickle instancemethod 对象
Cannot pickle Scikit learn NearestNeighbor classifier - can't pickle instancemethod objects
我正在尝试 pickle NearestNeighbor 模型,但它说不能 pickle instancemethod 对象。
代码:
import cPickle as pickle
from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors(n_neighbors=50, algorithm='ball_tree', metric=self.distanceCIE2000_classifier)
nbrs.fit(allValues)
with open('/home/ubuntu/nbrs.p','wb') as f:
pickle.dump(nbrs, f)
完整的回溯:
File "/home/ubuntu/colorSetter.py", line 82, in createClassifier
pickle.dump(nbrs, f)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects
NearestNeighbors
实例中的某处是一个属性,它引用您在 metric
参数中传递给它的实例方法。 Pickle 不会 pickle 实例方法,因此会出现错误。
一种解决方法是将方法 distanceCIE2000_classifier()
从 class 移出到常规的独立函数中,如果可能的话。
我正在尝试 pickle NearestNeighbor 模型,但它说不能 pickle instancemethod 对象。
代码:
import cPickle as pickle
from sklearn.neighbors import NearestNeighbors
nbrs = NearestNeighbors(n_neighbors=50, algorithm='ball_tree', metric=self.distanceCIE2000_classifier)
nbrs.fit(allValues)
with open('/home/ubuntu/nbrs.p','wb') as f:
pickle.dump(nbrs, f)
完整的回溯:
File "/home/ubuntu/colorSetter.py", line 82, in createClassifier
pickle.dump(nbrs, f)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects
NearestNeighbors
实例中的某处是一个属性,它引用您在 metric
参数中传递给它的实例方法。 Pickle 不会 pickle 实例方法,因此会出现错误。
一种解决方法是将方法 distanceCIE2000_classifier()
从 class 移出到常规的独立函数中,如果可能的话。