Python加载npz文件包括Scipy对象

时间:2019-10-08 16:27:53

标签: python numpy scipy

我不知道如何正确加载scipy对象。我看了类似的问题'Save scipy object to file',但是字典示例不适用于我的情况。

#!/usr/bin/python3.5
import numpy as np
x1=np.array([1,2,3,4,5,6,7])
y1=np.array([10,20,30,40,50,60,70])

# Interpolate
from scipy.interpolate import interp1d
interpFunc = interp1d(x1, y1, kind='cubic')
print('The intermediadate value is',interpFunc(2.5))

import os as osCommand
# Save
fileSaveDir = osCommand.getcwd() + "/test.npz"
np.savez_compressed(fileSaveDir,x1=x1,y1=y1,interpFunc=interpFunc)

# Let''s Load
data=np.load(fileSaveDir,'r',allow_pickle=True)
print(data['interpFunc'])
recallFunc=data['interpFunc']
print(recallFunc.item()[2.5])

中间值为25。它会出现以下错误:

The intermediadate value is 25.0
<scipy.interpolate.interpolate.interp1d object at 0x7ff3df5a8048>
Traceback (most recent call last):
  File "general.py", line 21, in <module>
    print(recallFunc.item()[2.5])
TypeError: 'interp1d' object is not subscriptable

如果我要泡菜模块,有人可以给我看吗?

1 个答案:

答案 0 :(得分:0)

解决方案是

print(recallFunc.item()(2.5))

或更好的方法,

recallFunc=data['interpFunc'].item()
print(recallFunc(2.5))

感谢hpaulj