【问题标题】:Cannot pickle Scikit learn NearestNeighbor classifier - can't pickle instancemethod objects无法腌制 Scikit 学习最近邻分类器 - 无法腌制实例方法对象
【发布时间】:2016-10-12 03:19:06
【问题描述】:

我正在尝试腌制 NearestNeighbor 模型,但它说无法腌制 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

【问题讨论】:

    标签: python pickle


    【解决方案1】:

    NearestNeighbors 实例中的某处是一个属性,它引用您在metric 参数中传递给它的实例方法。 Pickle 不会 pickle 实例方法,因此会出现错误。

    如果可能的话,一种解决方法是将方法 distanceCIE2000_classifier() 从您的类中移出到常规的独立函数中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2020-04-07
      • 2017-10-23
      • 2013-08-18
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多