gpt4 book ai didi

python - 无法 pickle scipy.spatial.KDTree 对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:12 25 4
gpt4 key购买 nike

我有一个代码,我在其中构建了一棵巨大的树,我需要保存它以备后用。不幸的是,我似乎无法 pickle 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

鉴于此,有没有办法 pickle 它?或者至少保存可用于快速重建树的对象的一部分?否则,除了 scipy.spatial.KDTree 之外,还有其他快速选项吗?

最佳答案

使用cKDTree代替KDTree如下:

import pickle
import scipy.spatial
tree=scipy.spatial.cKDTree([[1,2,3]])
pickle.dump(tree,open('tree.p','wb'))

关于python - 无法 pickle scipy.spatial.KDTree 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com