python - 在另一个 1d/2d 向量之后重采样(缩小)2D 向量

标签 python numpy pandas scipy time-series

在地球科学中,我们面临一个共同的问题。有时,我们的数据代表以不同时间间隔采样的任何给定参数,这些时间间隔不一定均匀。

例如,我们有时间与温度的关系,但温度读数之间的时间间隔不是均匀分布的。现在,很多时候我们需要将此时间序列与以不同时间间隔采样且间隔不均匀的另一个时间序列(对于相同或任何其他参数,例如湿度)进行比较。

我想在Python中做的是用更多的数据点(温度)缩小系列,对数据进行插值并将其重新采样到与第二个时间序列(本例中为湿度)相同的时间间隔。

在Python中以编程方式,我有2个不同长度的二维数组,我想将最长的数组重新采样到与较短数组相同的间隔,并在必要时进行线性插值以创建相应的数据点。

Python中是否有任何函数或库可以以简单的方式做到这一点?

谢谢

最佳答案

我相信您正在寻找 SciPy 的插值:

http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

我已经调整了示例以适用于间隔不均匀的点。

from scipy.interpolate import interp1d
import numpy as np

x = np.linspace(0, 10, num=11, endpoint=True) + np.random.normal(loc=0, scale=2, size=11)
y = np.cos(-x**2/9.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')

xmin = min(x)
xmax = max(x)
xnew = np.linspace(xmin, xmax, num=101, endpoint=True)
import matplotlib.pyplot as plt
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.show()

enter image description here

关于python - 在另一个 1d/2d 向量之后重采样(缩小)2D 向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31708145/

相关文章:

python - 使用 NumPy 索引数组对 Python 列表进行切片——任何快速的方法?

python - 根据 Pandas 中的索引连接多列

python - Unicode 警告 : Unicode equal comparison failed to convert both arguments to Unicode

python - python 中 ttk TreeView 的 3 个不同问题

python - Numpy CSV fromfile()

python - numpy SVD 给出错误的值?

python - 使用 Pandas 从 csv 文件中读取标题信息

Python Pandas Dataframe 仅添加负数条件

python - 帮助错误信息

Python:无法写入 CSV 文件