【问题标题】:Python Load npz file included Scipy ObjectPython 加载 npz 文件包括 Scipy 对象
【发布时间】:2019-10-09 01:21:43
【问题描述】:

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

#!/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

如果我应该腌制模块,有人可以告诉我吗?

【问题讨论】:

  • 在保存之前使用interpFunc(2.5)。为什么[2.5]之后?
  • 是的,它有效,谢谢。

标签: python numpy scipy


【解决方案1】:

解决办法是

print(recallFunc.item()(2.5))

或者更好的方法,

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

谢谢hpaulj

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2018-06-29
    相关资源
    最近更新 更多