Python numpy 插值给出了错误的输出
Python numpy interpolation gives wrong output
我有一个输入和输出数组。我在下面给出了情节。我想在 x=0 处插入值。我原本期待大约 16.7,但给出的是 17.4881,峰值。有什么问题。
数据:
我的代码:
xdata = [0.101,-0.008,-0.111,-0.209,-0.303]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885]
xp = np.interp(0,xdata,ydata)
print(xp)
当前输出:
17.4885
预期输出:
16.7 # around from plot
如果你看interp function documentation,上面写着
The x-coordinates of the data points, must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.
但是你的xdata是降序的,所以你需要把xdata
和ydata
中的顺序倒过来
import numpy as np
xdata = [0.101,-0.008,-0.111,-0.209,-0.303][::-1]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885][::-1]
xp = np.interp(0,xdata,ydata)
print(xp)
# 16.778545871559633
我有一个输入和输出数组。我在下面给出了情节。我想在 x=0 处插入值。我原本期待大约 16.7,但给出的是 17.4881,峰值。有什么问题。
数据:
我的代码:
xdata = [0.101,-0.008,-0.111,-0.209,-0.303]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885]
xp = np.interp(0,xdata,ydata)
print(xp)
当前输出:
17.4885
预期输出:
16.7 # around from plot
如果你看interp function documentation,上面写着
The x-coordinates of the data points, must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.
但是你的xdata是降序的,所以你需要把xdata
和ydata
import numpy as np
xdata = [0.101,-0.008,-0.111,-0.209,-0.303][::-1]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885][::-1]
xp = np.interp(0,xdata,ydata)
print(xp)
# 16.778545871559633