簡體   English   中英

如何從數組中刪除數據,然后在python中插入該間隙?

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

因此,我正在查看一天的數據。 我正在嘗試從數據集中刪除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

任何幫助表示贊賞。 謝謝!

您無需使用循環即可完成此操作。 這是一個包含更多“有意義”數據的示例。 (您當前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])

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM