2
我有一個代碼,我建立一棵巨大的樹,我需要保存它以備後用。 不幸的是,似乎我不能泡菜scipy.spatial.KDTree
對象。無法醃scipy.spatial.KDTree對象
事實上,當我運行此:
import pickle
import scipy.spatial
tree=scipy.spatial.KDTree([[1,2,3]])
pickle.dump(tree,open('tree.p','wb'))
我得到的錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib64/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib64/python2.7/pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib64/python2.7/pickle.py", line 419, in save_reduce
save(state)
File "/usr/lib64/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib64/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib64/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib64/python2.7/pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib64/python2.7/pickle.py", line 401, in save_reduce
save(args)
File "/usr/lib64/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib64/python2.7/pickle.py", line 562, in save_tuple
save(element)
File "/usr/lib64/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib64/python2.7/pickle.py", line 748, in save_global
(obj, module, name)) pickle.PicklingError: Can't pickle <class 'scipy.spatial.kdtree.leafnode'>: it's not found as scipy.spatial.kdtree.leafnode
鑑於此,有沒有辦法鹹菜呢?或者至少保存一部分可以用來快速重建樹的對象?否則,除了scipy.spatial.KDTree還有其他快速選項嗎?
這似乎cKDTree的一個很酷的功能,他們應該definely提到它在文檔中 –