插值时间序列,select 来自 x 的 y 值
Interpolate time series, select y value from x
我一直在寻找这个问题的答案有一段时间了,已经很接近了,但一直 运行 出错。有很多类似的问题几乎可以回答这个问题,但我一直无法解决。任何帮助或正确方向的观点都将受到赞赏。
我有一张图表显示温度主要是深度的非线性函数,其中 x 和 y 值来自 pandas 数据框。
import matplotlib.pyplot as plt
x = (22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37)
y = (5, 16, 23, 34, 61, 68, 77, 86)
#Plot details
plt.figure(figsize=(10,7)), plt.plot(style='.-')
plt.title("Temperature as a Function of Depth")
plt.xlabel("Temperature"), plt.ylabel("Depth")
plt.gca().invert_yaxis()
plt.plot(x,y, linestyle='--', marker='o', color='b')
这给了我一张有点像这个的图像(注意翻转的 y 轴,因为我在谈论深度):
我想找到特定 x 值 22.61 处的 y 值,这不是数据集中的原始温度值之一。我尝试了以下步骤:
np.interp(22.61, x1, y1)
这给了我一个我知道不正确的值,
s = pd.Series([5,16,23,34,np.nan,61,68,77,86], index=[22.81,22.81,22.78,22.71,22.61,22.55,22.54,22.51,22.37])
s.interpolate(method='index')
我正在尝试设置一个框架并强制进行插值。我也试过了
line = plt.plot(x,y)
xvalues = line[0].get_xdata()
yvalues = line[0].get_ydata()
idx = np.where(xvalues==xvalues[3]) ## 3 is the position
yvalues[idx]
但是这个 returns y 值是一个特定的、已经列出的 x 值,而不是一个内插值。
我希望这已经足够清楚了。我是数据科学和 Whosebug 的新手,所以如果我需要重新表述问题,请告诉我。
我认为 Scipy 提供了更直观的 API 来解决这个问题。然后您可以轻松地继续使用 Pandas.
中的数据
from scipy.interpolate import interp1d
x = np.array((22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37))
y = np.array((5, 16, 23, 34, 61, 68, 77, 86))
# fit the interpolation on the original index and values
f = interp1d(x, y, kind='linear')
# perform interpolation for values across the full desired index
f([22.81,22.81,22.78,22.71,22.61,22.55,22.54,22.51,22.37])
输出:
array([16. , 16. , 23. , 34. , 50.875, 61. , 68. , 77. ,
86. ])
您也可以选择多个其他非线性插值(二次、三次等)。查看综合 interpolation documentation 了解更多详情。
[编辑]:您需要按照@ImportanceOfBeingErnest 添加的那样在 x 轴上对数组进行排序。
您确实可以使用numpy.interp
功能。正如文档所述
The x-coordinates of the data points, must be increasing [...]
因此在使用此函数之前,您需要对 x 数组上的数组进行排序。
# Sort arrays
xs = np.sort(x)
ys = np.array(y)[np.argsort(x)]
# x coordinate
x0 = 22.61
# interpolated y coordinate
y0 = np.interp(x0, xs, ys)
完整代码:
import numpy as np
import matplotlib.pyplot as plt
x = (22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37)
y = (5, 16, 23, 34, 61, 68, 77, 86)
# Sort arrays
xs = np.sort(x)
ys = np.array(y)[np.argsort(x)]
# x coordinate
x0 = 22.61
# interpolated y coordinate
y0 = np.interp(x0, xs, ys)
#Plot details
plt.figure(figsize=(10,7)), plt.plot(style='.-')
plt.title("Temperature as a Function of Depth")
plt.xlabel("Temperature"), plt.ylabel("Depth")
plt.gca().invert_yaxis()
plt.plot(x,y, linestyle='--', marker='o', color='b')
plt.plot(x0,y0, marker="o", color="C3")
我一直在寻找这个问题的答案有一段时间了,已经很接近了,但一直 运行 出错。有很多类似的问题几乎可以回答这个问题,但我一直无法解决。任何帮助或正确方向的观点都将受到赞赏。
我有一张图表显示温度主要是深度的非线性函数,其中 x 和 y 值来自 pandas 数据框。
import matplotlib.pyplot as plt
x = (22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37)
y = (5, 16, 23, 34, 61, 68, 77, 86)
#Plot details
plt.figure(figsize=(10,7)), plt.plot(style='.-')
plt.title("Temperature as a Function of Depth")
plt.xlabel("Temperature"), plt.ylabel("Depth")
plt.gca().invert_yaxis()
plt.plot(x,y, linestyle='--', marker='o', color='b')
这给了我一张有点像这个的图像(注意翻转的 y 轴,因为我在谈论深度):
我想找到特定 x 值 22.61 处的 y 值,这不是数据集中的原始温度值之一。我尝试了以下步骤:
np.interp(22.61, x1, y1)
这给了我一个我知道不正确的值,
s = pd.Series([5,16,23,34,np.nan,61,68,77,86], index=[22.81,22.81,22.78,22.71,22.61,22.55,22.54,22.51,22.37])
s.interpolate(method='index')
我正在尝试设置一个框架并强制进行插值。我也试过了
line = plt.plot(x,y)
xvalues = line[0].get_xdata()
yvalues = line[0].get_ydata()
idx = np.where(xvalues==xvalues[3]) ## 3 is the position
yvalues[idx]
但是这个 returns y 值是一个特定的、已经列出的 x 值,而不是一个内插值。
我希望这已经足够清楚了。我是数据科学和 Whosebug 的新手,所以如果我需要重新表述问题,请告诉我。
我认为 Scipy 提供了更直观的 API 来解决这个问题。然后您可以轻松地继续使用 Pandas.
中的数据from scipy.interpolate import interp1d
x = np.array((22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37))
y = np.array((5, 16, 23, 34, 61, 68, 77, 86))
# fit the interpolation on the original index and values
f = interp1d(x, y, kind='linear')
# perform interpolation for values across the full desired index
f([22.81,22.81,22.78,22.71,22.61,22.55,22.54,22.51,22.37])
输出:
array([16. , 16. , 23. , 34. , 50.875, 61. , 68. , 77. ,
86. ])
您也可以选择多个其他非线性插值(二次、三次等)。查看综合 interpolation documentation 了解更多详情。
[编辑]:您需要按照@ImportanceOfBeingErnest 添加的那样在 x 轴上对数组进行排序。
您确实可以使用numpy.interp
功能。正如文档所述
The x-coordinates of the data points, must be increasing [...]
因此在使用此函数之前,您需要对 x 数组上的数组进行排序。
# Sort arrays
xs = np.sort(x)
ys = np.array(y)[np.argsort(x)]
# x coordinate
x0 = 22.61
# interpolated y coordinate
y0 = np.interp(x0, xs, ys)
完整代码:
import numpy as np
import matplotlib.pyplot as plt
x = (22.81, 22.81, 22.78, 22.71, 22.55, 22.54, 22.51, 22.37)
y = (5, 16, 23, 34, 61, 68, 77, 86)
# Sort arrays
xs = np.sort(x)
ys = np.array(y)[np.argsort(x)]
# x coordinate
x0 = 22.61
# interpolated y coordinate
y0 = np.interp(x0, xs, ys)
#Plot details
plt.figure(figsize=(10,7)), plt.plot(style='.-')
plt.title("Temperature as a Function of Depth")
plt.xlabel("Temperature"), plt.ylabel("Depth")
plt.gca().invert_yaxis()
plt.plot(x,y, linestyle='--', marker='o', color='b')
plt.plot(x0,y0, marker="o", color="C3")