Python pickle:不能pickle <class'interpnd.linearndinterpolator'=“”>:它找不到interpnd.LinearNDInterpolator </class>

时间:2012-03-23 09:46:46

标签: python interpolation pickle

我在Python中遇到了Pickle模块的问题。我想插入一些4参数数据,所以我想构建和存储插值器,以便它可以在另一个代码中重用,而不需要在执行时重新生成。我实际上在每次执行代码时都会浪费很多时间重新创建它。 我能得到的就是这条消息:

  

“PicklingError:不能发泡:   它没有被发现为interpnd.LinearNDInterpolator“

我正在试图弄清楚如何解决它,这是我的代码的简化版本(Python v2.7),跟踪如下 提前感谢您的建议

a1,a2,a3,a4 = np.mgrid[paramvariation]
pts = np.array([a1.ravel(),a2.ravel(),a3.ravel(),a4.ravel()]).T
sr=paramvalues.ravel()
LinearNDInterp = interp.interpnd.LinearNDInterpolator(pts,sr,fill)

fileid=open("try.out",'w')
pickle.dump(LinearNDInterp,fileid) 
fileid.close()

Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 128, in runfile
execfile(filename, glbs)
File "tryout.py", line 516, in <module>
main("")
File "tryout.py", line 354, in main
pickle.dump(LinearNDInterp,fileid)
File "C:\Python27\lib\pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "C:\Python27\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python27\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python27\lib\pickle.py", line 401, in save_reduce
save(args)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 562, in save_tuple
save(element)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 748, in save_global
(obj, module, name))
PicklingError: Can't pickle <class 'interpnd.LinearNDInterpolator'>: it's not found as interpnd.LinearNDInterpolator

2 个答案:

答案 0 :(得分:2)

看起来像SciPy中最近修复的错误:
BUG: interpolate: make interpnd.* objects pickleable

答案 1 :(得分:0)

简单的解决方法就是挑选LinearNDInterpolator的参数。