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

新手上路,请多包涵

我有两个数据点 xy

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

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

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

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

原文由 Al_Iskander 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 601
2 个回答

那是你要的吗?

 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

原文由 MaxU - stop russian terror 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 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");

在此处输入图像描述

原文由 Vlad Bezden 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题