python - 如何从数组中删除数据,然后在 python 中插入该间隙?

标签 python arrays python-3.x interpolation

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

我遇到了两个问题:

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

  2. 我不知道我是否需要使用 interpolate.interp1dinterpolate.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

感谢任何帮助。谢谢!

最佳答案

您可以在不使用循环的情况下完成此操作。这是一个包含一些更“有意义”的 y 数据的示例。 (您当前的 ydata 的平均值为 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])

enter image description here

关于python - 如何从数组中删除数据,然后在 python 中插入该间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352437/

相关文章:

python - 如何将表示时间范围的字符串转换为 struct_time 对象?

c++ - C++ 中的数组排序问题

python - 类型错误:无法将 'builtin_function_or_method' 对象隐式转换为 str

java - 局部变量整数数组默认为零

python - 在 Python 中随机获取重复值数组的 3 个最小值

python - keras中的fit_generator,将所有内容加载到内存中

python - 如何在Python中读取压缩文件夹内的文件夹中的文件

python - Python 查找两个元组中的对应项

python - 如何计算numpy数组的两行之间的欧氏距离

python - django:胖模型和瘦 Controller ?