如何从数组中删除数据,然后在 python 中的间隙进行插值?

How to delete data from array and then interpolate over that gap in python?

所以我正在查看一天的数据。我正在尝试从数据集中删除 12:00 到 13:00 的小时,然后根据其余数据在该小时内进行插值。

我运行遇到两个问题:

  1. 我删除数据的方式不正确,我不确定如何修复它。它删除了数据,但随后又将数据转移过来,因此没有间隙。当我绘制数据图表时,这一点很明显,没有差距。

  2. 不知道需要用interpolate.interp1d还是interpolate.splev

这不是我正在使用的确切数据,但它是一个很好的例子,可以说明我需要做什么以及我已经尝试过什么。

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from matplotlib import pylab

day_sec=np.arange(0,86400,1) #24 hours in seconds
ydata=np.exp(-day_sec/30) #random ydata for example

remove_seconds=np.arange(43200,46800,1) #12-13:00 in seconds

last=len(remove_seconds)-1 

for i in range(len(day_sec)): #what I have tried to remove the data, but failed
  if (day_sec[i] >= remove_seconds[0]) and (day_sec[i] <= remove_seconds[last]):
    new_day_sec=np.delete(day_sec,[i],None)
    new_ydata=np.delete(ydata,[i],None)

f=interpolate.interp1d(new_day_sec,new_ydata) #not sure this is correct

感谢任何帮助。谢谢!

您可以在不使用循环的情况下完成此操作。这是一个包含更多 "meaningful" y 数据的示例。 (你当前ydata的均值为0,期望也为0。)

import numpy as np
from scipy import interpolate

# Step one - build your data and combine it into one 2d array
day_sec = np.arange(0, 86400)
ydata = np.random.randn(day_sec.shape[0]) + np.log(day_sec + 1)
data = np.column_stack((day_sec, ydata))

# Step two - get rid of the slice from 12:13 with a mask
# Remember your array is 0-indexed
remove_seconds = np.arange(43200, 46800)
mask = np.ones(data.shape[0], dtype=bool)
# Mask is now False within `remove_seconds` and True everywhere else.
# This allows us to filter (boolean mask) `data`.
mask[remove_seconds] = False
newdata = data[mask, :]

# Step three - interpolate
# `f` here is just a function, not an array.  You need to input
#     your "dropped" range of seconds to produce predicted values.
f = interpolate.interp1d(newdata[:, 0], newdata[:, 1])
predicted = f(remove_seconds)

# If you want to insert the interpolated data
data[~mask, 1] = predicted

plt.plot(data[:, 0], data[:, 1])