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

Al_*_*der 3 python interpolation dataframe pandas

我有两个数据点xy:

  x = 5 (value corresponding to 95%)
  y = 17 (value corresponding to 102.5%)
Run Code Online (Sandbox Code Playgroud)

不,我想计算xi应该对应100%的值.

 x = 5 (value corresponding to 95%)
 xi = ?? (value corresponding to 100%)
 y = 17 (value corresponding to 102.5%)
Run Code Online (Sandbox Code Playgroud)

我应该如何使用python执行此操作?

Tim*_*Tim 7

我们可以在没有Python的图形上轻松绘制它:

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

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

替换为等式的数字给出了这样的:

所以我们知道0.625我们增加Y值,我们将X值增加1.

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

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

我们也可以使用渐变(1 / m)的倒数向后移动.

我们已经得到X是13.我们知道102.5与17有关13 - 17 = -4.-4 / 0.625 = -2.5然后102.5 + -2.5 = 100.

我们如何在python中执行此操作?

def findXPoint(xa,xb,ya,yb,yc):
    m = (xa - xb) / (ya - yb)
    xc = (yc - yb) * m + xb
    return
Run Code Online (Sandbox Code Playgroud)

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

def findYPoint(xa,xb,ya,yb,xc):
    m = (ya - yb) / (xa - xb)
    yc = (xc - xb) * m + yb
    return yc
Run Code Online (Sandbox Code Playgroud)

此函数还将从数据点推断.


Max*_*axU 6

那是你要的吗?

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
Run Code Online (Sandbox Code Playgroud)


Vla*_*den 6

您可以使用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");
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明