简体   繁体   English

Python numpy 插值给出错误的输出

[英]Python numpy interpolation gives wrong output

I have an input and output array.我有一个输入和输出数组。 I have given below the plot.我已经给出了下面的情节。 I want to interpolate value at x=0.我想在 x=0 处插入值。 I was expecting something like around 16.7 but instead giving 17.4881, peak value.我期待大约 16.7 左右,但给出了 17.4881 的峰值。 What could be wrong.可能有什么问题。

Data :数据 :

在此处输入图片说明

My code:我的代码:

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)

Present output:当前输出:

  17.4885

Expected output:预期输出:

16.7 # around from plot

If you look at interp function documentation , it says that如果您查看interp 函数文档,它会说

The x-coordinates of the data points, must be increasing if argument period is not specified.如果未指定参数周期,则数据点的 x 坐标必须增加。 Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.否则,在使用 xp = xp % 周期对周期性边界进行归一化后,xp 将在内部进行排序。

But your xdata is in descending order, so you need to reverse order in xdata and ydata但是你的 xdata 是降序排列的,所以你需要在xdataydata颠倒顺序

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM