【发布时间】:2025-12-25 09:00:10
【问题描述】:
使用interp1d 进行kind='cubic' 插值获得的结果似乎在scipy 0.18.1 和1.2.1 之间变化。我将python 2.7.12/numpy 1.12.0 用于前者,python 3.6.8/numpy 1.16.2 用于后者。源代码中的哪个更改导致了不同的结果?
示例代码:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
np.random.seed(1000)
n_p = 6
n_p_interpolated = 11
a = np.linspace(0,5,n_p)
a_interpolated = np.linspace(0,5,n_p_interpolated)
b = np.random.rand(n_p)
b_interpolated = interp1d(a,b,kind='cubic')(a_interpolated)
print('b:', b)
print('b_interpolated:', b_interpolated)
plt.ion()
plt.figure()
plt.plot(a,b,label='original data')
plt.plot(a_interpolated,b_interpolated,label='interpolated data')
plt.legend()
plt.xlim(-1,6)
plt.ylim(-0.2,1)
与scipy 0.18.1:
('b:', array([ 0.65358959, 0.11500694, 0.95028286, 0.4821914 , 0.87247454,
0.21233268]))
('b_interpolated:', array([ 0.65358959, -0.1292027 , 0.11500694, 0.64514961, 0.95028286,
0.75328508, 0.4821914 , 0.58350879, 0.87247454, 0.95267129,
0.21233268]))
与scipy 1.2.1:
b: [0.65358959 0.11500694 0.95028286 0.4821914 0.87247454 0.21233268]
b_interpolated: [ 0.65358959 -0.05237073 0.11500694 0.62584926 0.95028286 0.75365451
0.4821914 0.60133139 0.87247454 0.88101143 0.21233268]
所有内插值(不在原始网格中)都是不同的,有些是完全不同的(例如第二个)。
【问题讨论】:
标签: python scipy interpolation cubic-spline