pandas 两个数据点之间的线性插值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38282659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:32:52  来源:igfitidea点击:

linear interpolation between two data points

pythonpandasdataframeinterpolation

提问by Al_Iskander

I have two data points xand y:

我有两个数据点xy

  x = 5 (value corresponding to 95%)
  y = 17 (value corresponding to 102.5%)

No I would like to calculate the value for xiwhich should correspond to 100%.

不,我想计算xi应对应于 100% 的值。

 x = 5 (value corresponding to 95%)
 xi = ?? (value corresponding to 100%)
 y = 17 (value corresponding to 102.5%)

How should I do this using python?

我应该如何使用 python 做到这一点?

回答by MaxU

is that what you want?

那是你要的吗?

In [145]: s = pd.Series([5, np.nan, 17], index=[95, 100, 102.5])

In [146]: s
Out[146]:
95.0      5.0
100.0     NaN
102.5    17.0
dtype: float64

In [147]: s.interpolate(method='index')
Out[147]:
95.0      5.0
100.0    13.0
102.5    17.0
dtype: float64

回答by Tim

We can easily plot this on a graph without Python:

我们可以在没有 Python 的情况下轻松地将其绘制在图形上:

This shows us what the answer should be (13).

这向我们展示了答案应该是什么(13)。

But how do we calculate this? First, we find the gradient with this:

但是我们如何计算呢?首先,我们找到梯度:

The numbers substituted into the equation give this:

代入方程的数字给出了这个:

So we know for 0.625 we increase the Y value by, we increase the X value by 1.

所以我们知道对于 0.625,我们将 Y 值增加了,我们将 X 值增加了 1。

We've been given that Y is 100. We know that 102.5 relates to 17. 100 - 102.5 = -2.5. -2.5 / 0.625 = -4and then 17 + -4 = 13.

我们已经知道 Y 是 100。我们知道 102.5 与 17 相关100 - 102.5 = -2.5-2.5 / 0.625 = -4然后17 + -4 = 13

This also works with the other numbers: 100 - 95 = 5, 5 / 0.625 = 8, 5 + 8 = 13.

这也适用于其他数字:100 - 95 = 5, 5 / 0.625 = 8, 5 + 8 = 13

We can also go backwards using the reciprocal of the gradient (1 / m).

我们也可以使用梯度的倒数 ( 1 / m)倒退。

We've been given that X is 13. We know that 102.5 relates to 17. 13 - 17 = -4. -4 / 0.625 = -2.5and then 102.5 + -2.5 = 100.

我们已经知道 X 是 13。我们知道 102.5 与 17 相关13 - 17 = -4-4 / 0.625 = -2.5然后102.5 + -2.5 = 100

How do we do this in python?

我们如何在python中做到这一点?

def findXPoint(xa,xb,ya,yb,yc):
    m = (xa - xb) / (ya - yb)
    xc = (yc - yb) * m + xb
    return

And to find a Y point given the X point:

并在给定 X 点的情况下找到 Y 点:

def findYPoint(xa,xb,ya,yb,xc):
    m = (ya - yb) / (xa - xb)
    yc = (xc - xb) * m + yb
    return yc

This function will also extrapolate from the data points.

此函数还将根据数据点进行推断。

回答by Vlad Bezden

You can use numpy.interpfunction to interpolate a value

您可以使用numpy.interp函数来插入一个值

import numpy as np
import matplotlib.pyplot as plt

x = [95, 102.5]
y = [5, 17]

x_new = 100

y_new = np.interp(x_new, x, y)
print(y_new)
# 13.0

plt.plot(x, y, "og-", x_new, y_new, "or");

enter image description here

在此处输入图片说明