三次样条插值用大于以下值的值填充 NaN
Cubic spline interpolation filling NaN with larger values than the following value
我正在进行三次样条插值,我得到的输出对我来说似乎有点奇怪。输入到 NaN 值中的值大于 NaN 之前的数字并且大于 NaN 之后的值。如果我使用 method=linear
就没有问题。但是,我更愿意使用 method=cubicspline
.
下面是我希望插入的部分和输出的示例:
df['a'] = df['a'].interpolate(method='cubicspline')
插值前
a
1 12.036
2 12.22
3 12.306
4 17.019
5 NaN
6 NaN
7 NaN
8 18.624
9 18.615
10 19.098
11 19.156
插值后
a
1 12.036
2 12.22
3 12.306
4 17.019
5 20.0825
6 20.5013
7 19.5803
8 18.624
9 18.615
10 19.098
11 19.156
如果有人知道我做错了什么,我将不胜感激!谢谢
要全面了解为什么会发生这种情况,您必须以更精细的步骤绘制样条曲线,以显示已拟合的三次多项式。
import numpy as np
import matplotlib.pyplot as plt
points = [12.036, 12.22, 12.306, 17.019, 18.624, 18.615, 19.098, 19.15]
ipoints = [12.036, 12.22, 12.306, 17.019, 20.0825, 20.5013, 19.5803, 18.624, 18.615, 19.098, 19.15]
plt.plot([1, 2, 3, 4, 8, 9, 10, 11], points, label='real')
plt.plot(range(1, 12), ipoints, label='pandas')
from scipy.interpolate import CubicSpline as CS
cs = CS([1, 2, 3, 4, 8, 9, 10, 11], points)
plt.plot(range(1, 12), cs(range(1, 12)), label='scipy')
x = np.linspace(1, 12, 200)
plt.plot(x, cs(x), label='scipy-fine')
plt.legend()
plt.show()
我正在进行三次样条插值,我得到的输出对我来说似乎有点奇怪。输入到 NaN 值中的值大于 NaN 之前的数字并且大于 NaN 之后的值。如果我使用 method=linear
就没有问题。但是,我更愿意使用 method=cubicspline
.
下面是我希望插入的部分和输出的示例:
df['a'] = df['a'].interpolate(method='cubicspline')
插值前
a
1 12.036
2 12.22
3 12.306
4 17.019
5 NaN
6 NaN
7 NaN
8 18.624
9 18.615
10 19.098
11 19.156
插值后
a
1 12.036
2 12.22
3 12.306
4 17.019
5 20.0825
6 20.5013
7 19.5803
8 18.624
9 18.615
10 19.098
11 19.156
如果有人知道我做错了什么,我将不胜感激!谢谢
要全面了解为什么会发生这种情况,您必须以更精细的步骤绘制样条曲线,以显示已拟合的三次多项式。
import numpy as np
import matplotlib.pyplot as plt
points = [12.036, 12.22, 12.306, 17.019, 18.624, 18.615, 19.098, 19.15]
ipoints = [12.036, 12.22, 12.306, 17.019, 20.0825, 20.5013, 19.5803, 18.624, 18.615, 19.098, 19.15]
plt.plot([1, 2, 3, 4, 8, 9, 10, 11], points, label='real')
plt.plot(range(1, 12), ipoints, label='pandas')
from scipy.interpolate import CubicSpline as CS
cs = CS([1, 2, 3, 4, 8, 9, 10, 11], points)
plt.plot(range(1, 12), cs(range(1, 12)), label='scipy')
x = np.linspace(1, 12, 200)
plt.plot(x, cs(x), label='scipy-fine')
plt.legend()
plt.show()