gpt4 book ai didi

python - Python 中的 MATLAB interp2 函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:04 26 4
gpt4 key购买 nike

我需要一个与 interp2 MATLAB 函数等效的 Python。我正在尝试制作这个 MATLAB example在 Python 中工作,但我不会。

import numpy as np

from scipy.interpolate import interp2d
from scipy.interpolate import RectBivariateSpline

service = np.array(range(10, 31, 10))
years = np.array(range(1950, 1991, 10))

wage = np.array([[150.6970,199.5920,187.6250],
[179.3230, 195.0720, 250.2870],
[203.2120, 179.0920, 322.7670],
[226.5050, 153.7060, 426.7300],
[249.6330, 120.2810, 598.2430]])

ip = RectBivariateSpline(years, service, wage)

print(ip(15, 1975))

但是我得到了这个我无法解决的错误(在 RectBivariateSpline 中):

    Traceback (most recent call last):
File "/Users/andrea/Documents/workspace/PythonProjects/pyArmBot/src/foo.py", line 15, in <module>
ip = RectBivariateSpline(years, service, wage)
File "/Library/Python/2.7/site-packages/scipy-0.10.1-py2.7-macosx-10.7-intel.egg/scipy/interpolate/fitpack2.py", line 728, in __init__
kx,ky,s)

最佳答案

我相信您需要使用以下行而不是您的打印行来调用 ip 以返回一个值。

print(ip.ev(15, 1975))

更新:

非常简单的调整。并盯着我的脸。您可以设置 kx 和 ky 的值,它们是双变量样条的度数 See documentation here

无论如何,只需将线调整为:

ip = RectBivariateSpline(years, service, wage, kx=2, ky=2)

如果你不想,你不需要调整kx。如果您将 kx 保留为 3,我怀疑它会大大改变插值。

关于python - Python 中的 MATLAB interp2 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503998/

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