gpt4 book ai didi

python - 用 Python 进行样条插值

转载 作者:太空狗 更新时间:2023-10-29 17:55:55 26 4
gpt4 key购买 nike

我编写了以下代码来执行样条插值:

import numpy as np
import scipy as sp

x1 = [1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

x = np.array(x1)
y = np.array(y1)

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

但我得到:

ValueError: A value in x_new is below the interpolation range.

interpolate.py

如有任何帮助,我们将不胜感激。

最佳答案

来自scipy documentation on scipy.interpolate.interp1d :

scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=True, fill_value=np.nan)

x : array_like. A 1-D array of monotonically increasing real values.

...

问题是 x 值不是 monotonically increasing .事实上,它们是单调递减的。让我知道这是否有效以及它是否仍然是您正在寻找的计算。:

import numpy as np
import scipy as sp
from scipy.interpolate import interp1d

x1 = sorted([1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02])
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]

new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

关于python - 用 Python 进行样条插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851770/

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