python - 用 Python 进行样条插值

标签 python interpolation spline cubic

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

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/

相关文章:

python - 如何构建距离或相异矩阵?

android - Infinite ObjectAnimator with interpolator...如何仅加速初始启动?

matlab - matlab中更快的插值方法

python - 确定任意 X 坐标处的 B 样条曲线值

c++ - 在 QGraphicsView 上实现交互式样条曲线的最佳方法是什么?

c++ - 三次方贝塞尔曲线到二次方

python:我可以将字符串格式化运算符与类的 getter 方法一起使用吗?

python - 将整数从一个 def 编辑到另一个

python - BeautifulSoup,更改特定样式属性

nlp - 插值权重